So X number of npcs get added to the scene when the game opens, and a random number rolls to determine which npc gets to be the "VIP", the "Target" or whatever you may call it.
Being the "VIP" means being inside an array, and if you kill it, you win, earn money and whatever.
The npcs are multiple Kinematicbody2Ds that are identical but their sprite and if they're VIP or not get to be random, that's it.
But I want only one of them to be the "VIP" npc, and if more than one get to be in the array, I want to remove as many elements as necessary, leaving only 1 element inside.
I've tried this, but it doesn't work as I want it to:
#Global.npcTarget is one VIP npc
#Global.numberofTargets counts how many VIPs got added when game started
if Global.npcTarget.size() > 1:
for i in range(Global.npcTarget.size() - 1, -1, -1):
Global.npcTarget.remove(i)
Global.numberofTargets -= 1
print("Removed an element, now array has: ", Global.npcTarget)
print("Num of Targets: ", Global.numberofTargets)
That code is in a _process function, and this code is the one that adds the VIPs:
func _ready():
randomize()
var randNumber = int(rand_range(1,5))
$Sprite.frame = int(rand_range(1,5))
print(randNumber)
if randNumber == 3:
istheTarget = true
Global.npcTarget.append(self)
Global.targetFrame = $Sprite.frame
Global.numberofTargets += 1
print(self, " is Target!")
else:
print(self, " is not target...")
it's very messy, I just don't know how to code something like this properly!
This ends up with the game having multiple VIPs (Targets) sometimes, and sometimes with ZERO VIPs,
and also if there are multiple VIPs and one of them gets erased from the array, the variable "istheTarget" will remain TRUE, which will allow me to kill that npc and it will count as a VIP, I don't want that either...
I'm also doing this to match a Sprite portrait frame with the corresponding sprite frame of the VIP npc.
Please, help me! I suck at arrays </3
Also some pictures of my code, the last one being a global singleton:
This one adds the VIP, and the code is inside the NPCs' script:

This one removes VIPs until there's only one of them, although sometimes it will remove ALL of them... and the code is inside the Level itself's script:

This is the global singleton:

The result of all this being sometimes this:
