How do I spread a unique value across all instances of objects?

Hello,

I’m not sure if this is a topic that’s already been covered and if so my apologies. In my game I’m wanting to use ball sprite objects that each ball has their own sensor detection type of sprite applied to them. How can I get each ball and sensor to “talk” to one another or ID with each other? I’m not sure if I’m using the link two objects method incorrectly or if it’s even possible to give each object a unique variable or ID to them. As someone who’s coming from Clickteam Fusion I don’t know if I’m viewing the process wrong overall? In that program they have something called Spread A Value and when you start the game each object will have a sequential number added to them that you can use for loops later on.

Thanks In Advance! :grinning:

You can use object variable, double click your object in the object panel then go to variable tab.
You can enter the variable value and all object will have this value, you can also modify it one by one by changing the value in object properties panel.

At the beginning of the scene, I’ll use a for each object event. It’s under the larger plus sign in the top toolbar. It’s also good for creating additional objects, arranging them and tweaking other settings.

There are times when IDs are the best approach but Gdevelop has conditions and techniques which are at times easier to use and don’t require any setup or maintenance.

3 Likes

Sorry for the slow reply and that setup makes sense. But, how can I create a loop to check that? When I choose the Repeat Event it just gives me a blank line. Can I reference the value of the Scene Variable so it knows how many times to loop without me putting my own number in there? Thanks for your time and any clarification :grinning:

It’s not a repeat it’s a for each object

You can use expressions, formulas and variables in the repeat event. I have no idea why the auto complete syntax thingy doesn’t appear for it. When I’m unsure about the syntax, I’ll create the expression elsewhere and then copy and paste the text not the event line.

I got it now and it works!! Thank you so much my friend! I will add snapshots below to demonstrate that it works and the code in case other people want to try this. This is a brilliant workaround:

Really appreciate it! :innocent:

1 Like

You’re welcome. Just a tip. & and isn’t necessary in this situation. By default all conditions must be true.

FYI, for you and others. & and is only really needed within an or

a is true or a is false and b<8.

Just a note. I’m not sure what you’re hoping to achieve with the event below. It only moves the ballsensor for the latest created ball and sensor. It’ll do this IDGen times:

So for example, if IDGen is 6, it’ll move ballsensor with id 6 to ball with id 6, and it’ll do this 6 times.

1 Like

Good catch. When Consolemu said it worked, I asumed they were using the for each object.

The repeat would work but would require more work. You’d have use another variable for the ID check, increment it and make sure to reset to zero before the repeat.

A bigger issue would be if objects were deleted. Unless you renumber the IDs each time, it would waste time checking for indexes which no longer exist while a for each object would just iterate the existing obects.

1 Like

Oh thanks I didn’t know that. I’m still getting used to the quirks of this engine.

1 Like

We’re all still learning. If the sensor always needs to be connected to the ball, then an alternative to most of the code would be to use the sticker behavior. It has 1 purpose to stick one object to another.

Add the sticker behavior to the sensor object and then after the 2 objects are added and arranged just stick the sensor to the ball. You then just move the ball.

1 Like

Okay cool I’ll give that Stick extension a try. To answer your question from earlier in the thread I’ll try to explain it better of what I need.

So say for example I’m doing a tower defense game. Every time a new tower is created, I also want a sensor to be created and assigned to that specific tower. That way if an enemy collides with the sensor, it will notify that tower that it’s assigned with to shoot. The way I accomplished this in the past with other game engines was by using object ID’s and Loops. But by all means if there’s a much simpler or effective solution I’m all ears and thanks again.

Ultimately, I wish GDevelop had a way to have multiple collision masks for different purposes. I only know of the collision mask but it would be great if if there was say an extra sensor mask for each object. Hope this helps clarify and again I appreciate you all taking the time to help! :slightly_smiling_face:

1 Like

You can still use IDs or after you add the objects you can stick them together physically but also create a link and then use among objects to pick the linked object(s). I don’t mean to overwhelm you. I don’t know how familiar you are with Gdevelop.

Linked Objects [GDevelop wiki]

EDIT: The link actions are under other actions and not an objects.

Using your theme. Here’s an example.

Click to add objects.
When cursor is on ballSensor it checks for the balls that are linked to the ballSensor and “picks” those objects. Then any action will apply to those ball objects just like any other condition. The order of the among is important because it decides which object gets “picked”.

Source:
doug13579/linked-and-sticker-objects-example (github.com)

1 Like

So the secret sauce is in the linked objects I see! When I tried it your way it worked exactly the way I needed. Funny thing was as I was putting the events in I was worried if later on down the line when I make the sensors detect will it make just the one ball shoot or make all of them shoot simultaneously? When i added a collision event then only one ball would shoot. But by adding the same take into account linked objects the same way then it knew to only shoot from the specific linked object. Can’t thank you enough!

Now that I know this is a reliable method, it opens the door for so many other game possibilities out there. I could use it for a Tower Defense, I could use it for the enemies in beat em up, and I’d imagine it would work great in a one-on-one fighting game too. Overall it seems like the more I use/practice with GDevelop the more I look back at other engines in comparison and almost don’t want to go back lol. At this point it’s feeling like the 2D king. :grin:

Oh and forgot to mention thanks so much for uploading the game example. I really appreciate it! :innocent:

1 Like

You’re welcome. Linking comes in handy. I still use index sometimes but it’s less work to link. It gets more complicated with linking multiple objects to other multiple objects but also more powerful. For now, just experiment with 2 objects. Just remember the take into account order matters and you can’t link instances of the same object to each other. You can link as many balls to sensors that you want but you can’t link balls to balls or sensors to sensors.