Object Clone's Variables

I am having trouble setting the variables of an object’s clone through the variable object.clone() returns. In the project below, I spawn balls and in the spawning process assign them a random number. Then, when showing the value in the console, the value is unchanged and still at 0.
ballTest.wick (2.5 KB)

Am I doing something wrong, or is this a bug?

Edit: the x and y positions work fine, but the custom this.[name] variables do not work

Hey @Baggef,

So this is both expected behavior and a little weird. Since at the top of your default script you set the ball’s number to be 0, it resets the number to 0 immediately on the next tick.

Default is currently called the first tick after an object appears on screen. So to fix this, you can remove the this.num = 0 line from the top of the ball’s default.

@zrispo and I are going to talk this over though. It might make more sense for the default and load scripts to run immediately after a clone.

I do think that

var c = obj.clone();
c.num = 5;

would make more sense.

Removing the declaration statement at the beginning actually causes some undefineds in the project that I’m actually working on. Which you can see in the console
Ball Shoot.wick (7.8 KB)

Yeah, we think so as well.

The new behavior will most likely be:

  1. Clone Object
  2. Default and Load are run immediately
  3. Change properties how you’d like

Good catch by the way and thanks for the clear example.

1 Like