[Solved] How to store x & y positions in a way that allows me to ""access"" them at the same time?

Hi guys I’m back. This time my problem is a little but more complicated I think.

This is my game. “Bridges” will spawn on those green places, they are “bridgeSpawns”.

That shows how the bridges are randomly spawned in events (the bridge is in two parts: left and right. They together form the full bridge.)

The problem is that a bridge can spawn on top of old bridge. So I have to keep track where bridges have already spawned. When I first made this game in C#, I stored the positions of spawned bridges in an array of “tuples” which allowed me to hold 2 numbers in the same variable: “100, 90” (100 for x and 90 for y). But I don’t think there is anything similar to that in gDevelop.

I’ll try to explain why “normal” number variables are not powerful enough to work in their usual manner. Let’s say that the first bridge spawns at 50-x, 90-y. I can store both of those in number variables. But now it’s time to spawn a new bridge and I must exclude the position where the first one spawned. I can’t say something like:
-“the x position of bridgeSpawn ≠ Variable(firstBridgeX)”
-“Pick a random bridgeSpawn”
because there are other positions with that same x-position (that obviously have different Y-position) that must be treated as potential spawning places. Similar problem arises when comparing y-positions instead of X. I MUST COMPARE BOTH X AND Y. And I have no idea how to do that here.

There are 18 spawn locations. So 36 values to worry about: 18x and 18y.

How should I do this? Specific instructions are much appreciated!! :slight_smile:

I mean, you can absolutely store multiple variables within a single variable, including sub-arrays and structure variables.

While I don’t have time to go into detail on variable structure, I will say you’re making it more difficult on yourself.

Just do “Pick all bridgeSpawn” as your first condition.
Then as your second condition, do “bridgeSpawn is in collision with Bridge (inverted)” (put all of your bridge objects into an object group named “Bridge”)
Then, as a third condition, do your “Pick a random BridgeSpawn”.

This will eliminate any bridgeSpawn objects that are in collision with an existing bridge, and only pick randomly from the ones that don’t.

  1. Oh, okay cool, I’m pretty sure someone in another post said that it’s not possible? I might be wrong tho.

  2. I can’t believe it, that’s clever, works wonderfully! Thanks!

That was probably me. I’m sorry, I didn’t think to use an [inverted] collision condition. I tend to use object variables over collisions. Silver Streak’s solution is beautiful and perfect for this situation.

1 Like

Just to loop back around on this now that I have more time:

Tuples as a base concept doesn’t exist in Javascript (nor GDevelop).

That said, if you want to store multiple attributes on the same parent variable, you can create child variables (to treat them as a structure) or individual indexes (to do an array). you can also mix and match (you can have arrays of arrays, arrays of structures, structures of structures, structures of array).

You can have a structured variable named bridge1 that has a child of x and a child of y. That’d be bridge1.x and bridge1.y, you could even have an array that is just “bridges” and has index 0-17 (since arrays start at 0), and each of those indexes be a structure with x and y, so that would be bridges[0].x or bridges[0].y.

There are event types that let you do “for each child” or “for each array index”, although keep in mind those require the arrays to be scene variables ,not global or object instace variables.

ALL of that said, I’d recommend not tracking the positions at all, and using the method I provided above which worked for you. :smiley:

1 Like