AeroXbird Posted July 22, 2012 Share Posted July 22, 2012 Today i'm going to show you how to debug your code, this is very helpful for both you, and the people who might help you in the scripting forum. for example: function peeps() if ( getElementType(source) == "player" ) then outputChatBox(getPlayerName(source), source) end end Is wrong and does not function, its a very simple example but shows how to debug your code effeciently. Now how do we get to work. We start off by checking if we actually pass our if check, like so: function peeps() if ( getElementType(source) == "player" ) then outputChatBox(getPlayerName(source), source) outputDebugString("We passed our check!",3) end end If we passed the check, it will show the message 'We passed our check!' in the debug log. Let's say it didn't. function peeps() outputDebugString(tostring(getElementType(source))) if ( getElementType(source) == "player" ) then outputChatBox(getPlayerName(source), source) outputDebugString("We passed our check!",3) end end We make sure that what we try to check for, is actually correct. In this example we're going to assume that source is nil. Now we know that source is not the player, say that this function is called by a command handler If we check the MTASA Wiki, we see that a commandhandler returns a playersource, which is what we need. function peeps(thePlayer) outputDebugString(tostring(getElementType(thePlayer))) if ( getElementType(thePlayer) == "player" ) then outputChatBox(getPlayerName(thePlayer), thePlayer) outputDebugString("We passed our check!",3) end end now we just replace all the source variables that we thought was the player with the variable thePlayer. and our script should function properly.. now we get things tidy: function peeps(thePlayer) if ( getElementType(thePlayer) == "player" ) then outputChatBox(getPlayerName(thePlayer), thePlayer) end end And that rounds up my small, but effective tutorial. I hope that this informs you a bit about how you can easily debug your code yourself, without help from anybody. All you need is to think wisely, and think with common sense. Usually the mistake is something very easy, or something you never imagined could be the issue Link to comment
Jaysds1 Posted July 22, 2012 Share Posted July 22, 2012 This is very useful for people who has problems with scripting. thanks Aero Link to comment
Callum Posted July 23, 2012 Share Posted July 23, 2012 With outputDebugString, if the level is 3, you don't need to specify (as this is the default value). Link to comment
Edikosh998 Posted July 26, 2012 Share Posted July 26, 2012 This is useful, but it would be more useful if someone explain HOW TO USE THE DEBUGGER. I saw people saying : "I have this problem : 'Expected "end" at line 26' " More obvious impossible. Link to comment
Anderl Posted July 26, 2012 Share Posted July 26, 2012 This is useful, but it would be more useful if someone explain HOW TO USE THE DEBUGGER. I saw people saying : "I have this problem : 'Expected "end" at line 26' " More obvious impossible. That's true, I don't understand how people can't understand this. Expected 'end' at line 26 -> A 'end' is needed at line 26. Hard? Link to comment
AeroXbird Posted July 27, 2012 Author Share Posted July 27, 2012 Just like Anderl said, its very easy. Just read what it says. so if it says: expected end at line 26. you do what it says, and you simply add an end at line 26. all you have to do is think with common sense, and scripting is a breeze. Link to comment
krischkros Posted April 3, 2013 Share Posted April 3, 2013 Is actually fine. What bothers me is that in MTA can not "correct" debug. (With breakpoints and Watcher) Link to comment
Recommended Posts