If your nesting 6 loops deep your doing something wrong IMO
True.
But the problem remains. I was greatful that local variables are now introduced which is useful for for loops.
But it seems like for loops are a pretty bad thing to use.
Thats good to know and as my first step when i get to optimization i will implement my own for loops.
Blizzard should stop taking us for complete idiots who never coded. That would make such extreme precautions uneccessary. I just wait untill they allow us to code in raw data rather than the UI, like they did in Wc3. That would fix all the messiness that comes from the UI. Even If statements and conditions are propably messy.
Quote from adovid:
I made a post on the b.net thread.
It has little to do with being user friendly and a whole lot to do with keeping the game from hanging or freezing.
Heck, the code expansion and the limit might even be a "keep dummies from breaking this" kind of fail-safe for GUI users because they don't know that you can break a game by doing too much at once.
----
If that's true, then why are While loops magically okay? For and While loops both essentially do the same thing, just slightly differently from the code standpoint. I can make a for loop and a while loop do exactly the same thing with nearly the same lines, if not exactly the same amount. If there's a problem where for loops specifically cause issues, that's Blizzard's fault, not ours.
There are situations where you need gigantic nested loops, though when it comes to map making it's generally less likely, but still perfectly possible. Just because you have a huge set of nested loops doesn't mean you're doing something wrong. You might be trying to do too much, but it doesn't mean it's wrong.
Honestly, an idea of "keeping dummies from doing this" is absolutely the wrong way to go. You will never learn what not to do if the engine pussy foots you around problems. If you've created code that will break the game, so be it, that's your fault. Of course, you run into the issue where people publish those types of things, but of course you can always make it unpublishable due to the reasons, but if you don't experience a problem like this first hand you may never learn what you did wrong. By having the editor just go "Hey, don't do that" people will not know why, except "Hey the Editor doesn't like it". Instead, I think it should allow them to test, and then when it blows up in their face...well you learned the hard way.
It still brings me back to the question of, if the for loops go so out of control to "stop crashing", then why do while loops magically work with the same code? That, I repeat, is not a user-created problem, it's a developer-created problem. This is Blizzard's mistake, and they need to fix it. It's like my old programming classes where I was told "YOU MUST USE A WHILE LOOP, A FOR LOOP WILL GET YOU A 0 ON THIS ASSIGNMENT!"
For-loops are always messy. For a non-nested loop is much less problematic. A while loop would still be more efficient, but the different is far smaller.
Seems like a compelling response to that "Should I learn the Scripting Language?" thread.
I had a double nested for-loop with a very deep if then else (if then else (...)) in it and got the same error. It was really frustrating! Thanks for the help, I think I can redo the original code now \o/
I posted link, which is not a blizzard link, it is an article from a respected Computer Science website which explicitly mentions blizzard when talking about iteration and effective game scripting techniques.
http://queue.acm.org/detail.cfm?id=1483106
If you want to learn to be a good modder then you should consider what it means to be a good scripter. A good scripter has at least some clue of the limitations of the engine and the purpose and capability of the scripting language.
Basically what the article is saying is that it is admiring blizzard's implementation of for loops in their natives because they are very streamlined at iteration because they don't iterate arbitrarily, they only iterate over fixed numbers of things. So yes it is better to use blizzard's for loops because the engine only makes you think it's doing arbitrary iteration but it's really doing is establishing a collection that it iterates over so that it can optimize the wazoo out of your scripts.
You shouldn't think in terms of how much code you generate but how fast your code runs, which doesn't always mean more lines of code means less performance. Keeping branch prediction happy is the aim of the game when you want speed and that sometimes means expanding code.
Branch prediction happens on modern processors and is the key to making things happen really quickly, when a processor can rely on it's branch prediction it can execute operations in a timely manner but if you nest a whole bunch of loops or if statements it becomes hard for branch prediction to work.
If your nesting 6 loops deep your doing something wrong IMO
also when doing heaving looping put a wait in for .01 this helps keep you from overloading the trigger system in SC2
I really dont understand what you could possibly need to nest 6 loops deep. sounds like terrible logic to me.
The actual problem you ran into that you stated at the top of this post is due too to much code in a single trigger. I had this problem myself. So I took a good chunk of my code and moved it to an extrenal function that this trigger would call. That solved the -Code jump out of bounds (Try reducing the size of very large functions)"
Yes, of course you're doing something wrong if you have 6 nested For loops. =) That was just an example of how to break it with a single trigger.
As a note, however, it is not due to one large trigger. When I encountered the error, I reduced the size of the function that caused the error and started working in different triggers to see if the issue would still occur (ones without any nested loops at all), and adding more than a few lines of code caused the error to crop up again.
EDIT: Adovid, in regards to your last post, compare the code from While and For loops. You seem to be implying that the way their For loops is setup is intentional and is more efficient than any other way it would be setup. That is false.
Compare the code from While and For loops. They both use While loops in their code. The only difference is For loops include several hundred If statements as well and a ridiculous amount of duplicated code. While loops are more efficient. This isn't some weird veiled attempt on Blizzard's part to make For loops run more efficiently.
Just as a general rule of thumb when scripting you shouldn't try to use more than 3 nested loops anyways because of how fast the number of computations grows. I haven't looked at it, but you guys are saying that Blizzard inefficiently coded how to convert GUI for loops into their galaxy?
I posted link, which is not a blizzard link, it is an article from a respected Computer Science website which explicitly mentions blizzard when talking about iteration and effective game scripting techniques. http://queue.acm.org/detail.cfm?id=1483106
First, this link isn't really on the subject. It takes an example of a pattern that restricts the usage domain of a for loop, so that users can not do anything wrong. Here we are talking about a "flat" for loop that goes from a minimum value to a maximum value using a step.
Quote:
Given the number of times this pattern appears in the Warcraft III scripts, this could result in significant performance improvements.
Given the really poor performances of jass, I really don't believe about this argument :)
It's really great to give ACM articles as references but you should read them in detail! :)
How much closer can you get to arbitrary iteration than a loop? That is what they are describing even though it uses that term to describe looping more generally.
When he says "Although this was a fairly drastic solution, most games do not need arbitrary iteration in their scripts." he means general purpose loops.
What it describes is a design pattern. A general rule of thumb that makes the design better. And no, it's not just for dummies, arbitrary looping in a game is expensive when you do it over and over for no reason.
The article was for the purpose of showing the performance hit of looping in scripting, it just happened to turn around and mention blizzard's solution to the problem on a low level which was a bonus. (I swear I found this just looking on the internet when I saw this post, I typed in "game scripting loop" and it was right there.)
I could compare Blizzards scripting system to Dawn of War's SCAR and it was a headache trying to get functionality into that game. You had to add a rule that had an update called by the game loop and had to implement all of the event handling yourself, one wrong implementation would slow the game down because you couldn't stop it from calling your trigger every update rather than being called by a message system, it was like having nothing but a game loop and a set of functions and everything you added to the game showed a performance hit, you had to use time slicing. All of the performance would fly out the door if you tried to use a loop all at once because it would be called over and over again.
Go look at blizzard's list of implemented for loops and look at the options that allow you to iterate over a searched set. Those are there for performance reasons.
So then are you suggesting that for loops are faster than while loops? Is there a proper way to test this? Or to test is for loops really are slower than while loops?
Just as a general rule of thumb when scripting you shouldn't try to use more than 3 nested loops anyways because of how fast the number of computations grows. I haven't looked at it, but you guys are saying that Blizzard inefficiently coded how to convert GUI for loops into their galaxy?
I frequently use a nested for statement to for instance, execute a function on specific coordinates of the map, such as:
for x = 0 to 256 do
for y = 0 to 256 do
function
I can imagine someone wanting to perform a series of function per point, resulting in a doubly nested loop already.
The article isn't talking about for loops, but for each loops, which blizzard has provided in the form of "pick each unit in unit group" and "for variable in such and such group".
Like someone mentioned, this "inefficiency" is because for-loops can either be ascending or descending.
I've released a proper library to solve this, it's available at http://www.sc2mapster.com/assets/utility-library/ - let me know if you have any problems. The project page also shows an example of how it produces 125 lines for 8 nested loops.
True.
But the problem remains. I was greatful that local variables are now introduced which is useful for for loops.
But it seems like for loops are a pretty bad thing to use.
Thats good to know and as my first step when i get to optimization i will implement my own for loops.
Blizzard should stop taking us for complete idiots who never coded. That would make such extreme precautions uneccessary. I just wait untill they allow us to code in raw data rather than the UI, like they did in Wc3. That would fix all the messiness that comes from the UI. Even If statements and conditions are propably messy.
Quote from adovid:
I made a post on the b.net thread.
It has little to do with being user friendly and a whole lot to do with keeping the game from hanging or freezing.
Heck, the code expansion and the limit might even be a "keep dummies from breaking this" kind of fail-safe for GUI users because they don't know that you can break a game by doing too much at once.
----
If that's true, then why are While loops magically okay? For and While loops both essentially do the same thing, just slightly differently from the code standpoint. I can make a for loop and a while loop do exactly the same thing with nearly the same lines, if not exactly the same amount. If there's a problem where for loops specifically cause issues, that's Blizzard's fault, not ours.
There are situations where you need gigantic nested loops, though when it comes to map making it's generally less likely, but still perfectly possible. Just because you have a huge set of nested loops doesn't mean you're doing something wrong. You might be trying to do too much, but it doesn't mean it's wrong.
Honestly, an idea of "keeping dummies from doing this" is absolutely the wrong way to go. You will never learn what not to do if the engine pussy foots you around problems. If you've created code that will break the game, so be it, that's your fault. Of course, you run into the issue where people publish those types of things, but of course you can always make it unpublishable due to the reasons, but if you don't experience a problem like this first hand you may never learn what you did wrong. By having the editor just go "Hey, don't do that" people will not know why, except "Hey the Editor doesn't like it". Instead, I think it should allow them to test, and then when it blows up in their face...well you learned the hard way.
It still brings me back to the question of, if the for loops go so out of control to "stop crashing", then why do while loops magically work with the same code? That, I repeat, is not a user-created problem, it's a developer-created problem. This is Blizzard's mistake, and they need to fix it. It's like my old programming classes where I was told "YOU MUST USE A WHILE LOOP, A FOR LOOP WILL GET YOU A 0 ON THIS ASSIGNMENT!"
Seems like a compelling response to that "Should I learn the Scripting Language?" thread.
I had a double nested for-loop with a very deep if then else (if then else (...)) in it and got the same error. It was really frustrating! Thanks for the help, I think I can redo the original code now \o/
they should make it like in wc3
for loop =>
loop
exitwhen i>x
set i=i+1
endloop
I posted link, which is not a blizzard link, it is an article from a respected Computer Science website which explicitly mentions blizzard when talking about iteration and effective game scripting techniques. http://queue.acm.org/detail.cfm?id=1483106
If you want to learn to be a good modder then you should consider what it means to be a good scripter. A good scripter has at least some clue of the limitations of the engine and the purpose and capability of the scripting language.
Basically what the article is saying is that it is admiring blizzard's implementation of for loops in their natives because they are very streamlined at iteration because they don't iterate arbitrarily, they only iterate over fixed numbers of things. So yes it is better to use blizzard's for loops because the engine only makes you think it's doing arbitrary iteration but it's really doing is establishing a collection that it iterates over so that it can optimize the wazoo out of your scripts.
You shouldn't think in terms of how much code you generate but how fast your code runs, which doesn't always mean more lines of code means less performance. Keeping branch prediction happy is the aim of the game when you want speed and that sometimes means expanding code.
Branch prediction happens on modern processors and is the key to making things happen really quickly, when a processor can rely on it's branch prediction it can execute operations in a timely manner but if you nest a whole bunch of loops or if statements it becomes hard for branch prediction to work.
Yes, of course you're doing something wrong if you have 6 nested For loops. =) That was just an example of how to break it with a single trigger.
As a note, however, it is not due to one large trigger. When I encountered the error, I reduced the size of the function that caused the error and started working in different triggers to see if the issue would still occur (ones without any nested loops at all), and adding more than a few lines of code caused the error to crop up again.
EDIT: Adovid, in regards to your last post, compare the code from While and For loops. You seem to be implying that the way their For loops is setup is intentional and is more efficient than any other way it would be setup. That is false.
Compare the code from While and For loops. They both use While loops in their code. The only difference is For loops include several hundred If statements as well and a ridiculous amount of duplicated code. While loops are more efficient. This isn't some weird veiled attempt on Blizzard's part to make For loops run more efficiently.
@TheFallenOne222: Go
I say it is, it doesn't surprise me that their code expands when you deeply nest loops and if statements.
while (true) create threaded function(); kekekekekekekekekekeke
Just as a general rule of thumb when scripting you shouldn't try to use more than 3 nested loops anyways because of how fast the number of computations grows. I haven't looked at it, but you guys are saying that Blizzard inefficiently coded how to convert GUI for loops into their galaxy?
@adovid: Go
Trust me, I'm fully aware that you can break the game!~
First, this link isn't really on the subject. It takes an example of a pattern that restricts the usage domain of a for loop, so that users can not do anything wrong. Here we are talking about a "flat" for loop that goes from a minimum value to a maximum value using a step.
Given the really poor performances of jass, I really don't believe about this argument :)
It's really great to give ACM articles as references but you should read them in detail! :)
How much closer can you get to arbitrary iteration than a loop? That is what they are describing even though it uses that term to describe looping more generally.
When he says "Although this was a fairly drastic solution, most games do not need arbitrary iteration in their scripts." he means general purpose loops.
What it describes is a design pattern. A general rule of thumb that makes the design better. And no, it's not just for dummies, arbitrary looping in a game is expensive when you do it over and over for no reason.
The article was for the purpose of showing the performance hit of looping in scripting, it just happened to turn around and mention blizzard's solution to the problem on a low level which was a bonus. (I swear I found this just looking on the internet when I saw this post, I typed in "game scripting loop" and it was right there.)
I could compare Blizzards scripting system to Dawn of War's SCAR and it was a headache trying to get functionality into that game. You had to add a rule that had an update called by the game loop and had to implement all of the event handling yourself, one wrong implementation would slow the game down because you couldn't stop it from calling your trigger every update rather than being called by a message system, it was like having nothing but a game loop and a set of functions and everything you added to the game showed a performance hit, you had to use time slicing. All of the performance would fly out the door if you tried to use a loop all at once because it would be called over and over again.
Go look at blizzard's list of implemented for loops and look at the options that allow you to iterate over a searched set. Those are there for performance reasons.
So then are you suggesting that for loops are faster than while loops? Is there a proper way to test this? Or to test is for loops really are slower than while loops?
I frequently use a nested for statement to for instance, execute a function on specific coordinates of the map, such as:
for x = 0 to 256 do for y = 0 to 256 do function
I can imagine someone wanting to perform a series of function per point, resulting in a doubly nested loop already.
@ZarafFaraz: Go
The article isn't talking about for loops, but for each loops, which blizzard has provided in the form of "pick each unit in unit group" and "for variable in such and such group".
Like someone mentioned, this "inefficiency" is because for-loops can either be ascending or descending.
I've released a proper library to solve this, it's available at http://www.sc2mapster.com/assets/utility-library/ - let me know if you have any problems. The project page also shows an example of how it produces 125 lines for 8 nested loops.
Yeah but if blizzard has optimized the game to handle thier for loops doesnt that defeat the point of not using them?
inless you really need to save on those 6 lines of code extra that thiers uses. Im pretty sure doing this is pointless.