Create two random objects from a group without being duplicated


I tried to extract the text and it works correctly while inserted as a sub-event of the cash animation it returns 0

I don’t know your setup but the 2nd half (in your original event sheet) can only trigger when the animation has finished playing for an object. If the animation hasn’t been completed then nothing gets created but the first half continues to choose numbers and removes items from the array until it’s empty. By the time animation finishes, I’m assuming the array is empty.

A new random text only needs to be generated when the animation finishes. Maybe move the 1st half inside the 2nd half. There’s nothing to check if the array is empty. I’m not sure what you want to happen when the array is empty. It needs a condition to check how many children exist. Then either stop creating objects or start the shuffle over again.

Yes, I have made several attempts and I believe that all the children are deleted until the array is completely emptied before creating the text.
what I would need to do is simply a way to be able to create a non-repeating random drop of ammo when the chest is opened.
I had used this method for creating powerup objects at the end of the level and I thought I could reuse this method with the creation of text but it is becoming more difficult than expected.
do you have an alternative solution?

Here’s a method to regenerate a list of text. It uses 2 arrays. One has the text. The other array is a list of corresponding numbers. (0-3) It picks a number from the list of numbers and uses that number to pick the text from the other array. If the array is empty, it automatically refills it. It uses a button and a text object.

Note: it uses the newer syntax where if variables are setup, it doesn’t need the Variable() and VariableString() part

try me: click the button to add 2 text objects

.

project: (Click the green [code] button and [download zip] open the Json file using Gdevelop

1 Like

Thank you @Keith_1357 you were a great help!
one last question… can you tell me why I am not being taken into consideration for each instance “cassearermi”?
the text is created for me only on the first open case, nothing is created for the subsequent ones

The object is being created at x, y but x, y are only set when the array of numbers get created.

Moving the set x and Ă˝ outside of the repeat might fix things or you could remove the x, y and set the values directly inside the create object.

Edit: I do see the line reducing y but I’m not sure if that’s needed if you want each to be a certain distance from the object in the for each.

Edit 2: It might be the trigger once. Trigger once works on an object level not instance. Try removing it. It’s best to use trigger once outside of a loop or use a boolean object variable or timer instead.

Sorry for all the edits. Im on my phone. In your case, when the actions are triggered I might change the animation so it’s no longer the one that just finished. That would work as a trigger once of sorts.

1 Like

Instead of using a repeat event with conditions, create an event with the condition and add the repeat as a subevent (without it’s conditions):

There’s a twofold benefit there:

  1. It’s a much more efficient way of iterating over relevant objects.
  2. The trigger works as expected (in the repeat event a “trigger once” doesn’t function as you think it should, though logically it’s correct)

And I agree with @Keith_1357’s last comment of changing the animation once it’s completed. That would save a lot of hassle.

2 Likes

yes @Keith_1357 the line that reduces y I need it because I want there to be a gap between the two texts.
if this is what you meant it seems to work, now all the open cases are taken into consideration and the text is created.
the only thing that doesn’t convince me is the creation of the text in the center of the case being created and then changed the text based on the child of the chosen array many times it is created laterally.

Just be aware if multiple animations finish then multiple objects get picked and then the x and ý are going to be in relationship to I believe the 1st object or instance that was created. (it could be the last but I believe it’s the first) Regardless, it’s only going to be one of the objects. It should work fine if there’s only 1 object but not multiple.

I think the x, y actions need to be inside the for each object but not inside the repeat number of times like it was earlier. That way, it will use each object’s position as reference.

1 Like

you’re right keith… thank you all for your help!

1 Like

I humbly ask for forgiveness, just a piece of advice… do you think it is the best solution to add the variable of ammunition found

Whether you show the number is completely up to you I have no opinion on that.

I’d like to suggest that instead of using separate events for your items that you create an array of structures instead of an array of text(strings). You could then use 1 event to handle an unlimited number of items. It’s up to you. IDK how familiar you are with arrays or structures.

This just picks a random item and shows the name and the random quantity in text objects. It uses the items minimum and maximum children for the range.

Once you create the first structure then you can just copy and paste it to add more items. The numbers are reversed because Gdevelop sorts child by name. Everything would be the same, you just need to set up the array and then add the child’s name to the variable name.
Instead of item[#] it’s iten[#].name or .minimum or .maximum
You only need the period before the child name.

I put the range in the names just for testing. You can add any number of children as needed.

It’s all up to you. The code would be easier to read and maintain with just one event.

1 Like




thanks for the example shared, now everything is a little clearer to me but I was trying to implement the creation of the double text but sometimes the same name is created twice.
I’m most likely doing something wrong

I don’t think it’s using the 2nd array like it was before. It looks like it’s using the random number instead of the number in the random number list. It’s getting a little tougher to keep track of things but it will eventually get together.

previous

current

I think it needs to be:
set randomindex to RandomNumberList[Random(VariableChildCount(RandomNumberList)-1)]

Edit: that wasn’t right. I fixed it. It’s getting quite long. You might want to use 2 variables. It’s up to you. Sometimes, I’ll split things into multiple steps instead of 1 super long line. You have a random number and then the number from the randomNumberList in that position.

1 Like

thanks again @Keith_1357
it works now!

1 Like

sorry again if I come back to this post again… once the text has been created and the quantity found, how can I add that value to the ammunition variable?
example:
if the player opens the chest and finds arrows+5 and grenades+2
how can I add those values in “ammoarrows” and “ammogranate” variables

What are you adding to what. Variables can be added to variables.

Set variableName + variableName

If you want to enforce a maximum quantity you could use

Say the maxAmmo was 30
Set ammo = min(maxAmmo,ammo + foundAmmo)

Or a sub event after adding new Ammo to ammo
Ammo > maxAmmo set ammo = maxAmmo

Using a variable like arraowsMax would allow you to change the value later and allow the player to carry more arrows.

I had already previously created variables for each type of weapon called:
ammoarrows
amorevolver
ammominigun
etc…
I would like to add the value generated by the drop of the ammunition boxes to these variables but I don’t understand how to attribute it

I thought of something like that but it doesn’t work

Instead of [set] use add.

I’m not sure if you read this or watched the intro video.

https://wiki.gdevelop.io/gdevelop5/all-features/variables/

Yes, I saw the video and I’m trying to learn, English is not my first language and I find it a little difficult to understand some passages.
I don’t understand how to identify the value generated for each child in the condition and then add it to the variable in the action

1 Like