• Our friends from AlphaCentauri2.info are in need of technical assistance. If you have experience with the LAMP stack and some hours to spare, please help them out and post here.

Need help with simple ContextPtr and TopPanel UI Change

hazel16

Chieftain
Joined
May 5, 2014
Messages
64
First, thanks to all for the wonderful modding posts and tutorials. There's a reason this is the first time I've had to post a question here.

I'm making a mod for BNW that does not use the tourism system, so I'm trying to hide tourism from the TopPanel. I've successfully done this by modifying TopPanel.lua but would like to avoid this method so I can preserve compatibility with other mods (like IGE).

The following works elegantly in FireTuner (with state set to InGame):

Code:
ContextPtr:LookUpControl("/InGame/TopPanel/TourismString"):SetHide(true)

But returns a "attempt to index a nil value" error when I include it in my mod's lua files. I've tried many variations, including

- above command alone in its own Lua file
- command changed to:
Code:
ContextPtr:LookUpControl("../TourismString"):SetHide(true)
- command in a function called on ActivePlayerTurnStart
- setting Lua file as InGameUIAddin
- setting VFS true/false

And always get the nil value error.

My code currently looks like this:

Code:
print("H_Hide_Tourism.lua loaded!!!!!!!!");

function C_HideTourism ()
ContextPtr:LookUpControl("/InGame/TopPanel/TourismString"):SetHide(true);
end

Events.ActivePlayerTurnStart.Add(C_HideTourism());

What am I missing?
Thanks in advance!
 
ContextPtr:LookUpControl() can't be used outside of a method in a Lua file as that executes before the context tree has been built, and it also doesn't play nicely during loads, so you'll need something like

Code:
local bIsRegistered = false

function SetActivePlayer(iPlayer, iPrevPlayer)
  if (not bIsRegistered) then
    -- This doesn't want to play nicely at load time!
    local control = ContextPtr:LookUpControl("/InGame/TopPanel/GoldPerTurn")
    
    if (control) then
      control:RegisterCallback(Mouse.eLClick, OnGold)
      bIsRegistered = true
    end
  end
end
Events.GameplaySetActivePlayer.Add(SetActivePlayer)
 
Back
Top Bottom