# Adding scripts during runtime

**URL:** <https://forum.defold.com/t/adding-scripts-during-runtime/2094>\
**Category:** Questions\
**Created:** [June 9, 2016, 8:09am UTC](https://forum.defold.com/t/adding-scripts-during-runtime/2094 "2016-06-09T08:09:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GregorGullwi](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@GregorGullwi](https://forum.defold.com/u/GregorGullwi)\
**Post date:** [June 9, 2016, 8:09am UTC](https://forum.defold.com/t/adding-scripts-during-runtime/2094/1 "2016-06-09T08:09:53Z")

</div>

Is it possible to add scripts during runtime to a game object to add new functionality or would I have to have a factory that spawns a separate game object that contains the new script that I want to add?

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [June 9, 2016, 9:09am UTC](https://forum.defold.com/t/adding-scripts-during-runtime/2094/2 "2016-06-09T09:09:06Z")

</div>

You can load and execute scripts via other scripts at runtime. Check these posts

> [@Big List of Defold Pro Tips!](https://forum.defold.com/t/big-list-of-defold-pro-tips/1519/8):
>
> Dynamically loading and running Lua script with Defold. Thanks to @sicher References [(1)](https://forum.defold.com/t/defold-1-2-58-has-been-released/183) [(2)](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/9) If for some reason (such as NPC dialog and movement scripting - which I’ll talk about in another post) you need to load scripts dynamically (as in you don’t know what names they are beforehand, are loading their names from another arbitrary data file) then there are some special considerations requires due of the way Defold bundles files. First put the lua scripts you want to be able to run into a sp…

> [@Big List of Defold Pro Tips!](https://forum.defold.com/t/big-list-of-defold-pro-tips/1519/48):
>
> That works! Nice! Good to know! local script = ("/main/files/level\_1.lua") local code = sys.load\_resource(script) local def = assert(loadstring(code))() print(def.layers[1].type) DEBUG:SCRIPT: tilelayer

---

<div class="post-metadata">

**Author:** ![GregorGullwi](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@GregorGullwi](https://forum.defold.com/u/GregorGullwi)\
**Post date:** [June 9, 2016, 9:29am UTC](https://forum.defold.com/t/adding-scripts-during-runtime/2094/3 "2016-06-09T09:29:55Z")

</div>

The script is regular script file, so I don’t have to load it during runtime. I just wondered if there was an automated way to just add a script that automatically gets the init(), update() and final() callbacks.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 9, 2016, 9:33am UTC](https://forum.defold.com/t/adding-scripts-during-runtime/2094/4 "2016-06-09T09:33:50Z")

</div>

You can load and run code at run-time using the links provided by pkeod, but I consider that solution better suited for downloading and running patched code or similar.

Is that what you want to do, or is it rather like you having a couple of pre-defined and at compile time known snippets of code that should be used run-time under certain conditions? If it’s the latter case then I’d have a script file require one or more modules with code and forward init(), update() and similar life cycle messages to the modules depending on some run-time condition.

---

<div class="post-metadata">

**Author:** ![GregorGullwi](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@GregorGullwi](https://forum.defold.com/u/GregorGullwi)\
**Post date:** [June 9, 2016, 9:35am UTC](https://forum.defold.com/t/adding-scripts-during-runtime/2094/5 "2016-06-09T09:35:42Z")

</div>

The scripts I was thinking about would be static and bundled. I was just wondering if there was a way to skip the forwarding, since it adds manual steps that might be missed when they are reused later.

I might try with a factory and see how it goes or just skip it entirely then.
