Character Selection Problem

I want to create some relatively simple character selection code: when I click on a character, it gets added to the list of characters that will appear in the game. However, nothing happens when I click—it doesn’t get added to the list, play a sound, or change size; absolutely nothing happens. I just need a fix here: I want the character to get bigger when it’s in the list and return to normal size when it isn’t. I even added a sound to give me a signal, but that didn’t work either.

Usually, a mouse event is also used. Either mouse released or pressed with a trigger once.

Also, it’s checking if the child exists before the cursor is on condition. So, it’s going to use the object variable of the oldest instance.

Since, there’s no trigger once, it’s probably adding and removing the child on every other frame. Or maybe it just removes them all and then adds one back. IDK. I’d have to test it

Either way, the cursor condition needs to be checked first, then the child exists, then a button condition with a trigger once depending on which mouse event is used.

Actually, I’d check the mouse released first, then cursor on object, then child exists.

hello! this should fix all your problems.
these few lines of code deals with all your characters selection at once.
and the visual does work.

i prefer going for object variable instead of scene/global arrays and structures. Unless you have the array tool extension… these are best avoided when possible.

putting the scale events as sub-events will boost performances as well, since they are only checked while the “left button released condition” is checked true.

to be clear… you can use a structure/array if you want. (ie. trying to use the structure as a list instead of like “bucket” containing pieces of info)

its just complicated to dynamically add a child variable to a structure and then call it later. structures are not meant to be a dynamic list. arrays are. but arrays are numbered, not named. you can easily fetch info from a structure’s child, but its hard to get the child name associated with a value.

structures (including their children) are generally predefined from the start. which you could do (manually name your 4 child of the structure “character” the name of the character. then store the info of their state in them. ie, selected/not selected). however, this ends up being the same as just an object variable.

I agree with the abject variable approach or even a structure where the value is toggle but I wanted to test the original concept.

Edit: I just realized you were using an array with remove child. I thought you were using a structure. Remove Child is for structures as is child exists although the Array Tools extension has a search function. The remove by index is for arrays. You would need to know which position the character in the array. My version uses a structure.

The thing to remember with the else event is that it looks at the entire event. So, in this case if you had mouse, cursor and child exists, the event would be false it any of the conditions were false. You could use a trigger once but still, the 1st time it was evaluated, it would trigger the else is the conditions weren’t met.

So, how do you fix it? You could split the conditions so the else event only evaluated the child exists condition. It basically isolated the conditions. So, the 1st two conditions need to be true before the child is looked for.

I used 3 buttons in a group named Buttons and a text object to show the structure.

(ChildName should be declared as text but it still works because of the way the for each child works.)

Last edit.

So, after realizing that you were using an array not a structure. I wanted to test it using an array and the array tools’ search condition and expression. It didn’t require that much of a change.

what the heck is this event? where have you found it? is it in array tools extension? i thought that array tools was for arrays

That’s a built-in event. It’s in the same menu as the for each object. It goes through either an array or a structure.

https://wiki.gdevelop.io/gdevelop5/events/foreach-child-variable/

It works similar to for each object but for arrays or structures. It uses 2 optional variables to either return the value or index or structure name depending on which variable field is used and if it’s an array or structure.

It’s very flexible.