flexaplex, first let me say, excellent job. Others have given enough reason why, thus I won't explain. Just good stuff.
Five things I noticed (outside of trivial typos and whatnot) that you might want to fix, however:
First, Variables
num = 1;
item = potion;
draw_text(x,y,item+string(num));
I wasn't sure what you were doing until I read your paragraph on it. Did you mean to write
num = 1;
item = "potion";
draw_text(x,y,item+string(num));
? Because otherwise, I don't see the variable potion containing the string "potion"... $:^ ]
Second, If/else statements
Since you cover switch statements here, it might be good to rename this section, If/Else Statements, Switch Statements. Up to you of course. (When trying to find switch in your tutorial again, my initial instinct to glance at headlines didn't work.) But the thing that really got me when I read through was this:
switch (x)
{
case < 40: {}
case > 40: {}
}As things like this do not work you will need to use a normal if statement instead.
When actually, you
can make it work. I suggest either saying you can do things like this once you get more advanced (and leaving out the explanation), or perhaps bring back this example after explaining expressions. How it can work:
switch (n < 40) {
case true:
// what to do when n < 40
break;
case false:
// what to do when n >= 40
}Chances are, you want to test n<40 vs. n>=40, or n<=40 vs. n>40, but if you really truly are avoiding n==40, then the regular if..else statements
if (n != 40) {
if (n < 40) {
// what to do when n < 40
} else {
// what to do when n > 40
}
} can be switched like so:
if (n != 40) {
switch (n < 40) {
case true:
// what to do when n < 40
break;
case false:
// what to do when n > 40
}
}or
switch (n < 40) {
case true:
// what to do when n < 40
break;
case false:
if (n > 40) {
// what to do when n > 40
}
}or even
switch (n < 40) {
case true:
// what to do when n < 40
break;
case false:
switch (n > 40) {
case true:
// what to do when n > 40
}
}...and of course you can substitute 1 and 0 for true and false, respectively.
EDIT: I realized after I got home (no Internet) that I forgot the easiest way to switch between the two, which actually works better than nested if's (since it also avoids nested switch's):
switch (true) {
case (n < 40):
// what to do when n < 40
break;
case (n > 40):
// what to do when n > 40
}I'll just leave the previous (less efficient) examples so that anyone can compare/contrast the different methods and learn from them.
Third, If/else statements
It is technically bad notation to drop the > 0 comparison and exploit how GM handles non Boolean expressions to make the code shorter,
Really? I've never heard of this before, that if(!x){} is bad notation. I even tried Googling for awhile and couldn't find anything on it. Could you show a source that says this is bad notation? I'd hardly say things like that "exploit how GM handles Boolean expressions" because every English-based programming language I've ever seen uses Booleans like that. I'm just curious to see where you learned this, thanks. $:^ ]
Fourth, Registered Functions
Registered: Nothing requires pro/registered version
Perhaps you've forgotten that all the data structure (ds) functions are Registered/Pro only. Fortunately, you
have written very well on the topic of arrays, in my opinion. And, fortunately, if you removed 1D ds code, you could move straight from 1D Arrays to 2D Arrays. (You
could, however, mention that the Pro version allows use of "data structures" which in many cases work a lot more efficiently than arrays.) And, like you said, ds's are much like paths in that they're not basic, general coding skills.
Fifth, 2D Arrays
I agree with the others, the double-for-loop
var i,j;
for (i = 0; i < 3; i += 1)
{
for (j = 0; j < 3; j += 1)
{
draw_text(10+j*10,10+i*10,square[i,j]);
}
}made perfect sense to me, but a beginner might stare and drool for a little bit. $:^ b The main suggestion I'd make is showing all nine values as you go, and why. (A slopped-together explanation example: i = 0, and i < 3, thus we start the first iteration of the first for (i) loop ... j = 0, and j < 3, thus we start the first iteration of the *second* for (j) loop ... it draws text (with an explanation), then finishes this iteration and comes back to the for (j) loop that's already running ... add 1 to j which now = 1, which is < 3, thus draw_text() again, (repeat explanation until..).. add 1 to j which now = 3, and j is *not* less than 3 now, thus this entire for (j) loop has ended ... and we hit the end of the *first* iteration of the *first* for (i) loop ... add 1 to i, i now = 1, which is < 3, now do the *second* iteration of the first for (i) loop ... this time, it goes through the second for (j) loop adding 1 to j with each iteration, and i=1 the whole time ... when the second for (j) loop is finished, we come to the end of the second iteration of the first for (i) loop ... i+=1 thus i=2 (thus i<3) and we go through the second for (j) loop with i=2 this time ... when the for (j) loop ends this time, i+=1 thus i=3 thus i is *not* less than 3, and the first for (i) loop finally ends.) It might be nice to create an example on how to use nested for loops while you're still explaining for loops -- and then bring the nested for loop back for the 2D array. For one thing, repetition is a good way to learn, and therefore reading about the nested for loop once, and then getting it reinforced soon after, should go well.
Allllllrighty, like I said, you've written an excellent tutorial in my opinion. Thanks for all the work you put into it.
Edited by ParodyKnaveBob, 24 January 2010 - 12:53 AM.