[LUA] Way to see console log of active game

Status
Not open for further replies.
Level 6
Joined
Jul 21, 2020
Messages
67
Hey,

So, I'm creating this map and I has some fairly long Lua scripts in it that I'm trying to debug. Which has become kinda a nightmare because if there is a runtime error, it's super difficult to track down because it doesn't display any info about what caused that thread to crash.

I was wondering if anyone knew of a way to run a lua map from the command line or see a log of all of the runtime errors either while the map is going or afterwards once the map is closed.

Let me know if you need any more info or if any part of this doesn't make sense. I've been trying to find info on this and can't seem to find anything good. Thanks!
 
Level 6
Joined
Jul 21, 2020
Messages
67
Hey, thanks for the advice on that. Yeah, I'm currently doing the print thing but it's not always obvious what is causing an error and if there would be a way to catch and print the error on screen that's all I'd need. I've been looking for like a try / catch / except block but oddly, it doesn't seem like that's built into LUA, unless I'm missing something, which I most likely am. I basically just want to be able to see what the error message is. Would speed up dev SOOOOO much.
 
Level 14
Joined
Feb 7, 2020
Messages
392
Try using this in a wrapper or inserting it wherever you want to see broken code messages.

It could be tweaked to not require "name", but labels can be useful for grouping code then traversing in the text editor.

utility function:
Lua:
function debugfunc( func, name )
  local passed, data = pcall( function() func() return "func " .. name .. " passed" end )
  if not passed then
    print(name, passed, data)
  end
  passed = nil
  data = nil
end
example with func as variable:
Lua:
debugfunc(mySpawnUnitsFunc, "mySpawnUnitsFunc")
example with func as class and needing params:
Lua:
debugfunc( function()
  local someParam    = "someValue"
  local anotherParam = "anotherValue"
  mySpawnUnitsFunc:method(someParam, anotherParam)
  myOtherFunc:method(someParam, anotherParam)
end, "mySpawnUnitsFunc:method")
try broken code to test it:
Lua:
debugfunc( function()
  print("string" + 3)
end, "testbreak")
you should start seeing printed errors:

upload_2020-9-27_10-36-43.png
 
Level 14
Joined
Feb 7, 2020
Messages
392
Also, I noticed that in your debug function you nil'ed out the variables at the end. Do you need to do that in Lua? I read somewhere recently, can't remember where that you didn't need to do that in Lua, only Jass. Is that not the case though?
You shouldn't need it. It's a relic from when the garbage collector was causing frame hiccups.
 
Level 6
Joined
Jul 21, 2020
Messages
67
One last question. Let me know if I'm asking dumb or stupid questions.

What's the difference between these two lines?

Lua:
functionName.method(param1, param2)
functionName:method(param1, param2)

I've just been using the dot and not the colon, but I'm not sure what it does differently.

Okay, sorry to keep going off topic. Thanks for your responses so far! Super helpful.
 
Level 14
Joined
Feb 7, 2020
Messages
392
One last question. Let me know if I'm asking dumb or stupid questions.

What's the difference between these two lines?

Lua:
functionName.method(param1, param2)
functionName:method(param1, param2)

I've just been using the dot and not the colon, but I'm not sure what it does differently.

Okay, sorry to keep going off topic. Thanks for your responses so far! Super helpful.
Using colon invisibly passes in the object as "self" so you can reference it. If you use metatables, you can then run methods on objects with ease. Lua classes are a key feature you should read about: Programming in Lua : 16

Lua:
functionName:method(param1, param2)
translates to:
Lua:
functionName.method(functionName, param1, param2)
-- you could then test the mechanics of it with a test class:
function myFunc:method(param1, param2)
  print(self)
  print(myFunc)
end
-- self == myFunc
 
Level 6
Joined
Jul 21, 2020
Messages
67
Okay, good to know. Weirdly, I’ve set up my own classes but have been using just the dot everywhere instead of a colon. Weird that it was still working, (or seems to be) without using a colon, as it’s referencing self a bunch in the code. I’m going to look into the more and try to figure out what’s going on there. Basically I’m working on creating a fuzzy AI for an AOS style map and I’ve created the AI as a class which is where the debugging request is coming from.
 
Status
Not open for further replies.
Top