#make variable on method

1 messages · Page 1 of 1 (latest)

ruby brook
#

I want to put a variable inside method without specifying its type
Is this possible? And in what way

   void test(var somting)
    {

    }
meager oyster
#

What exactly are you trying to do here

#

A parameter?

#

Or a local variable?

#

without specifying its type
Because parameters can't have type inference, that makes no sense

thick latch
#

They probably want generic type parameters

#

idk

ruby brook
# meager oyster What exactly are you trying to do here

I have a code that consists of for loop. This code is repeated several times, but with variables of a different type
for example

 int find(GameObject test)
    {
        for (int i; i> tests.Count; i++)
        {
            //return index;
        }
    }

    int findObject (string name)
    {
        for (int i; i > names.Count; i++)
        {
            //return index;
        }
    }

I want a way to put the list with the variable and return value I want

#

@thick latch

meager oyster
#

the way you solve this is by calling an overload

#
int Find(GameObject test)
{
    return Find(test.name);
}

int Find(string name)
{
    // put actual logic here
}
#

(but this seems to be a bad example, you should not be trying to "find" a match)

ruby brook
thick latch
#

Probably easier to show us the actual code you're working with

#

But if you have multiple lists and need to do specific operations for each you will simply just need multiple loops

ruby brook
# thick latch Probably easier to show us the actual code you're working with

The code consists of approximately 400 lines, but these are some parts

    void ammoindex(WeaponController weapon)
    {
        var ammolist = GinventoryManager.instance.ammoInventory;
        if (ammolist.Count > 0)
        {
            for (int i = 0; i < ammolist.Count; i++)
            {


            }
        }
     void addItemRealTime()
        {

                if (AmmoItem.tileType == Items.items.Weapon && !Ifanded)
                {

                    for (int i = 0; i < Weapons.Count; i++)
                    {

                        
                    }

                }
                else 

    public void ChickAmmo()
    {


        if (!iGetMyAmmo)
        {
            if (amocrach.Count != 0)
            {
                for (int i = 0; i < amocrach.Count; i++)
                {
      
                }
            }
            else
            {
meager oyster
#

checking for count > 0 is redundant

#

but these methods seem to be doing different things, and writing each loop separately is not inherently a bad thing

#

it's bad if the logic is duplicated 100% - but that doesn't appear to be the case here

ruby brook
meager oyster
#

only seeing a subset of the code doesn't really give us any way to help shorten it. perhaps you can show one method or block in particular you think is long, and we'll see what we can do

ruby brook
#

I will try to find something

#

maybe this

#
    void select()
    {
        if (Input.GetKey(KeyCode.Alpha1))
        {
            selectGun = 0;
            WeaponsSelecting();
        }
        else if (Input.GetKey(KeyCode.Alpha2))
        {
            selectGun = 1;
            WeaponsSelecting();
        }
        else if (Input.GetKey(KeyCode.Alpha3))
        {
            selectGun = 2;
            WeaponsSelecting();
        }
        else if (Input.GetKey(KeyCode.Alpha4))
        {
            selectGun = 3;
            WeaponsSelecting();
        }

    }
#

@meager oyster

#

I think anyone who sees this number will go crazy😅

meager oyster
#

oh god

#

that is a long file

#

yes you can indeed shorten this

ruby brook
#

yes so match

meager oyster
#

it's a bit of a hack, but you can loop from 0-3, adding that value to Alpha1 and checking each

meager oyster
#

KeyCode being an enum means Alpha1 through Alpha4 are just numbers. you can cast them to integers, because they are just integers

#

so you can do KeyCode.Alpha1 + 2 which will give you the value KeyCode.Alpha3

#

so you can just loop from 0-3

#

add that to Alpha1 to get each Alpha value

ruby brook
#

like that

    {
        if (Input.GetKey(KeyCode.Alpha1))
        {
            selectGun = 0;
            WeaponsSelecting();
        }
        else if (Input.GetKey(KeyCode.Alpha1+1))
        {
            selectGun = 1;
            WeaponsSelecting();
        }
        else if (Input.GetKey(KeyCode.Alpha1+2))
        {
            selectGun = 2;
            WeaponsSelecting();
        }
        else if (Input.GetKey(KeyCode.Alpha1+3))
        {
            selectGun = 3;
            WeaponsSelecting();
        }

    }
meager oyster
#

in a loop

ruby brook
#

This will make them all conditions work at same time

meager oyster
#
for (int i = 0; i < 4; i++)
    if (Input.GetKey(KeyCode.Alpha1 + i))
      // ... do stuff
#

that's what I was getting at

#

then you only need one if statement

ruby brook
#

i get that

#

It works perfectly

#

thank u so match