The Script Builders' Tutorials:
----Lesson 5: Errors and Their Meanings----
----Lesson 5: Errors and Their Meanings----
I. Introduction
II. Standard Errors
III. Undefinition Errors
IV. Conclusion & EXTRAS
V. Homework
I. Introduction
Welcome back, if your reading this, you probably lived through my first tutorial. Unfortunately, this tutorial should probably have come before the first, and I will attempt not to make such mistakes again. I also want to make this as simple as possible without sounding so easy that you skip parts. You know what errors are, and you know how to find them. What this tutorial will teach you is how to decipher the error messages and make fixes (without my help, and without other people's help). When helping decipher errors, they will be explained in this format:
CODE
[b]Name[/b]
[b]Format: {"blah blah blah"}[/b]
[b]English[/b]
[b]Best Way to Fix[/b]
[b]Format: {"blah blah blah"}[/b]
[b]English[/b]
[b]Best Way to Fix[/b]
II. Standard Errors
Standard Errors, these are the errors are quite common (okay, sarcasm aside..). They consist of 80% to 90% of the errors you'll encounter. For the most part, they're easy to resolve. let's start from the top..
Argument error
Format: {wrong number of arguments(n for n)}
English: basically, for this one, you've given too many/few 'arguments.' In simpler terms, supposing that when I tell you to draw a rectangle, I tell you how HIGH I want it, and then tell you how WIDE I want it.. What would you do If I said, draw a rectangle with the dimensions of 3 inches, 2 inches, 1 inch. Well, if you were a computer, you'd tell me this: wrong number of arguments(3 for 2). You're asking for 2 arguments (width and height) and I'm giving you three (normal people like you would probably write me off as a crazy person..)
Best Way to Fix: Read the format, (n for n) the first number is how many your giving, the second, how many it wants. remove the extra numbers and double check that only the numbers you want to keep are there.
(IE: when calling: "draw_actor_graphic(actor, x, y)" provide those three arguments and no more/less. If you've provided (actor, x, y, height), don't remove 'actor/x/y.' Rather, remove 'height')
SystemStackError
Format: {stack level too deep}
English: this mostly occurs with aliasing. I actually haven't seen it occur otherwise, it just means that a def is being 'alias'ed too many times, the problem occurs when 1) using two scripts that alias the same class/def (IE: mixing two different custom message systems) 2) when you press F12 and you have a script that has an alias (for this there are F12 fix script snippets) or 3) when you alias a def within itself:
CODE
class Scene_Menu
def initialize(menu_index = 0)
alias initialize_new_scene_menu initialize
initialize_new_scene_menu
@menu_index += 1
end
end
def initialize(menu_index = 0)
alias initialize_new_scene_menu initialize
initialize_new_scene_menu
@menu_index += 1
end
end
Best Way to Fix: There's several ways to fix this, for 1) either remove one script or have them merged (NOTE: sometimes merging scripts is impossible) 2) use a F12 fix snippet, or just don't alias, write directly into the original class or write a new class (actually, 90% of the time I do not alias methods, they're less prone to stack errors this way, although, some(and even maybe you) might consider this unwieldy, and 'roundabout') 3) either don't alias, or alias BEFORE the def.
Type Error
Format: {String can't be coerced into Bignum/Fixnum/Float | cannot convert Bignum/Fixnum/Float into String}
English: In simpler terms, your trying to work with both numbers and text at the same time.. of course. if I gave you the numbers: 123456789123456786789 and told you to add "some numbers" to that, you'd say "123456789123456786789 some numbers." However, this is not the same when working with a script, you cant add a string to the end of some numbers, or vice versa. thus, if you wish to add string to numbers, or numbers to string, you must first convert the numbers into a string.
Best Way to Fix: if you meant to have numbers in a string, then convert them. (if you didn't mean to have them a string, correct that problem)
CODE
$BIGNUM_HERE = 12345678902345678901234567890
$STRING_HERE = "< some numbers"
$NEW_STRING = $BIGNUM_HERE.to_s + $STRING_HERE
print($NEW_STRING)
$STRING_HERE = "< some numbers"
$NEW_STRING = $BIGNUM_HERE.to_s + $STRING_HERE
print($NEW_STRING)
SEE 'Conclusion & EXTRAS' for help on understanding fixnum/bignum
Runtime Error
Format: {Runtime Error occured}
English: this happens when some of you get a little too ambitious and decide to setup exception raising. (which, don't get me wrong, doing such is a great thing) supposing I decide I want to start debugging, I add a raise to my script:
CODE
if @raise_me != nil
print("i give you life")
else
raise
end
print("i give you life")
else
raise
end
and so you get the runtime error, you could try fixing it by adding ("message here") but even though it displays the message, you still get a runtime error. This is because your forgetting to create an 'exception' first. Runtime errors aren't harmful, and usually require less coding than creating an exception, but for newbie or novice scripters who don't know, they'll easily get confused.
Best Way to Fix: first create an exception, you can do it prior, or during the error. this way is how to do it prior:
CODE
$nil_var_E = Exception.new("the variable referenced here is nil!")
and
CODE
raise($nil_var_E)
during:
CODE
if @raise_me == nil
raise(Exception.new("the variable referenced here is nil!"))
else
print("I give you life")
end
raise(Exception.new("the variable referenced here is nil!"))
else
print("I give you life")
end
ZeroDivisionError
Format: {Divided by 0}
English: really? do you need any more description on this? this problem occurs when you divide by zero.
(by the way, every time you divide by zero you create a black hole somewhere in the universe.. so don't.. (just kidding, but to be safe, don't divide by zero..))
Best Way to Fix: make sure you don't divide by zero, simple as that, and if you have a number that for some reason ranges from 0~3 or something like that, increment it prior to division.
CODE
$var_that_has_zero += 1
III. Un-definition Errors
There's not many of these errors, but I'll still try to cover them as simply as possible. These are the errors that occur when you fail to define something, whether it be a variable, or a class, a module, a def, anything. Basically put, it's like me asking you to tell me how many apples there are right now. You could never know right now, because I haven't defined how many there are. If I say the number of apples is the same as the number of oranges plus the number of pears minus two, you still don't know, and you don't know the number of oranges or pears. But if I said there were ten oranges, and three pears it'd be easy to tell me that the number of apples is eleven.
Name Error
Format: {Name error Uninitialized constant <>}
English: This usually only happens when your trying to work with nil constants. (variables usually with no @ or $ and in all caps LIKE_THIS) such as "UBER_NIL += TOTALY_UNDEF" will result in an a Name Error. It basically means you haven't defined the variable, or in better words, the program is saying this: 'this constant is still nil, how the heck do you expect me to add it to, what the? another nil constant?! nil + nil = undefined, bleargh, error!'
Best Way to Fix: make sure all constants are defined (non-nil), or that you don't try adding/subtracting/doing anything with them (other than comparing them like == or !=). And make sure the names are the same. (ABS::BAR_COLOR can go mistyped and unnoticed as ABS::BAR_COLLOR)
NoMethodError
Format: {NoMethodError occured. undefined method '<method>' for <module/class/nil>:<class/def/NilClass>}
English: remember the problem with apples? well, if I told you "function 'add'" to the 'variable' apples, you'd assume the amount of apples were 0, and count up/down by how many i told you to add/subtract. However, such is not the logic with RGSS, it would assume the number of apples were nil (nil is hard to 'contemplate' because we live in a non-nil world.). it would give you an error, probably "undefined method '+' for nil:NilClass" and you'll be wondering why. not any more, you now know that it's either because your trying to run a method on a nil variable. OR your trying to call a nil(non-existent) method. (IE: $scene.gimme_pie doesn't work, UNLESS you happened to create a 'def' in that scene named gimme_pie)
Best Way to Fix: make sure whatever your trying to operate on isn't nil, and make sure that the method your trying to call exists. By calling 'draw_atcor_name' instead of 'draw_actor_name' (a simple typo) you can cause this error.
IV. Conclusion & EXTRAS
There's one more kind of error you'll most likely encounter. It's the most annoying one ever, the 'syntax error' unfortunately, I could write a book explaining the ways you can encounter this. but I'm not going to. some things are best learned by trial and error. writing en instead of end, Class instead of class, not closing quotation marks, putting one too many 'end's or one too few end's, writing sel. instead of self. hitting 0 without pressing shift to write ), these are just a few of the many ways to encounter a syntax error. But with time, and knowledge how to decipher these error messages, you can find and fix the problems.
I hope you managed to survive through this, it came out a little more difficult than I wanted it to be. for those of you who survived this tutorial, great! if you didn't manage to get it all, that's okay too, all of these errors can be explained logically in 'human' terms. If you have any questions, I'd like to hear them, and I'll try to help as soon as possible.
About bignum/fixnum. you generally don't need to worry about these, but in case your curious, a fixnum should be any number between -2,147,483,648 to 4,294,967,295 and when it goes out of those bounds, it becomes a bignum, so 1 billion is a fixum, but 7 billion is a bignum. if that helps. generally, all numbers you'll work with should be fixnums, unless your doing some serious crunching in your game.
V. Homework
Is that a grin I see? Or are you frowning while doing a hand-stand? I was going to give you a whole class to fix.. but I decided on an alternative.
This def I'm about to give you contains several errors, I don't want you to fix it. I don't want you to even plug it into your script editor. in fact, if you do so your considered cheating, and while I'm not there to watch if you cheat or not, your the one who'll suffer with not being able to recognize errors as well. I want you to tell me which TYPE of errors occur where and why. (if you cant get them all, then just submit what you think are the ones.) for example,
CODE
alias main_new main
main_new
main_new
Will result in a stack error, why? because I've aliased a def while inside the def I'm aliasing. see? wasn't that easy? You don't even need to tell me how to fix it. (here's the script, and don't go off cheating/comparing how it looks to the original.)
Your Homework
(try putting your answers in spoilers, so you don't spoil them for other people)