Solarus-Games English Forum

Solarus => Development => Topic started by: froggy77 on April 13, 2015, 08:23:49 PM

Title: Create an output file to debug your script
Post by: froggy77 on April 13, 2015, 08:23:49 PM
I am a beginner in lua scripting , but I would like to share something that helped me to debug a problem in a lua script.
This will be useful only for newbies like me ;)
I simply redirected the outcome of variables into a new text file to better understand what was wrong with my script .
Just add these lines in the script by replacing, of course, the name of your variables.
For my part , I inserted these lines in a loop.

- Here an example with 3 variables: var1, var2 and booleanvar

file = io.output (io.open ("my_debug.txt", "a +"))
io.write ("variable1 =" ,var1 , "\tvariable2 =", var2, "\t", "\tboolean variable =")
if booleanvar then
io.write ("true")
else
io.write ("false")
end
io.write ("\n")
io.close (file)


- Info:
"\t" means tab
so "\t", "\tboolean variable =" will generate 2 tabs.

"\n" creates a new line in the file.

Further explanation here (http://www.railsim-fr.com/forum/index.php?showtopic=2424) and here (http://www.luteus.biz/Download/LoriotPro_Doc/LUA/LUA_Training_FR/LUA_FonctionInputOutput.html)
(Sorry but it's in French)


- In "my_debug.txt" that will be created in your Solarus project, you will get lines like these:
(Of course DO NOT name your output file "error.txt")

Quote
variable1 =232   variable2 =277      boolean variable =false
variable1 =232   variable2 =277      boolean variable =false
variable1 =232   variable2 =277      boolean variable =false
variable1 =232   variable2 =277      boolean variable =false
variable1 =232   variable2 =277      boolean variable =false
variable1 =232   variable2 =277      boolean variable =false
variable1 =232   variable2 =277      boolean variable =true
variable1 =232   variable2 =277      boolean variable =false
variable1 =232   variable2 =277      boolean variable =false
variable1 =232   variable2 =277      boolean variable =false
variable1 =232   variable2 =277      boolean variable =false
variable1 =232   variable2 =277      boolean variable =false
variable1 =232   variable2 =277      boolean variable =false
variable1 =232   variable2 =277      boolean variable =true
variable1 =338   variable2 =413      boolean variable =false
variable1 =338   variable2 =413      boolean variable =false
variable1 =338   variable2 =413      boolean variable =false
variable1 =338   variable2 =413      boolean variable =false
(...)