0
\$\begingroup\$

I'm creating an inventory system for a dungeon crawler, I've got world gen, and player controls down, and this is the only snag.`

public Transform[] items;

public bool[] usingitem;

public int usingitemnumber;

public void Update()
{
    if (Input.GetKeyDown("1"))
    {
        usingitemnumber = 1;
    }

    if (Input.GetKeyDown("2"))
    {
        usingitemnumber = 2;
    }


    if (Input.GetKeyDown("3"))
    {
        usingitemnumber = 3;
    }

    if (Input.GetKeyDown("4"))
    {
        usingitemnumber = 4;
    }

    usingitem[usingitemnumber] = true;
    if (usingitem.Rank != usingitemnumber)
    {
        usingitem[!= usingitemnumber] = false;
    }




}

`

i need to check if a rank on the transform[] usingitem array is not equal to the int usingitemnumber , and if it's not equal to it set it to false, and keep the on that is equal to it true

\$\endgroup\$
1
\$\begingroup\$

If I understand you correctly:

  • usingitem is an array of bools
  • usingitemnumber is the index of the item that should be active / in use
  • You want the ith entry of usingitem to be true if and only if i is equal to usingitemnumber (ie. this array item corresponds to the currently used item)
  • All other entries in the usingitem array should be false

We can do this very simply with a for loop. This steps over each entry in the array in order, from index i = 0 to index i = usingitem.Length - 1, and sets it to true if the index i matches usingitemnumber, otherwise it assigns the array entry to false.

for(int i = 0; i < usingitem.Length; i++)
   usingitem[i] = (i == usingitemnumber);
\$\endgroup\$
  • \$\begingroup\$ thank you this is helpful, but I'm still new to how c# works how would this set the unused bools to false?,and how would i do that? \$\endgroup\$ – Indiepixel Jan 9 at 3:04
  • 2
    \$\begingroup\$ i == usingitemnumber is an expression using the equality comparison operator ==. Before assigning a result to the array, the program evaluates "is the current index i equal to usingitemnumber?" If so, the expression evaluates to true and we store true in that array slot. Otherwise, the expression evaluates to false and we store false in that array slot. But in general, for basic language syntax like this, GameDev.StackExchange is not the best place to ask. Instead, work your way through beginner tutorials and guides for your language until you have a sound grasp of the basics. \$\endgroup\$ – DMGregory Jan 9 at 5:14
  • \$\begingroup\$ ok, thank you for the help, i will continue with tutorials for now. \$\endgroup\$ – Indiepixel Jan 9 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.