Help with ChangeGoldBalance()

Hatsu

Chieftain
Joined
Oct 31, 2005
Messages
8
Hi,

I wanted to use this mod to create a small list with a few helper functions for me. Menu creating works as intended, but my custom function fails on the above action. So that's why I'm posting here and not in the mod discussion thread.

This is my function:
Code:
function SetMyGold()
    local player = Players[Game.GetLocalPlayer()];
    if player:IsHuman() then
        local pTreasury = player:GetTreasury();
        local pGold = pTreasury:GetGoldBalance();
        pTreasury:ChangeGoldBalance(20);
    end
end

Clicking on the button produces and in line 697 is pTreasury:ChangeGoldBalance(20);
Code:
Runtime Error: C:\Users\myuser\Documents\My Games\Sid Meier's Civilization VI\Mods\ModularScreens\UI\LaunchBar.lua:697: function expected instead of nil
stack traceback:
    C:\Users\myuser\Documents\My Games\Sid Meier's Civilization VI\Mods\ModularScreens\UI\LaunchBar.lua:697: in function 'SetMyGold'
    C:\Users\myuser\Documents\My Games\Sid Meier's Civilization VI\Mods\ModularScreens\UI\LaunchBar.lua:208: in function '(anonymous)'
Inserting print statements in the function show that player is not nil, treasury is not nil and printing GetGoldBalance prints the right amount of current gold in log.

So I guess there's something I miss. I read something about contexts but actually I have no idea how to change it in my mod.

My modinfo file is:
Code:
<?xml version="1.0" encoding="utf-8"?>
<Mod id="f596994b-30d8-4639-9175-1c2e965cbd20"  version="1.0">
  <Properties>
    <Name>LOC_MODULAR_SCREENS_NAME</Name>
    <Stability>LOC_MODULAR_SCREENS_STABILITY</Stability>
    <Teaser>LOC_MODULAR_SCREENS_TEASER</Teaser>
    <Description>LOC_MODULAR_SCREENS_DESCRIPTION</Description>
    <Authors>LOC_MODULAR_SCREENS_AUTHORS</Authors>
    <AffectsSavedGames>0</AffectsSavedGames>
    <SupportsMultiplayer>0</SupportsMultiplayer>
    <SupportsHotSeat>0</SupportsHotSeat>
  </Properties>

  <Components>
    <ImportFiles id="MODULAR_SCREENS_IMPORT_FILES">
      <Items>
        <File>UI/launchbar.lua</File>
        <File>UI/launchbar.xml</File>
      </Items>
    </ImportFiles>
  </Components>

  <LocalizedText>
    <Text id="LOC_MODULAR_SCREENS_NAME">
      <en_US>Modular Screens</en_US>
    </Text>
    <Text id="LOC_MODULAR_SCREENS_STABILITY">
      <en_US>Alpha</en_US>
    </Text>
    <Text id="LOC_MODULAR_SCREENS_TEASER">
      <en_US>Allows modders to safely add icons to launchbar, hookups and pulldown.</en_US>
    </Text>
    <Text id="LOC_MODULAR_SCREENS_DESCRIPTION">
      <en_US>Allows modders to safely add icons to launchbar, hookups and pulldown.</en_US>
    </Text>
    <Text id="LOC_MODULAR_SCREENS_AUTHORS">
      <en_US>astog</en_US>
    </Text>
  </LocalizedText>

  <Files>
    <File>UI/launchbar.lua</File>
    <File>UI/launchbar.xml</File>
  </Files>
</Mod>
 
It has to be done in a GameplayScript file loaded as like this file is in this snippet of modinfo file code
Code:
  <InGameActions>
    <AddGameplayScripts id="Civ6LUA_Testing_File2">
      <File>LUA/Civ6LUA_Testing_File2.lua</File>
    </AddGameplayScripts>
  </InGameActions>
<InGameActions> is equivalent to <Components>. The game will accept either one.
 
So I have to split my lua file into two, one for the UI and one for the gameplay where I do the changes to the game?
 
Yes, and you can communicate between contexts using LuaEvents.
 
Thank you. So I guess this is the right place to look for all the available events? There I also find what to use in UI or gameplay context?
 
Yes for what's available in UI/Gameplay contexts, but it's not updated since some time, and some methods/events/objects may not be listed.

But LuaEvents are custom events, you can make your own, for example, in your case, we could create and use LuaEvents.ChangePlayerGold like that:

UI file
Code:
function SetMyGold()
    local playerID = Game.GetLocalPlayer()
    local player = Players[playerID];
    if player:IsHuman() then
        LuaEvents.ChangePlayerGold(playerID, 20);
    end
end

GameplayScripts
Code:
function SetMyGold(playerID, value)
    local player = Players[playerID];
    local pTreasury = player:GetTreasury();
    pTreasury:ChangeGoldBalance(value);
end
 LuaEvents.ChangePlayerGold.Add( SetMyGold )
 
Thank you so much. I will give it a try tomorrow evening and will report after that. It's kinda late here now :)
 
Back
Top Bottom