[BNW] Nil value error on event hook

Wichilie

Chieftain
Joined
Nov 9, 2017
Messages
6
Hey!

I've been trying to get a custom unit for a mod I'm making to have attack based on the current era. This is what I've got so far for the script:

Code:
print("Loading AssignEraStrength.lua...")

local iRequiredUnit = GameInfoTypes.UNIT_SHURIMAN_ASCENDED
local iStrengthPerEra = 15

function AssignEraStrength(iPlayer, iPlayerEra)
   local pPlayer = Players[iPlayer]
   for pUnit in pPlayer:Units() do
       if (pUnit:GetUnitType() == iRequiredUnit) then
           local iStrengthMultiplier = iPlayerEra + 1
           pUnit:SetBaseCombatStrength(iStrengthMultiplier * iStrengthPerEra)
       end
   end
end

Events.PlayerEraChanged.Add(AssignEraStrength)

print("Loaded AssignEraStrength.lua.")

However, running it gives this error:

Code:
AssignEraStrength: Loading AssignEraStrength.lua...
 Runtime Error: (path)/AssignEraStrength.lua:16: attempt to index field 'PlayerEraChanged' (a nil value)

I don't really see what would cause this to happen. As far as I know this only happens for assignments but it gives the error on the event hook. Any idea's?
 
Hey!

I've been trying to get a custom unit for a mod I'm making to have attack based on the current era. This is what I've got so far for the script:

Code:
print("Loading AssignEraStrength.lua...")

local iRequiredUnit = GameInfoTypes.UNIT_SHURIMAN_ASCENDED
local iStrengthPerEra = 15

function AssignEraStrength(iPlayer, iPlayerEra)
   local pPlayer = Players[iPlayer]
   for pUnit in pPlayer:Units() do
       if (pUnit:GetUnitType() == iRequiredUnit) then
           local iStrengthMultiplier = iPlayerEra + 1
           pUnit:SetBaseCombatStrength(iStrengthMultiplier * iStrengthPerEra)
       end
   end
end

Events.PlayerEraChanged.Add(AssignEraStrength)

print("Loaded AssignEraStrength.lua.")

However, running it gives this error:

Code:
AssignEraStrength: Loading AssignEraStrength.lua...
 Runtime Error: (path)/AssignEraStrength.lua:16: attempt to index field 'PlayerEraChanged' (a nil value)

I don't really see what would cause this to happen. As far as I know this only happens for assignments but it gives the error on the event hook. Any idea's?

The issue here is seemingly that Events.PlayerEraChanged doesn't exist, and as such you're doing the equivalent of nil.Add.

Idk what the actual Event you're looking for is, but there's a GameEvent you can use instead which is (probably) a better option:

Code:
function C15_CallFunctionOnTeamEra(teamID, eraID)
    for i = 0, GameDefines.MAX_CIV_PLAYERS - 1, 1 do
        local pPlayer = Players[i]
        if pPlayer:GetTeam() == teamID then
            AssignEraStrength(i, eraID)
        end
    end
end

GameEvents.TeamSetEra.Add(C15_CallFunctionOnTeamEra)

Although this is obviously less efficient than your code, it's more logical (since tech related stuff is usually handled on a Team level rather than on individual Player levels) and it's guaranteed to work for the AI too (since it's not unprecedented/uncommon for Events to only fire for the human player).
 
Events.PlayerEraChanged is indeed a nil value since as far as I can see it does not exist in Civ5. It is a Civ6 Event-Hook.

Aaaah, I see. I must've looked up the events from the wrong game.

Idk what the actual Event you're looking for is, but there's a GameEvent you can use instead which is (probably) a better option:

Code:
function C15_CallFunctionOnTeamEra(teamID, eraID)
for i = 0, GameDefines.MAX_CIV_PLAYERS - 1, 1 do
local pPlayer = Players[i]
if pPlayer:GetTeam() == teamID then
AssignEraStrength(i, eraID)
end
end
end

GameEvents.TeamSetEra.Add(C15_CallFunctionOnTeamEra)

This works, thanks!
 
Top Bottom