Lua abort without error message?

Discussion in 'Mod Creation Help' started by Serp, Oct 1, 2018.

  1. Serp

    Serp King

    Joined:
    Apr 1, 2015
    Messages:
    650
    I'm currently trying to catch the location of a unit that died and this code:


    print("unit lost");
    print(m_unitDeaths[PlayerID]);
    local pPlayer = Players[PlayerID];
    local pUnit = pPlayer:GetUnits():FindID(unitID); -- unit is dead, so does not work?
    local UnitIndex = pUnit:GetUnitType();
    print(UnitIndex);

    does not raise any error, but the last print is never executed.
    That means that, maybe because pUnit is nil, the function is aborted without any error message.
    This makes debugging very hard.

    So what to do, so I get an error or warning message for everytime sth like this happens?
     
  2. LeeS

    LeeS Imperator

    Joined:
    Jul 23, 2013
    Messages:
    7,241
    Location:
    Illinois, USA
    Are you running this from a GameplayScripts context or from a UI Context ? In Civ6 lua objects are not always available in both contexts. Usually an attempt to use something in the wrong context will result in a function expected instead of nil error in the log.

    Unit:GetUnitType() for example is only valid in a UI context. To get the Index number from table <Units> for a given unit in GameplayScripts we can use Unit:GetType()

    If you are not getting a runtime error at the time of execution or a syntax loading error in the lua log, you should get "nil" printed into the lua log when UnitIndex from your code has a value of nil. Without seeing the "context" from within which your quoted lines of code originate, hoever, it is impossible to determine what is or is not happening when you execute those quoted methods.
     

Share This Page