Set constant on GO created by factory

In general you want to keep the number of scripts to a minimum (at least scripts that run a lot of code all the time). Having a script that deals with these simple one-off messages is ok, but you should always be a bit cautious. The solution suggested by Ross does not require that you attach a script to each spawned game object.

Another option would be to have a script and a game object property (in this case color) that you pass to the game object in your factory.create() call:

local col = vmath.vector4(math.random(), math.random(), math.random(), 1)
factory.create("#factory", nil, nil, { color = col })

-- planet.script
go.property("color", vmath.vector4())
function init(self)
   go.set("#sprite", "tint", self.color)
end
1 Like