[BNW] [SOLVED] "index global 'Map' (a nil value)" even without mods.

Wobzter

Chieftain
Joined
Mar 24, 2020
Messages
4
Hello all,

i am trying to work with LUA and I run into the issue as described in the title.
For whatever reason I cannot acces the instance "Map". For example, when I do "Map.GetPlot(3,3)" in FireTuner while playing a normal (non-modded!) game, I get the following error:

Code:
> Map.GetPlot(3,3)
Runtime Error: _cmdr = {Map.GetPlot(3,3)}:1: attempt to index global 'Map' (a nil value)
stack traceback:
    _cmdr = {Map.GetPlot(3,3)}:1: in main chunk

From my understanding the instance Map should always be created, no?
This error persisted even upon restarting Civ 5, playing with mods and playing for more than 0 turns.
I have no extra mods in the DLC folder (like TakMod), so it's all "clean" in that sense.

Bonus info: I am trying to give a new Civ "Thule" a bonus of +1 food on snow and tundra. As a matter of practice I try to do this with LUA rather than XML (the prints are to see how far I can get without errors - it prints 2, but not 2.5):

Code:
print("1")
function ThuleSnowTundra(iHexX,iHexY,iPlayer,bunknown)
        print("2")
        local plotX, plotY = ToGridFromHex(iHexX, iHexY);
        local pplot = Map.GetPlot(plotX, plotY)
        plot("2.5")
        
        if pplot:GetPlotType() == PlotTypes.PLOT_TUNDRA or pplot:GetPlotType() == PlotTypes.PLOT_SNOW then               
            print("3")
            local pPlayer = Players[iPlayer]
            if pPlayer:GetCivilizationType() == GameInfo.Civilizations.CIVILIZATION_THULE.ID then
                Game.SetPlotExtraYield(x,y,YIELD_FOOD,1)
                print("4")
            else
                Game.SetPlotExtraYield(x,y,YIELD_FOOD,-1)
                print("5")
            end
        end
end



Events.SerialEventHexCultureChanged.Add(ThuleSnowTundra)


Thank you very much for your attention!

Cheers
Wobzter
 
http://modiki.civfanatics.com/index.php?title=PlotType_(Civ5_Type)

  1. The function
    Code:
    Map.GetPlot(iX, iY)
    is valid code but first you must be sure you are getting a valid XY grid-location return from
    Code:
    ToGridFromHex(iHexX, iHexY)
  2. Firetuner is it's own hybrid beast and what you get from command-line execution within Firetuner is often meaningless in relation to what works in a In-Game lua script.
  3. you never get "2.5" printed into the lua log because this is not a print command
    Code:
    plot("2.5")
    It will be interpreted by lua as being a function which has never been defined (ie, a nil value error) which will terminate the attempt to execute the function
  4. See the provided link. There is no such thing as PlotTypes.PLOT_TUNDRA so far as I am aware. You need to use
    Code:
    pplot:GetTerrainType()
    and compare against
    Code:
    GameInfoTypes.TERRAIN_TUNDRA
    for example.
  5. Your variables for grid location here have never been defined
    Code:
    Game.SetPlotExtraYield(x,y,YIELD_FOOD,1)
    And YIELD_FOOD must be designated properly as either of
    Code:
    YieldTypes.YIELD_FOOD
    or
    Code:
    GameInfoTypes.YIELD_FOOD
 
Thank you so much LeeS! I've seen you responding to many questions in my search for the answer, and I was hoping you'd respond - but you were way quicker than I imagined! Thank you so much! So far it all seems to be working! I still don't understand why I got a Map-error before as the problems started afterwards (with the plot which should've been a print - silly me).

Anyway, I again thank you so much!
I also added an extra if-statement in the beginning to avoid this code from running when the plot turns to player -1 (i,e. no one) - as to avoid processing, especially when the game sets up.

I haven't been able to test properly that the food also goes down when the tile gets lost, but I'll keep you updated when I do those tests.
 
Back
Top Bottom