1. Choose your means of doom in the 17th Mini Mapping Contest Poll.
    Dismiss Notice
  2. A slave to two rhythms, the 22nd Terraining Contest is here.
    Dismiss Notice
  3. The heavens smile on the old faithful. The 16th Techtree Contest has begun.
    Dismiss Notice
  4. The die is cast - the 6th Melee Mapping Contest results have been announced. Onward to the Hive Cup!
    Dismiss Notice
  5. The glory of the 20th Icon Contest is yours for the taking!
    Dismiss Notice
  6. Shoot to thrill, play to kill. Sate your hunger with the 33rd Modeling Contest!
    Dismiss Notice
  7. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[LUA] Way to see console log of active game

Discussion in 'Triggers & Scripts' started by Kick King, Sep 7, 2020.

  1. Kick King

    Kick King

    Joined:
    Jul 21, 2020
    Messages:
    19
    Resources:
    0
    Resources:
    0
    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!
     
  2. Uncle

    Uncle

    Joined:
    Aug 10, 2018
    Messages:
    2,254
    Resources:
    0
    Resources:
    0
    Maybe something like this: [Lua] Console Window at Runtime

    That aside, make a habit of printing everything you do.
    Code (Lua):

    print("Before")
    SomeFunctionThatCrashesTheThread()
    print("After")
     
    It's annoying but it's not like it takes that much time to do.
     
    Last edited: Sep 15, 2020
  3. Kick King

    Kick King

    Joined:
    Jul 21, 2020
    Messages:
    19
    Resources:
    0
    Resources:
    0
    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.
     
  4. Drake53

    Drake53

    Joined:
    Jan 1, 2018
    Messages:
    417
    Resources:
    0
    Resources:
    0
  5. Planetary

    Planetary

    Joined:
    Feb 7, 2020
    Messages:
    290
    Resources:
    1
    Maps:
    1
    Resources:
    1
    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:
    Code (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:
    Code (Lua):

    debugfunc(mySpawnUnitsFunc, "mySpawnUnitsFunc")
     
    example with func as class and needing params:
    Code (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:
    Code (Lua):

    debugfunc( function()
      print("string" + 3)
    end, "testbreak")
     
    you should start seeing printed errors:

    upload_2020-9-27_10-36-43.png
     
  6. Kick King

    Kick King

    Joined:
    Jul 21, 2020
    Messages:
    19
    Resources:
    0
    Resources:
    0
    Thanks guys! @Planetary I appreciate you throwing all of that example code in there for me. I'll work on integrating that, and let you know how it goes. This bit of it has been killing my productivity because debugging takes such a long time with runtime errors.
     
  7. Kick King

    Kick King

    Joined:
    Jul 21, 2020
    Messages:
    19
    Resources:
    0
    Resources:
    0
    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?
     
  8. Planetary

    Planetary

    Joined:
    Feb 7, 2020
    Messages:
    290
    Resources:
    1
    Maps:
    1
    Resources:
    1
    You shouldn't need it. It's a relic from when the garbage collector was causing frame hiccups.
     
  9. Kick King

    Kick King

    Joined:
    Jul 21, 2020
    Messages:
    19
    Resources:
    0
    Resources:
    0
    Okay good to know. I'm still learning the best practices with Lua, very new to it, so I love understanding all of these little tips and tricks to getting things to work.
     
  10. Kick King

    Kick King

    Joined:
    Jul 21, 2020
    Messages:
    19
    Resources:
    0
    Resources:
    0
    One last question. Let me know if I'm asking dumb or stupid questions.

    What's the difference between these two lines?

    Code (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.
     
  11. Planetary

    Planetary

    Joined:
    Feb 7, 2020
    Messages:
    290
    Resources:
    1
    Maps:
    1
    Resources:
    1
    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

    Code (Lua):
    functionName:method(param1, param2)
    translates to:
    Code (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
     
  12. Kick King

    Kick King

    Joined:
    Jul 21, 2020
    Messages:
    19
    Resources:
    0
    Resources:
    0
    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.
     
  13. Kick King

    Kick King

    Joined:
    Jul 21, 2020
    Messages:
    19
    Resources:
    0
    Resources:
    0
    BTW, I got your Logging method to work in my code!!! Thank YOU!!! This brings back the joy to debugging and coding.