Best practice for getting variables from another script

I have various scripts in my game that need a particular variable. At the moment I’m sending messages to the script that contains that variable and in turn that script sends back the variable in another message.

The issue is there seems to be a delay in getting that number back - the code doesn’t seem to work linearly and this is a real issue when you need to access the variable quickly. Is there anything in the documentation that outlines the best ways to get a variable immediately from another script?

The messages are dispatched at certain stages during the frame, so there will always be some “delay” until you receive the event. They should be dispatched during the same frame though: “Defold will deliver the message the next time messages are dispatched, which happens within the current frame”

When you want access to global data that you don’t wish to pass around, you can use a lua module to set and get data to.

Thanks again - sounds like a lua module might be my best bet for storing global data.

Did you find script properties and go.get?

3 Likes

Like Ragnar said, script properties and go.get() and go.set() is probably your best bet. You could also look into storing some kind of shared data in a Lua module that you require() from where ever you need to access the data.