So, I am following a tutorial and this happens:
.
The Execute method is in another script and at the bottom of the code in that method, it returns false.
Here is my question, wouldn’t this mean that the loop offsets forceExit by one, because:
Is at the bottom of the ExecuteActionsList method and therefore ExecuteActionsList doesnt know if its true or false until its ran it atleast once? And by then its too late as the next item in the for loop is ran?
I really dont understand how this code works because of my understanding. Any advice?
I can’t say this enough, learn to use the debugger!
You’ll easily see how the control flow works.
Just set a breakpoint, start a debugging session and then step through your code.
It’s really not that hard and it gives you a lot of insight into how c# code is executed.
there will be a class property called forceExit this gets set, but when force exit gets set there is still one record to be executed, on the next loop it will exit without executing but it will still enter the loop.
Let’s say the list has 3 elements, then what the method does is:
It won’t check the last forceExit but it doesn’t matter because the method ends anyways.
Also since forceExit isn’t a local variable it could happen that it’s already been set to true, in this case the method won’t call any Execute()
The return statement means it will exit the method immediately.
I get that, but say the script takes in a method that returns true.
I dont assign forceExit until the very end of the ExecuteActionsList method. So it will run once, before it later sets forceExit as true with:
And by then the if statement has already ran?
EDIT: I think I am starting to get this. I think my brain was thinking the
was ran and then it checks if its forceExit is true or false. But instead is the method constantly checking to see if forceExit is true or false after it sets the if condition?
C# devs
null reference exceptions