Load at the end of the game which objects collided with the player

Hey guys, I need your help! :slight_smile:

I am looking for a way to save the objects that collided with the player during a game (in my game there is a group of 10 objects and i want to save which of them collided with the player) and when the game is over I want to load or present in the ‘game over scene’ which objects collided with the player (as a evaluation for the person who played the game). The form of presentation would be a text where the objects are listed. Do you know how to do that?

If you know please give a detailed solution or better send a screenshot of the gdevelop conditions and actions. Thanks a lot already!

I’m assuming there’s only 1 object of each type. If not then my idea won’t work as is.

I would put all of the objects into a group. When the player collides with the group, you could add the ObjectName() to an array and then at the end display them in a text object using a for each child event. If it’s all 1 scene then use a scene variable otherwise use a global. If the player can collide with multiple objects at a time you would need a for each object after the collision to add the ObjectNames() individually.



Hi, thanks again for responding. I am sorry, but I am very new at this and I have to create the game for my studies, so I am very thankful for your explanations. I get the idea and I already read the examples on gdevelop, but I don´t know exactly how to do that.
I tried to add the ObjectName() to an array but if I use the “for each child” in the next scene it´s only for “scene variables” and not for “global variables” … I am not really sure in general, what to do exactly… Maybe you can tell me, what I did wrong?

You’re very close. Here’s how I would the first part.

ObjectName() is an expression or object property. (it also works for groups) If it’s in quotes it’s going to add the text to the array. You need the expression. You can either start typing “ObjectName()” and choose it from the list or use the expression builder. (the purple button on the right)

Now, I arranged the code slightly differently. Conditions like collision work like filters. It’s more efficient to filter or pick the object(s) that are in collision and then just cycle through them with the for each object I left the sound and shake outside of the loop because I didn’t think it needed to shake or play a sound for each sprite. (should there be more than one collision at a time)

This is where the ObjectName() is.

Part 2. This is how I would do it.

It’s been suggested in the past that we need a for each for global variables. A current workaround would be to convert the global variable from part 1 to a scene variable using Json. Json is just a way to format data. You convert the global variable to a string and then the string to a scene variable.

The first part:

The top “Json string” part: (the bottom part is just the scene variable name)

Edit: Note: the variable child is part of the for each child event. It stores the child value as it cycles through the array. It defaults to the scene variable “child” but it can be changed.

Thank you again for all the effort you put into your answer… I did as you explained and it worked so far… the text displays all the names of the objects, but the only problem is, that the text never stopps… after the objects are named, they are named again and again and aigan like this: object1object2object3object1object2object3… Do you know how to display the names of the objects only once and then make the text stop? (I tried with trigger once but that did not work or I did it the wrong way)
I thank you, you helped me a lot! Sorry to disturb you again… :slight_smile:

Are there multiple copies of the same monster? I noticed you don’t delete them but are there multiples?

What if, when you add a monster to the list you set a boolean object variable for that object to true and add a condition to the same subevent that checks if the boolean is false before adding it to the array. I’m not at my pc. I can’t currently imagine the events to use if there are multiple copies. It would be easy to set the boolean of the one in collision but if there are multiple copies, I’m not sure how to do it. I know it would involve the pick all but IDK at the moment how to pick all of one object based on a collision with an object group. It’s never come up to me until now.

My other idea is easier. It would be to use a structure instead of an array. I believe the 2nd part would still work but might need some tweaking. You would use the child’s name instead of the child value. You can use a structure of any type; I would use a Boolean. You could set the value as set VariableName[groupName.ObjectName()] to true. That way if there are multiple collisions, it would just set the same variable instead of creating a new one. If needed you could test if the variable was false before setting it to true along with any other actions. Say if you didn’t want it to shake each time. You would still need the initial trigger once so each collision only triggers once. In my test, I deleted the object. I wasn’t sure on how your process. If you did that you would need the value. For each child, if child is true, add childname to the text object.

I can’t be confident of my ideas without testing them on my PC. I’ll be able to try them and be more confident in a couple of hours.

OK, this is how I would do it: Make sure you add childName to the 2nd box in the for each event

Thank you, you´re a genius… it worked! I am really thankful and I appreciate your help very much! :slightly_smiling_face: Thank you for all the time and effort you put into this…

1 Like

Just one question, sorry :smiley:
Can I change the object name to only a number, now they are named like “n1” or “n2”… I want only the numbers (but objects can´t be named after numbers)… is it possible to modify the text and substract the “n” from the text?

You’re welcome.

The StrAt(string, number) expression will get the character at a certain position. The first position is 0 not 1. It can be used anywhere text in quotes or a string variable is used.

StrAt(“a1”,1) (returns the 1)

You can type it or use expression builder. It’s under “text manipulation” as “get a character from a text”

A good reference page

1 Like

Thanks again very much!!! Sorry that I did not answer and thanked you sooner… so I tried it as you can see in the screenshot, but now the text does not display anything (before it displayed the object name), do you know what I did wrong? Like I said, I apprecciate your help with my project, would not work without you!

You’re welcome. I’m not quite sure what you’re trying to do. It doesn’t look like my example. I think the condition should be if boolean of child = true and the first change text should use childName instead of child.

I’m not sure where the n1, n3, n5, n7 came from or why there inside the for each the first text action is set to add but the other’s use set. So, even if they were triggered they’d replace the previous text.

hey :slight_smile: i tried how you explained it: StrAt(“a1”,1) (returns the 1) and that worked so far. Thank you very much and sorry for not answering back!!!
But I have now one new problem and a question (sorry :confused: for my many questions):
I have also object names that are named in example: “a12” and i tried to solve that with: StrAt(“a12”,1 + 2) (I wanted 12 in return, so that two characters are returned) but it only returned the first number (that means 1)… Do you know how to solve that?

You can use the SubStr() expression.
SubStr(string, start position, length)
(the 1st position is zero)

Example: this would return everything except for the 1st character.

SubStr(VariableString(a), 1, StrLength(VariableString(a)-1)

If the length value is larger than the text, it will work the same. I perfer to do the math.