Jump to content

[TUT] Debugging your code


Recommended Posts

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
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
  • 2 months later...
  • 5 months later...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...