Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can I reference a non local variable of a different script?

Asked by 8 years ago

Is it possible to reference a variable from a different script from my current one? for example if in script 1 I have VARIABLE1 = THING1 in my second script can I somehow tell it to use the VARIABLE1 variable? Thanks guys!

0
Im sure you can do that with module scripts TrollD3 105 — 8y

1 answer

Log in to vote
0
Answered by
Necrorave 560 Moderation Voter
8 years ago

You have a couple options regarding what you are looking for.

First would be what we call Global Variables. You can declare a global variable by placing _G. before the name of the variable. You will be able to call this variable from almost any script within your game file.

Script 1:

_G.bool = true -- Creates a global variable and assigns 'true' as the value

Script2:

print (_G.bool) --Should print 'true'

Another option would be to create a Module Script with variables you wish to use in multiple scripts. These work somewhat similar to global variables when you refer to them in other scripts.

Lets say you made a module script returned the array of variables called _M. After you pull the module script into your new script, you can refer to the variables as _M.variable or whatever you named the variable you created in the module script.

Example of module script:

_M = {
    bool = true
}

return _M

How to call the variable from another script:

_M = require('Path to Module Script') -- This will call the module script and return the variables

print (_M.bool) --This should print 'true'

Let me know if this answers your question.

1
(For most cases, global variables are not good practice) BlueTaslem 18071 — 8y
0
so wait if I wanted the variables "child" and "current" to be equal on a global scale would I do _G.child = current ? Gwolflover 80 — 8y
Ad

Answer this question