#Should I just let it be for now and come
1 messages Β· Page 1 of 1 (latest)
Lets get to the bare minimum of the loops. A for loop uses an integer to loop through a piece of code {} until it reached the i < maxCount.
Is it not in the name? For each int in array, do code.
We need basic level shit here, haha
the first to lines, its what you need to know for the moment
So did you get my basic explanation of for?
So compare i to the size of the array
But in this example it is, right
because it's compared to numArray.Length
I understand it can be anything
doesn't always have to be an array
it's just an array in my example
var arr = new int[10];
for (int i = 0; i < arr.Length; i++)
{
int num = arr[i];
// do things with num
}
foreach (int num in arr)
{
// do things with num
}
These 2 snippets are functionally identical. Foreach pretty much does the above (only that it provides you with num directly) in the underlying code
When should I go for for and when should I go for foreach?
When the index of the current item is at all relevant to you, you use for
as you want, but if you want to loop from 1 to 5 for exemple, use for
or if you need to loop over the pair line for exemple
That too, you can limit the items you iterate through with for
Lets say I want to use it to go through a an array of playable characters in Unity
here, you will do "for(i = 2; i < array.lenght; i+=2)"
So... for = when you care about what's inside the array and foreach only when you care about the size?
with inside I mean the value
Foreach does not care about the size
foreach is for that too dude x) (when you care about whats inside)
there arn't rules between this, is as you want
=>You want to loop over all the element : use for or foreach, as you want
=>You want to loop over some elements and not all : use for
Based on this image it feels like for is the better method
with the only downsize it uses more code
side*
for performance yep, but its not a huge difference to my mind
personnaly, more often i use foreach, its just my preference, like Ero
The game i'm planning to make could run on a potato, so I'm not worried about performance that much, haha
its a bit faster to write
Why cant you break out of a foreach?
No. the image above says you cant break frmo foreach
you can break
So you don't iterate the whole collection, only until the element you're looking for
but your image says you cant
you can break for and foreach
yeah, idk why they say we can't
I love all you guys efforts to help me, but it's not easier if we don't agree on how it works, hahaha
I KNOW that you can break... ffs. Your image is tellin gyou cant. the one you share above. Is that even c# language?
maybe not ahahah
rip
thats why I wanted to have a thread, should have pm'd you π
there arn't rules between for/foreach, is as you want
=>You want to loop over all the element : use for or foreach, as you want
=>You want to loop over some elements and not all : use for
Just reminder this
its all
Is it possible to come to a conclusion you guys all agree on? π
use foreach to iterate over users.
Unless you care about the index
Or only want a specific amount
You probably don't
The problem is, the discussion is about two different topics here. What is the difference of foreach and for and what can be accomplished with it.
Difference is, for can iterate through any integer count with a given int you increment.
foreach will always need a List/array of something to iterate through and will check every item in that list/array.
Can you do the same with both, yes. The rest I agree with Ero, if you dont need the integer for anything, just take foreach.
yw π
most important thing, try, and you will see by yourself
Code is not always theorie, practice it
I agree, @placid wren, but I'm not able to come up with something if I don't know what it does
i have sooo much none finished project, but it let me try so much thnigs
What I understand is... foreach goes through the whole list/array
sure, but with experience you'll understand easier
what if you want to stop at every iteration?
to stop at every iteration to do what ?
For example swapping between characters in an array of playable characters
(I want to actually do that at some point, that's why I ask)
i don't really understand here
you loop over all the character controller yeah
and at each iteration, you want to swipe you character ?
Well lets say character SliDeen is at [0]
character Sander is at [1]
you press R1 on your PlayStation controller
and it goes from [0] to [1]
okay, i understand
it will be a bit different here
you need to know at wich index you are
So at the begining of the game, you use character SliDeen, you are at index 0
yes
The index need to be a variable (an int) private or public, as you want
when you press D, index is +1, when you press Q, index is - 1
Imagine you press D and you were at index 0
when you push D, you will call a function to change your character controller (for exemple, we will just disable the character controller)
so, you will just do, in your function
myCharacterControllerArray[currentCharacterControllerIndex].enable = false;
currentCharacterControllerIndex ++;
myCharacterControllerArray[currentCharacterControllerIndex].enable = true;
just a thing here, you need to be carefull with "currentCharacterControllerIndex", it has to be between 0 and lenght of the list, it can't be outside of the bounds
I just remembered I wrote something like this before I understood what it does
do you understand what it do here ?
int currentIndex = 0;
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
currentIndex--;
currentIndex = currentIndex < 0 ? bodyController.Length - 1 : currentIndex;
You disable the current CC (=character controller), increment the current index, and enable the CC at new index
bodyAnimator.runtimeAnimatorController = bodyController[currentIndex];
was also part of that
currentIndex < 0 ? bodyController.Length - 1 : currentIndex;
this line could be hard to understand at your level, but it just clamp the index between 0 and bodyController.lenght
yeah, because the length is always one more than the index numbers right
yep
Length 10 = index 0-9
ahahha
I use the blue one for work and the red one for private use, so you're lucky π
ahahah yeah i see that
This isn't work though, this is a hobby that takes just as much time as work
Doing this masterclass in my week off