Sending a table as property using factory.create?

Hi,

I’m very new to Defold and Lua so pardon if this question is stupid but I’m trying to pass a table to be set on the newly created object from a factory:

if message_id == hash("respawn") then
	factory.create("#factory", go.get_position(), vmath.quat(), {history: message.history})
end

I then want to use this history in the new object:

go.property("history", {})
...
-- do stuff with self.history

However, this doesn’t work since I’m getting “Only these types are available: number, hash, msg.url, vmath.vector3, vmath.vector4, vmath.quat”.

What is the correct way to solve this? The history table will be of unknown length (basically recording stuff from the game that the new object needs to take into account).

As the error message state you can not send tables in messages. One way to solve it would be to have the history in a globally available lua module instead:

history.lua:
local _M={
history={}
}

return _M

some_script.script:
local history=require(“history.lua”)

function some_function(self)
– do stuff with history.history
end

1 Like

You can send tables in messages but not have table go.properties. Another way is to do:

local id = factory.create(...)
msg.post(id, "set_table", { table = my_table })

and on the receiving object store the table in self when the message arrives:

function on_message(self, message_id, message, sender)
    if message_id == hash("set_table") then
        self.table = message.table
    end
end
2 Likes

Yes of course you can send tables in messages, I meant you can’t have them as properties. Thanks!

1 Like