About variables and render script (small question)

Render_script changes resolution only once and never runs it’s init function after that and its variables are available to all collections. Does that mean that I can declare truly global variables with it?

Also I have a controller script (collection switcher) above all. Can I declare global variables there?

Any restrictions?

Thanks! =)

In your game.project, under “Script”, there is a “Shared State” setting. If this is not checked, then there will be three separate contexts for .script, .gui_script, and .render_script files. If it is checked, then they will all share the same context. (i.e. global variables)

With lua you can declare, access, and modify global variables from anywhere. Other than the above caveat, Defold doesn’t change this. You don’t have to use the render script, though it does call its init before everything else, if that’s what you mean.

Usually it causes a lot less trouble to use a lua module instead of global variables. As a bonus, in the Defold editor, you can right-click a lua module and click “Referencing Files…” and see every file that is using the module, which is great for debugging. It can be a bit harder to do this with just a global variable name.

The render_script will get a “window_resized” message whenever the window resolution changes, whether your particular render_script uses this message is up to you.

4 Likes

Wow… that was helpful! Thank you, Ross!

1 Like