I call AddWeapon() twice but it gets called only once.
Here is the code
public float movementSpeed = 5000f;
AnimatedSprite playerBodyAnimation;
AnimatedSprite playerLegsAnimation;
string playerLegDirection = "1";
Node2D weapon;
public List<string> weaponList = new List<string>();
public List<int> weaponTotal = new List<int>();
Label inventoryListLabel;
public override void _Ready()
{
weapon = GetNode<Node2D>("Weapon");
playerBodyAnimation = GetNode<AnimatedSprite>("Body");
playerLegsAnimation = GetNode<AnimatedSprite>("Legs");
inventoryListLabel = GetParent().GetChild(0).GetNode<Label>("InventoryLabel");
AddWeapon("Bow");
AddWeapon("Arrow");
updateInventoryLabel();
}
public override void _PhysicsProcess(float delta)
{
float direction = Mathf.Rad2Deg(weapon.GetGlobalRotation()) - 157.5f;
if(direction < 0)
{
direction = 360f + direction;
}
var motion = new Vector2();
motion.x = Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left");
motion.y = Input.GetActionStrength("move_down") - Input.GetActionStrength("move_up");
if(motion.x > 0.2f && motion.y > 0.2f)
{
playerLegDirection = "2";
playerLegsAnimation.FlipH = false;
playerLegsAnimation.Animation = playerLegDirection + "_run";
}
else if(motion.x < -0.2f && motion.y > 0.2f)
{
playerLegDirection = "2";
playerLegsAnimation.FlipH = true;
playerLegsAnimation.Animation = playerLegDirection + "_run";
}
else if(motion.x < -0.2f && motion.y < -0.2f)
{
playerLegDirection = "2";
playerLegsAnimation.FlipH = true;
playerLegsAnimation.Animation = playerLegDirection + "_run";
}
else if(motion.x > 0.2f && motion.y < -0.2f)
{
playerLegDirection = "2";
playerLegsAnimation.FlipH = false;
playerLegsAnimation.Animation = playerLegDirection + "_run";
}
else if(motion.x > 0.2f)
{
playerLegDirection = "3";
playerLegsAnimation.FlipH = false;
playerLegsAnimation.Animation = playerLegDirection + "_run";
}
else if(motion.y > 0.2f)
{
playerLegDirection = "1";
playerLegsAnimation.FlipH = false;
playerLegsAnimation.Animation = playerLegDirection + "_run";
}
else if(motion.x < -0.2f)
{
playerLegDirection = "3";
playerLegsAnimation.FlipH = true;
playerLegsAnimation.Animation = playerLegDirection + "_run";
}
else if(motion.y < -0.2f)
{
playerLegDirection = "1";
playerLegsAnimation.FlipH = false;
playerLegsAnimation.Animation = playerLegDirection + "_run";
}
else
{
playerLegsAnimation.Animation = playerLegDirection + "_idle";
}
if(direction > 315 && direction <= 360)
{
playerBodyAnimation.Animation = "3";
playerBodyAnimation.FlipH = false;
weapon.ZIndex = 1;
}
else if(direction > 0 && direction <= 45)
{
playerBodyAnimation.Animation = "2";
playerBodyAnimation.FlipH = false;
weapon.ZIndex = 1;
}
else if(direction > 45 && direction <= 90)
{
playerBodyAnimation.Animation = "1";
playerBodyAnimation.FlipH = false;
weapon.ZIndex = 1;
}
else if(direction > 90 && direction <= 135)
{
playerBodyAnimation.Animation = "2";
playerBodyAnimation.FlipH = true;
weapon.ZIndex = 1;
}
else if(direction > 135 && direction <= 180)
{
playerBodyAnimation.Animation = "3";
playerBodyAnimation.FlipH = true;
weapon.ZIndex = 1;
}
else if(direction > 180 && direction <= 225)
{
playerBodyAnimation.Animation = "4";
playerBodyAnimation.FlipH = true;
weapon.ZIndex = -1;
}
else if(direction > 225 && direction <= 270)
{
playerBodyAnimation.Animation = "5";
playerBodyAnimation.FlipH = false;
weapon.ZIndex = -1;
}
else if(direction > 270 && direction <= 315)
{
playerBodyAnimation.Animation = "4";
playerBodyAnimation.FlipH = false;
weapon.ZIndex = -1;
}
MoveAndSlide(motion.Normalized() * delta * movementSpeed);
}
public void AddWeapon(string weaponName)
{
for(int w = 0; w >= weaponList.Count; w++)
{
GD.Print(weaponName);
if(weaponList.Count != 0)
{
GD.Print("Test" + " " + weaponList[w]);
if(weaponList[w] == weaponName)
{
weaponTotal[w] += 1;
updateInventoryLabel();
return;
}
else if(w == weaponList.Count - 1)
{
weaponList.Add(weaponName);
weaponTotal.Add(1);
updateInventoryLabel();
return;
}
}
else
{
GD.Print("Test");
weaponList.Add(weaponName);
weaponTotal.Add(1);
updateInventoryLabel();
return;
}
}
}
public void updateInventoryLabel()
{
for(int w = 0; w < weaponList.Count; w++)
{
if(w == 0)
{
inventoryListLabel.Text = weaponList[w].ToString() + " X" + weaponTotal[w].ToString();
}
else
{
inventoryListLabel.Text = "\n" + weaponList[w].ToString() + " X" + weaponTotal[w].ToString();
}
}
}
Here is the Output