How to remove child from array?

I have a system for the party of henchmen that are supposed to follow the player, which I tied into my turn system, and it all works perfectly fine, but there’s a problem, regrettably.

My system works like this: Every character (all one object) has an ID. If that ID is equal to the value of an array that has an equal index to the variable time.turn, then it’s that character’s turn. For the character’s ID to be in the array, it has to be added either through the editor, or through the event sheet. To add a character via the event sheet, you switch the character’s string “role” to “henchman” or to “player,” (the only difference is that one is planned to have the ability to disobey the player via the unimplicated morale and loyalty systems) and the character’s ID must be added to the array as text.

But I can’t remove characters via the event sheet yet. I mean, I know how to remove the player (party[0]), or the last party member in the array, but I need to know how to remove the character from the array specifically, based on the value of the array’s child. If the value of any child of array is equal to “bob,” say, then remove that child from the array. Is there a way to do this?

My knowledge of arrays is very like let’s say none

BUT to my understanding you are adding to array some vars
Why not pass that index number to some variable of object you are reffering

Like you have pirate and captain
And they can be added in any order to array
It can be
MyArray.[0] = Pirate
MyArray.[1] = Captain
Or
MyArray.[0] = Captain
MyArray.[1] = Pirate

Why not in moment of adding them to array give them value of ArrayChildCount(MyArray)
Which would be child index which is assigned to them
For example give them var called ArrayIndex
And then you can do
When you target such object in your events for example you added them to ShipCrew object group
ShipCrew is clicked

Remove from MyArray child at index ShipCrew.ArrayIndex

Other than that maybe array tools could help but IDK how to use it
So maybe wait for someone with better idea or knowledge

I checked array tools; it didn’t help. But your idea sounds like it could work; I’ll try it.

Hi,

Based on your request, here’s my solution. If you want to remove a specific item from an array and replace it with a new value, you need a temporary variable to store the index of the item you want to modify.

For example, given the following array:

Array = ["Bob", "Mike", "Jan", "Nik"];

We want to replace “Mike” with “Kia”. First, we find the index of “Mike”, which is Array[1].

Next, we use the stored index to replace “Mike” with “Kia” using the splice method

Splice an array
Cuts a portion of an array off.

Then,

Insert variable at
Insert a variable at a specific index of an array.

Now, the updated array looks like this:

Array = ["Bob", "Kia", "Jan", "Nik"];

Array tools - GDevelop documentation

I did this. Bob is set up correctly, or else I could not have gotten as far as I did with the testing, but there is a certain bug.
Here’s how I set it up:


At the start, bob works perfectly fine. I assigned his index to him in the editor, rather than with the events. Then I removed Bob. His character became an image of the letter/character “O,” meaning he was an npc. Then I added him back on. The character whose turn it is has a bright green outline. The player takes a turn, Bob gets the outline, Bob takes a turn … nobody has the outline. The turns have ceased. The game is broken. This happens whether I set the index to the number of children in party or the number of children +1 upon adding Bob back on.

What’s going on?

OH! There’s an extra child in party, with an index of 2! I’ll check that out! Since no character ID matches the child, I guess, no character can act in that turn.

Oddly enough, that extra child contains the value, “0”. That’s all. I used a text object to figure this out. I don’t know how it is created yet, however.

It isn’t a result of my adding 1 to the index when setting up the index variable in adding him.

It has to be created within the event that readds Bob to the party, because it’s upon the execution of that event that the text object displaying the number of children in the array changes from 1 to 3.

Otherwise, I can’t think of anything else to check.

There’s a remove last text child action somewhere, but I can’t find it again. That may come in handy.

Ah, well; array tools actually does help to manipulate a child of an array based on its value with the “index of a text” parameter.


It works perfectly well. Woohoo!

1 Like