Accessing internal state (self) of another game object? (SOLVED)

Hi Playtypus.
You will need to adress the specific script and not the whole gameobject.
go.get("/instance#script",“health”)

To pass data between gameobjects and over the project I usually use this small module which I call gop.lua (GameObjectProperties)

local M = {}

local gop_tables = {}

function M.get(url)
	url = url or msg.url(".")
	if type(url) == "string" then url = msg.url(url) end
	if gop_tables[url.path] == nil then gop_tables[url.path] = {} end
	return gop_tables[url.path]
end

function M.final(url)
	url = url or msg.url(".")
	if type(url) == "string" then url = msg.url(url) end
	gop_tables[url.path] = nil
end

return M

In a go.script I can then do like this:

self.gop = gop.get()
self.gop.health = 20

Now, in another gameobject I can retrieve the same table by getting gop by url:

local data = gop.get("/gameobjects_url")
print(data.health)

Now, I, most of the time only use this to be able to share data between several scripts on the same gameobject. Then I don’t have to pass in any url as it defaults to the gameobject url.
Hope that helps?

11 Likes