Get factory passed values

Hi,

the tutorial for factories says :

function init(self)
self.my_created_object = factory.create(“#factory”, nil, nil, {my_value = 1})
– communicate with the object
msg.post(msg.url(nil, self.my_created_object), “hello”)
end

And then let the new game object have a script attached:
go.property("my_value", 0)
function init(self)
    -- do something with self.my_value which is now one
end

But how can i get the original value, 1? Deleting the go.property line is not working.

Thanks for the help !

If you have a property called my_value, its value will be set by the factory. If you want the original value (in this case 0) you would just omit the {my_value=1} when creating the instance.

My question was badly explained, sorry. I have a tower game object with a script tower.script :

factory.create(“#factory”, nil, nil,{x = action.x, y = action.y})

then i try to get the values sent in my projectile.script, x and y (the mouse position when the projectile is created) but for example

pprint(self.x)

is not working. What should i do to retrieve the data ?

And have you declared ‘x’ and ‘y’ in a script attached to whatever it is you are creating with the factory?

go.property("x", 0)
go.property("y", 0)

Or do you intend for ‘x’ and ‘y’ to actually be the position of the game object you create?

The x and y variables will be the default factory position. I need the x/y variables to send my projectile to the clic position.

Well, declare them using go.property() in your projectile script and then pass them in when you call factory.create(). In the init() function of your projectile script pick up the self.x and self.y use them to set position using go.set_position().

2 Likes

Oookay. I was totally misunderstanding the go.property use. It’s a declaration and not a modification of the variable !
My tower is now properly sending fireballs. Thanks a lot for your help :grinning:

3 Likes

Correct. Anything you declare using go.property(“foobar”, 123) will be:

  • accessible as self.foobar from the script where it is declared
  • accessible from other scripts using go.get("url_or_id_to_game_object", "foobar") (note that this is a bit slow and shouldn’t be used excessively)
  • accessible from the Properties window in the editor when you select the script when it’s attached to a game object (this is a perfect way to expose scriptable properties to non developers or simply as a quick way to reuse a script and modify it’s behaviour from the editor instead of doing it via code)
  • animatable (is that a word?) using go.animate("#", "foobar", go.PLAYBACK_ONCE_FORWARD, 567, go.EASING_LINEAR)
  • a valid property to set when calling factory.create("#factory", nil, nil, { foobar = 567 })
6 Likes