Grab a variable from two instances of the same object?

I am not sure if this can be done at all but I’m asking here. I’m super new so any tips appreciated. I have two objects (they get a random colour on creation) who - when they are in collision & either has a timer reached - will be set to delete & a third object is created. That’s working great, however currently the 3rd object gets a couple of random colours. I can store the colour in a text variable, that seems to work, however I don’t know how I would go about “getting” a variable from both objects or where I’d store it, an array maybe?

Is it possible to have an action that copies, fetches, whatever - a variable from the two instances of the object in collision? If so how would I set that up?

Currently the collision, creation & deletion are working I’m just stuck on how I’d grab a variable from the two being deleted, I think I know how I’d add it to the third. I am mostly just musing the possibility of doing more with the idea of two objects merging into one new one carrying over something from each, in this case the colour.

Yeah, you could store the values in an array, something like

(local variable) index = 0
for each object
  array[index] = object.color
  index += 1

Sorry I’m not a coder, I’m not sure what I’d do but I appreciate the help. It looks as though it is possible, I might go look up some youtube vids for tutorials on arrays in Gdev :slight_smile:

No worries, I wasn’t really writing code though (although it is close to some languages)

They would actually be events in your events sheet. You’ll need a number variable for the position in the array, and an array variable. Both of these could be declared in the scene or global variables editor, or as local variables for just that event. And, you’ll need the objects to have a text variable storing their color string.

“For each [object]” is a special event where you select the type of object to loop through. So under the event where your instances collide, you would make a sub-event by right clicking and selecting add → For each. In the top line of the for each block, you can select the object. Then the actions would be setting the variables. Let’s say your array is called “colors” and index is called “slot”… the actions would look something like this:

  • Change variable colors[slot]: set to object.color (or whatever your object/variable is called)
  • Change variable index: add 1
1 Like