I'm having a problem that may be simple for you, but is a pretty big deal for me. When I write code, I declare an empty array. When I need to create a weapon, I append it to that array.When I made 3 weapons, when I erase("weapon"), I asked how I could erase exactly the weapon I wanted to erase.Now my array is: array["weapon","weapon","weapon"], even though I know it's all independent. But I can't use remove() method. I just want to remove with just using strings. So can someone please guide me how before mounting I need to mark the weapon so that it can be removed correctly without confusion.Also can you guide me in the same way when using dictionary?Because although each KEY is different, when creating a weapon, when creating it, how do I mark or code to distinguish them from each other?for example the code is: array["weapon"], do I need to mark anything at this step?Please help me! Thank you so much!

  • SnapCracklins replied to this.
  • var weapon = ["axe,", "axe", "axe"]
    var selected_weapon = 0
    
    if Input.is_action_just_pressed("enter"):
        weapon.append("axe")
        selected_weapon = weapon.size() -1
    
    if Input.is_action_just_pressed("End"):
        weapon.erase(selected_weapon)
        selected_weapon -= 1
    
    weapon_in_use = weapon[selected_weapon]

    Odin this is quite easy. You just need to use "has". I don't know how your code looks, but it sounds like you are trying to remove a specific element by name/string?

    So you can run a simple if check and have a variable to store the string, then use erase like you did above. This is assuming your array is called _myWeaponArray and you're looking for, say, a 'hammer'.

    var _item = "hammer"
    if _myWeaponArray.has(item):
         _myWeaponArray.erase(item)

    But the problem is that your array is: array = ["hammer","hammer","hammer"], how do you distinguish which one you really want to delete?Of course, if I want to delete, I code erase("hammer").I mean do you have a way to when I append a weapon, how will I code to be able to mark it to distinguish it? Thank you for replying! ^^

      Odin Use indices. Assuming as per the "hammer" example that you are making an inventory out of an array, each array index would correspond to a slot I'd expect. Remember that indices start counting from 0 so first slot is 0.

      And if for an example you have a crafting system you might want the first slot or index 0 to be reserved for the crafting result. Such that the inventory can never be full and block the player from using the crafting system.

      Alternatively you might want to develop a custom RID/GUID system where the array holds unique identifiers which in turn reference an object/item name(type) and other features such as say a refinement class/type/tier for an example.

      For that you just need to have the int position
      0 is the first position
      ["hammer", "hammer", "hammer"]
      to remove the second hammer
      remove(1)

      Another way is to make the string name more unique, by giving it a number for example
      ["hammer1", "hammer2", "hammer3"]
      String names can be easily changed
      "hammer" + str(3) --> "hammer3"
      Than check if the first part of the name == "hammer"

      var inven = [ ]
      My code is:
      if Input.is_action_pressed("enter"):
      inven.append("axe")
      elif Input.is_action_pressed("End"):
      if inven.has("axe"):
      inven.erase("axe")
      Therefore, if in the game I pick up a lot of AXE , and then I press the END key to delete it, how do I delete the exact AXE that I clicked in the player's inventory?
      For example, you press the ENTER key 4 times, then in the array you are inven["axe","axe","axe","axe"].
      Can you guys add any lines of code to this code of mine to make it work smoothly?
      Sorry, I'm bad at coding. I look forward to your kind guidance.Thank you so much!

      If those are all the same (i.e if the player has four duplicate axes in their inventory), then just choose one, delete that one, and move on. On the other hand, if the elements of the array are different, then the find() method may prove useful. Be careful when using this method on big arrays, as it could take a while to find a match. There's also the bsearch() method, but that's more involved.

      var weapon = ["axe,", "axe", "axe"]
      var selected_weapon = 0
      
      if Input.is_action_just_pressed("enter"):
          weapon.append("axe")
          selected_weapon = weapon.size() -1
      
      if Input.is_action_just_pressed("End"):
          weapon.erase(selected_weapon)
          selected_weapon -= 1
      
      weapon_in_use = weapon[selected_weapon]

      Because while the elements in the array are the same,but the STATS of those elements can be different. So I need a way to distinguish and delete exactly the element I want to remove. I find Master SuperDoomKing's way to be quite reasonable. I will try it. Once again, I sincerely thank the brothers who have enthusiastically helped me.