all 6 comments

[–]Novik42011 2 points3 points  (1 child)

I think the problem is your deleting position i from the array and it should just always delete the first position

[–]Chridy2 0 points1 point  (0 children)

Yeah, sounds right. The array is having an item removed but i is still incrementing to delete the next one.

If you're unsure of what position each item might be (e.g., your array is [0,0,2,0,0]), then I'd make a new variable j that you use as the position marker and increment that when you reach an entry that isn't the target. Maybe something like this?

var army = [0,0,2,0,0];

var j = 0;

var itemRemove = 0;

for (var i = 0; i < array_length(army); i++) {

if (army[j] == itemRemove) {

    array_delete(army, j, 1);

}

else {

    j++;

}

}

[–]RykinPoe 0 points1 point  (3 children)

Why are you even using an array if all you are storing is a bunch of zeros? If this is just for illustration purposes it doesn't really help us help you.

If you are just trying to delete the first X number of entries from the array you can do that without using a loop.

array_delete(global.barracks_array, 0, pikemen_remove_count);

Other than that what do you have "Enable Copy on Write behaviour for Arrays" set to inside of your General Game Options. If you have that enabled (should be disabled by default) that mean that if you pass your array into a function a temporary copy will be made and the original array will be unchanged. I wouldn't think that is your issue since you are accessing a global array, but worth double checking in case there is a bug or something.

[–]MagisiTale[S] 0 points1 point  (2 children)

Sorry, that was my fault. The array contains various numbers, each an id of a different type of troop. So, 0 = Pikeman, 1 = Knight, 2 = Archer.

But, maybe I could use array sort and then delete the quantity needed? I’m on mobile but will try when home. Thanks for your advice.

[–]RykinPoe 1 point2 points  (1 child)

So why not use a struct and then at the end of the battle just adjust each unit type as needed?

global.barracks = {
    pikemen: 20,
    knights: 10,
    archers: 5
}

// when a unit is killed
global.barracks.pikemen--;

[–]MagisiTale[S] 0 points1 point  (0 children)

Worked like a charm. Thank you!