How do I clone an instance of an object?

Get help using Construct 2

Post » Sun Feb 12, 2017 9:25 am

I am trying to clone multiple instances of the same object, (each with unique instance variables that need to be maintained) The trouble is with "create new object" or "spawn object" it puts all the instance variables to the defaults. And due to picking behaviors it is difficult to associate the new object with its parent, to inherit its variables. Any help would be appreciated.
B
8
S
3
G
1
Posts: 6
Reputation: 1,940

Post » Sun Feb 12, 2017 9:51 am

After the create event, the clone is automatically picked. Just set its variables directly after.

Edit: When you create your parent object, just save its uid somewhere like a global variable. You can then set the clones variables using the saved parent reference.
Last edited by gameglaux on Sun Feb 12, 2017 10:03 am, edited 1 time in total.
Image
B
30
S
9
G
3
Posts: 171
Reputation: 3,329

Post » Sun Feb 12, 2017 9:57 am

Right, that's not the issue though, its bringing the variables from the other instance. if I go "Create object, ObjectA" then "Set instance variable ObjectA, Variable1= ObjectA.Variable1" it thinks I am referring to the object I just created, instead of the preexisting one when setting the new variable
B
8
S
3
G
1
Posts: 6
Reputation: 1,940

Post » Sun Feb 12, 2017 10:04 am

morgandood wrote:Right, that's not the issue though, its bringing the variables from the other instance. if I go "Create object, ObjectA" then "Set instance variable ObjectA, Variable1= ObjectA.Variable1" it thinks I am referring to the object I just created, instead of the preexisting one when setting the new variable


Just edited my answer ;)
Image
B
30
S
9
G
3
Posts: 171
Reputation: 3,329

Post » Sun Feb 12, 2017 10:18 am

I think that'll work in spite of the scale I am doing it on, I just can't figure out how to make that reference in the set instance variable window
B
8
S
3
G
1
Posts: 6
Reputation: 1,940

Post » Sun Feb 12, 2017 10:39 am

Pick the one that you want to 'clone'. (and only that one, do not create a picklist containing more sprites)
Store its expression Sprite.AsJSON in a local text variable.
Create object somewhere outside screen or so, does not matter.
Use the action .... Sprite > Set from JSON ... to set the Just Created sprite to the local variable.

The 'clone' should now be exact.
Meaning, now you have to set its positions and other things that that you want to be different.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sun Feb 12, 2017 10:59 am

Oh thank you! That sounds like it will work perfectly, alternatively I was going to save the parent variables to an array, but it seems more intensive than simply doing what you said. (since its a small repeating process 0-4 times)
B
8
S
3
G
1
Posts: 6
Reputation: 1,940


Return to How do I....?

Who is online

Users browsing this forum: beohs, brunopalermo and 13 guests