Best practice: Local variable or go.property?

When should I use a local variables and when should I use a go.property?

Are local variables (not in function) shared if the same script is used at 2 places?

Editing my questions because I can’t post again due to forum restrictions.

So, self.variable without go.property?

LUA allows dynamic runtime creation of member variables?

Yes, local variables are local to the script, not GO instances. You should use “self.variable” to store instance data.

The idea behind go.property is to be able to set instance specific data on a game object, which is why they are exposed in the editor.

1 Like

You have the following four ways and scopes in which to declare variables given a script with name some.script:

EXAMPLE 1

go.property("foobar", 123)

function init(self)
     print(self.foobar)
end

foobar is exposed in the editor when selecting some.script in the outline. foobar is unique to each game object instance with some.script attached. Accessible via self.foobar and go.get("some/url/", "foobar")

EXAMPLE 2

function init(self)
    self.foobar = 123
end

foobar is unique to each game object instance with some.script attached. Accessible via self.foobar.

EXAMPLE 3

local foobar = 123

function init(self)
    print(foobar)
end

foobar is shared between all script instances of type some.script.

EXAMPLE 4

-- in some.script
foobar = 123

-- in some_other.script
function init(self)
    print(foobar)
    print(_G.foobar)
    print(_G["foobar"])
end

foobar is declared on the global table _G and accessible from everywhere.

25 Likes