Replace a random object inside a group for another object

Hi everyone,

I’m working in an experimental project and I would like some help.

I have a scene with several objects and all of them belong inside a group.
How do I pick a random object (within those that belong to the same group) and replace it with a specific object?

The goal is to: when I catch 5 coins, pick a random character in the scene and replace it with another character not in the scene yet.

Every time I replay the level I want the game to choose a different random character.

Any help is appreciated!
Thanks

Welcome. This is one way to do it. I would use one object with all of the animations. If you want to use multiple objects then this wouldn’t require too many changes.

try me: click the numbers to randomly choose an used number.

This uses 2 objects, 1 text and 1 sprite with 5 animations

scene variables
image

Source: (click the green [code] button and select download)

Edit: I forgot you wanted a random object picked. That would be simple just replace the cursor is on with the condition sprite name pick a random instance Also add you coins quantity check with a trigger once.

Thank you very much for your help and time.

Unfortunately I can’t quite understand how to adapt your solution to my problem.

I will try to explain better and hopefully you or other user will be able to help me.

What I have is:

  • Multiple objects in the scene (each with its own animations, imagine a character in idle for example).
  • Some of these objects can be duplicates of the same object.
  • The number of objects in the scene can vary.

What I want is:

  • After a specific condition, pick a random object in the scene (within the specific group of objects mentioned above, as for example an enemy character) and replace that object with a new one.
  • The new object must be placed in the same position of the old one.
  • The new object will also have its own actions and animations.

I am new to GDevelop so I apologize of any misunderstanding from my end.

Thanks

Edit: I don’t understand the need to swap the objects. How are you adding objects? And I know you said when coins are collected, objects get swapped, which objects and under what premise? Do you just 1 random or several. Are they player objects or background AI characters or objects?

OK. The technique for swapping an object would be similar. Under [other actions] [object] is [create an object from its name] . You use the group name, an object name from the array and the x, y would be the current objects x, y. So, it’s among group name create object object name at x, y

You can use this to spawn random objects or swap existing.

Im not at my pc but later I could throw together an example if needed.

Create an array with all of the object names in the group. Or name the objects something like Obj1, Obj2, Obj3

At the start and when the coins are collected pick a random object, add a new object at it’s position and then delete the old object.

Remove the new object from the array and if you want the old one used again then read it to the array. Or just add the new object and delete the old if you go by object names without the array.

https://wiki.gdevelop.io/gdevelop5/all-features/object/reference/#actions

If there are multiple objects of each object type then you wouldn’t need to remove or add the object name to the array.

You could either use the name from the array or name the objects all the same with a number after them like Obj1, Obj2, Obj3 then add an object using the name “Obj” + ToString(RandomInRange (1,number of objects in group - 1)

Whether you put the names in array or name them all the same with a sequential number depends on what they represent and how you interact with them. An object named fox would be easier to write events fir than Obj1 unless you put all of the objects in a group and write actions based in the group name. Then it wouldn’t matter what the object names were.

It also depends on how much randomness you want. Do you want true randomness which might lead to a lot of one object at tunes or a but of controlled randomness where there’s a bit of balance.

Thank you again for your help.

What I want to do is:

  • I have several “DinoLena” characters that are in idle.
  • “DinoLena” is the name of the group and inside this group there is “DinoLena1”, “DinoLena2” and “DinoLena3”.
  • In the scene there are several characters from each “DinoLena1”, “DinoLena2” and “DinoLena3”.
  • When the score is 5, I want to pick a random character in scene (that belongs to the group DinoLena) and swap it with another character named DinoMort.
  • The player would then be able to interact with DinoMort.

The problem:

  • I’m trying to pick a random character in the scene but it always selects all characters from the group DinoLena and then swap all of them with DinoMort.

This is the code I’m trying to use:

When I use “Pick a random object”, shouldn’t all actions only affect the chosen random object?

Hiya, well what you’re saying is if the variable Egg1 is 5 to pick a random DinoLena, create a DinoMort where is it, and Delete the DinoLena. But you didn’t tell it how many times. So as far as it knows, the Egg1 is still 5, so it needs to pick a new random DinoLena and make a DinoMort there. And again. So for a test, right under where you say “xDelete DinoLena” you can add an action that says change scene variable Egg1 set to 0. Or don’t do that, but just add a trigger once under “Pick a random DinoLena” . Run your test to see what happens.

This won’t help with the spawning of a random DinoMort though, since you’re flat out telling it to create a specific DinoMort.

Thank you all for your help.

The solution is so simple when you know the tools.

It worked!