#make variable on method
1 messages · Page 1 of 1 (latest)
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
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
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)
I wrote this just to clarify, I have a lot of lists that have nothing in common so I need to use Loops for each one.
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
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
{
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
Yes u are right it does different things but I trying to find way to shorten the code
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
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😅
yes so match
it's a bit of a hack, but you can loop from 0-3, adding that value to Alpha1 and checking each
Can you explane that for me?
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
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();
}
}
in a loop
This will make them all conditions work at same time