#Should I just let it be for now and come

1 messages Β· Page 1 of 1 (latest)

mystic sedge
#

What is your problem exactly on this?

#

So we dont have to spam the channel πŸ˜„

hexed hawk
#

Thanks man, haha

#

I'm not sure why it isn't getting through my thick skull

mystic sedge
#

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.

wanton harbor
#

Is it not in the name? For each int in array, do code.

mystic sedge
#

no no no.... stop

#

πŸ˜„

hexed hawk
#

We need basic level shit here, haha

placid wren
#

the first to lines, its what you need to know for the moment

mystic sedge
#

So did you get my basic explanation of for?

hexed hawk
#

So compare i to the size of the array

mystic sedge
#

compare it to whatever number...

#

its not related to ANY array if you dont want

hexed hawk
#

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

wanton harbor
#
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

hexed hawk
#

When should I go for for and when should I go for foreach?

wanton harbor
#

When the index of the current item is at all relevant to you, you use for

placid wren
#

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

wanton harbor
#

That too, you can limit the items you iterate through with for

hexed hawk
#

Lets say I want to use it to go through a an array of playable characters in Unity

placid wren
#

here, you will do "for(i = 2; i < array.lenght; i+=2)"

hexed hawk
#

So... for = when you care about what's inside the array and foreach only when you care about the size?

wanton harbor
#

I'd personally use foreach for that

#

Other way around

hexed hawk
#

with inside I mean the value

wanton harbor
#

Foreach does not care about the size

placid wren
#

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

hexed hawk
#

with the only downsize it uses more code

#

side*

placid wren
#

for performance yep, but its not a huge difference to my mind

#

personnaly, more often i use foreach, its just my preference, like Ero

hexed hawk
#

The game i'm planning to make could run on a potato, so I'm not worried about performance that much, haha

placid wren
#

its a bit faster to write

mystic sedge
#

Why cant you break out of a foreach?

hexed hawk
#

it only takes break; i think

#

?

#

just as easy

mystic sedge
#

No. the image above says you cant break frmo foreach

wanton harbor
#

You can.

#

It's the whole point of it

placid wren
#

you can break

wanton harbor
#

So you don't iterate the whole collection, only until the element you're looking for

mystic sedge
#

but your image says you cant

placid wren
#

you can break for and foreach

placid wren
hexed hawk
#

I love all you guys efforts to help me, but it's not easier if we don't agree on how it works, hahaha

mystic sedge
#

I KNOW that you can break... ffs. Your image is tellin gyou cant. the one you share above. Is that even c# language?

placid wren
#

maybe not ahahah

mystic sedge
#

...

#

very useful pasting nonsense content here

placid wren
#

fuck its javascript

#

😐

hexed hawk
#

rip

mystic sedge
#

thats why I wanted to have a thread, should have pm'd you πŸ˜„

placid wren
#

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

hexed hawk
#

Is it possible to come to a conclusion you guys all agree on? πŸ˜„

wanton harbor
#

use foreach to iterate over users.

#

Unless you care about the index

#

Or only want a specific amount

#

You probably don't

mystic sedge
#

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.

hexed hawk
#

I think I get it

#

At least good enough to continue

#

10/10 effort guys

mystic sedge
#

yw πŸ˜„

hexed hawk
#

I would almost say I love you

#

I got a related question!

#

Now we're still here

placid wren
#

most important thing, try, and you will see by yourself
Code is not always theorie, practice it

hexed hawk
#

I agree, @placid wren, but I'm not able to come up with something if I don't know what it does

placid wren
#

i have sooo much none finished project, but it let me try so much thnigs

hexed hawk
#

What I understand is... foreach goes through the whole list/array

placid wren
#

sure, but with experience you'll understand easier

hexed hawk
#

what if you want to stop at every iteration?

placid wren
#

to stop at every iteration to do what ?

hexed hawk
#

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)

placid wren
#

i don't really understand here

#

you loop over all the character controller yeah

#

and at each iteration, you want to swipe you character ?

hexed hawk
#

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]

placid wren
#

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

hexed hawk
#

yes

placid wren
#

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

hexed hawk
#

yes

#

still following

placid wren
#

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

hexed hawk
#

I just remembered I wrote something like this before I understood what it does

placid wren
#

do you understand what it do here ?

hexed hawk
#
int currentIndex = 0;
    void Update()
    {

           if (Input.GetKeyDown(KeyCode.Q))
           {
            currentIndex--;
            currentIndex = currentIndex < 0 ? bodyController.Length - 1 : currentIndex;
placid wren
#

You disable the current CC (=character controller), increment the current index, and enable the CC at new index

hexed hawk
#

bodyAnimator.runtimeAnimatorController = bodyController[currentIndex];

#

was also part of that

placid wren
#

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

hexed hawk
#

yeah, because the length is always one more than the index numbers right

placid wren
#

yep

hexed hawk
#

Length 10 = index 0-9

placid wren
#

yeah !

#

so you understand here ? or you have still question about you want to do ?

hexed hawk
#

I think I got it for now

#

❀️

#

wow, that's big

placid wren
#

ahahha

hexed hawk
#

I use the blue one for work and the red one for private use, so you're lucky πŸ˜‰

placid wren
#

ahahah yeah i see that

hexed hawk
#

This isn't work though, this is a hobby that takes just as much time as work

#

Doing this masterclass in my week off

placid wren
#

i see, but you will see, with time your level will increase

#

its hard at the beggining but hten is easier

hexed hawk
#

Unity isn't that hard, so that's nice

#

it's C# that's breaking my mind sometimes