- Home /
How does Unity "read" JavaScript code?
If I have an update function with many different statements, does Unity execute them all at once every frame, or execute them line by line? For example, lets say I have a control scheme that first has an if statement that moves the character side to side, then another if statement that moves the character up and down. Will Unity first move the character side to side, then up and down, or compute both movements at the same time?
Sorry if this is a really basic question (new to Unity), but it would be really helpful to know how Unity "thinks" so I better organize my code.
Thanks!
Answer by Piflik · Apr 15, 2012 at 03:57 PM
Line after line...just like everything else. There is no way to do different things at the same time. Often later lines depend on changes done in earlier lines...if anything did calculate different lines simultaneously, it would be next to impossible to write any code at all.
I just remember hearing something like that somewhere, but line by line execution is what I thought, I just wanted to make sure!
Well, it actually does makes many thing at once at the processor level (Pipelines can generally do 5 different actions at once) but the coder does not see any of that and it is far beyond any of us to be able to know how the assembly codes are going to be generated.
You can use threads to do different things at the same time (literally at the same time if you have more than one core, otherwise it's really just rapidly switching between threads). That's a relatively advanced topic though, since it's easy to mess up and make things that fail randomly.
But seriously, you know this person has no program$$anonymous$$g background at all? Because if you've written print Hello World; print newLine;
, you learn that rule.
Yep, but by giving clues on advanced topics, who knows he might go and check and learn something about it.
Answer by fafase · Apr 15, 2012 at 03:59 PM
It takes one statement at a time, nut for a coroutine it jumps out of the update and put it on hold until the function returns. Smae when you have a callback function (Collision).
That's important thing to remember! Coroutines return to the caller code when the first yield is executed, and are resumed automatically each frame until they end.
Answer by maroonrs2 · Apr 16, 2012 at 07:02 AM
Technically, you all are right and wrong. What is does is use somethingm im refering to Lua as a coder for it, is (string.gmatch(arg1,arg2)) or match the string. It goes word, space, word, space and so on. Thats why when you play a code thats correct and you see if you use if's instead of else if's then you can do multiple things at once because it is matching the code up word by word. Enough said! BTW I hope ur under 15 cause im 15 -.-