[BNW] newbie to lua, what's wrong with this script?

ThanOscar

Chieftain
Joined
Feb 13, 2019
Messages
41
I'm using whoward's dll. I want to run an action when a great person is born, but it says "attempt to index local iUnit (a number value), isn't this the right way to ask?

Code:
function TestGiveGPFaith(iPlayer, iUnit, iUnitType, iPlotX, iPlotY)
    print("unit spotted")
    if iUnit:IsGreatPerson() == true then   --Or if iUnit:IsGreatPerson() then
        print("The unit is a great person my dudes")
    end
end

GameEvents.UnitCreated.Add(TestGiveGPFaith)
 
iUnit is the integer ID of the unit. To call methods (IsGreatPerson) you need the object that represents the unit. To get that you need the object that represents the player (as you only have the integer ID of the player in iPlayer), and then use the GetUnitByID() method to get the unit object.

local pUnit = Players[iPlayer]:GetUnitByID(iUnit)

then use pUnit:IsGreatPerson()

W

PS: Don't forget to enable the event with XML/SQL eg
INSERT OR REPLACE INTO CustomModOptions(Name, Value) VALUES('EVENTS_UNIT_CREATED', 1);
 
Last edited:
aaahhhhhhh that makes sense, thank you, i was fiddling by doing local pUnit == iUnit but i didnt understand if they were integers or unit classes
 
I don't understand: LUA/CheckGreatPerson.lua:9: attempt to call method 'GetUnitById' (a nil value)

Code:
function TestGiveGPFaith(iPlayer, iUnit, iUnitType, iPlotX, iPlotY)
    print("unit spotted")
    local pPlayer = Players[iPlayer]
    local pUnit = pPlayer:GetUnitById(iUnit)
    if pUnit:IsGreatPerson() then
        print("The unit is a great person my dudes")
    end
end

am i missing something??
 
Top Bottom