0 votes
by (350 points)
edited by

Hello everybody!

I created a vector to store the weapons that the character encounters on their way during my game. I used the vector in a code to check if the weapon he wants to pick up already exists or not within some vector position. If the weapon already exists, the game should display a message "You already have the X-Weapon". Otherwise, the code must find the first empty space (with the string "empty") inside the code and store the new weapon inside this position. However, when the code checks, even if the weapon is already stored in the vector, it again stores the same weapon in an "empty" position. If the player does not already have the weapon, the code places this weapon in the first "empty" space and includes the weapon in the player's weapon lists in the left menu (weapon_equiped).

Anyone know how can I fix this problem?

Here is my code:

<<for $count=0; $count <= $weapons_vet.length; $count++>>
    <<if $weapons_vet[$count] is "empty">>
        <<set $weapons_vet[$count] to "shotgun"; $weapons_damage[$cont] to 8; $weapon_equiped to $weapons_vet[$count]; $weapon_equipeddamage to $weapons_damage[$count]; $cont to $weapons_vet.length + 1>>
    <<elseif $weapons_vet[$count] is "shotgun">>
        <<print "You already have a shotgun.">>
    <<else>>
        <<continue>>
    <<endif>>
<<endfor>>


Yes. I have previously declared all positions of the vector as "empty".

1 Answer

+2 votes
by (23.6k points)
selected by
 
Best answer

You are confusing how <<continue>> works. You have to use <<break>> to stop the for loop from running:

<<for $count=0; $count <= $weapons_vet.length; $count++>>
    <<if $weapons_vet[$count] is "empty">>
        <<set $weapons_vet[$count] to "shotgun"; $weapons_damage[$cont] to 8; $weapon_equiped to $weapons_vet[$count]; $weapon_equipeddamage to $weapons_damage[$count]; $cont to $weapons_vet.length + 1>>
        <<break>>
    <<elseif $weapons_vet[$count] is "shotgun">>
        <<print "You already have a shotgun.">>
        <<break>>
    <<endif>>
<<endfor>>

That being said - I don't know what you want to accomplish exactly, but this seems overly complicated. You can probably get the same result by using an empty array and a widget like this:

<<widget addWeapon>><<nobr>>

<<if $weapons.includes($args[0])>>
	You already have a $args[0].
<<else>>
	<<set $weapons.push($args[0])>>
<</if>>

<</nobr>><</widget>>

Then add weapons like this:

<<set $weapons to ["Gun", "Bomb", "Pointy Stick"]>>
<<addWeapon "Gun">>
<<addWeapon "Bear">>
$weapons

 

by (350 points)
The <<break>> works! Thanx!!!
...