Can I Boost a Tech in LUA code?

PonderThis

Warlord
Joined
Nov 15, 2014
Messages
104
I'm trying to create a custom condition for boosting techs. Using the helpful info in Gedemon's Lua Objects doc I can hook up a function to the start of a turn, figure out what the current player is researching, whether that tech can be boosted and if it actually has been boosted. But I can't seem to find a way to actually trigger a boost myself. Does anyone have any ideas?
 
yes, it's not in the doc because it was added in a patch after the winter's patch, but it can be used in a script context

Code:
player:GetTechs():TriggerBoost(iTech)
 
Actually Infixo and Gedemon hit right on it, that was exactly what I was looking for.

OK now I have something really stupid but for the life of me I can't figure out what I'm doing wrong. I can't get my lua to run. I used as an example Research Reminder because it seems fairly simple. I slightly edited the very end of its lua file with a couple of print statements so I could tell when its events were hooked up. And sure enough when I look at lua.log there's those debug statements. But when I added my lua file to my project -- nothing. Modding.log says the file is being loaded but no debug statements.

I've simplified this to as simple a condition as I could. I created an empty mod in ModBuddy and added a single lua file (that is marked as content so that it shows up in the files list in the modinfo). That file consists of the following:
Code:
function JustBeforeTurn(...)
    print("LuaTestFile -- JustBeforeTurn");
end


function StartOfTurn(...)
    print("LuaTestFile -- StartOfTurn");
end

print("LuaTestFile -- Hooking Up Events Start");
Events.TurnBegin.Add( JustBeforeTurn )
Events.PlayerTurnActivated.Add( StartOfTurn )
print("LuaTestFile -- Hooking Up Events Done");

Again, modding.log shows the file being loaded but there's nothing in lua.log. What have I missed?
 
You need to add your Lua file to your project properties. The simplest way, which might work for you, is to go to In-Game Actions and add an AddGameplayScripts item. Don't put any spaces in the ID. Add your file to that. If the default context that runs in won't run all your code then it gets more complicated, but try that first.
 
Well I've certainly got it running, so that's good. But now I'm getting an error I don't know how to fix.

I'm trying to use Player GetTechs CanTriggerBoost and lua.log tells me "Runtime Error: (filepath).lua:22: function expected instead of nil". Looking closely at the Lua Objects doc I see this function is not in the left "IN SCRIPT (<Gameplay Scripts>)" column but in the right "IN CONTEXT(<UserInterface>)" column. I tried putting the lua file under In-Game Actions "AddUserInterfaces" but that didn't seem to do a thing.
 
depending on what you want to do with CanTriggerBoost, there is HasBoostBeenTriggered available in script context.
 
Gedemon you are seriously some sort of Civ Modding wizard! I was able to work around the lack of CanTriggerBoost and use just HasBoostBeenTriggered!

Say, you wouldn't know how to save a custom value to a save game file would you? I see that there is a GameConfiguration.GetValue() but not SetValue(). I tried it anyway, but sure enough SetValue() gives me the old "function expected instead of nil" business. It's not a big deal, really, I'm just trying to preserve a value between the end of one turn and the start of the next. During the normal course of the game, of course, a local variable in the lua file works just fine. And it doesn't matter for user-initiated saves. But those darned Auto-Saves apparently occur after the end of the turn but before the next one has started.
 
Gedemon you are seriously some sort of Civ Modding wizard! I was able to work around the lack of CanTriggerBoost and use just HasBoostBeenTriggered!

Say, you wouldn't know how to save a custom value to a save game file would you? I see that there is a GameConfiguration.GetValue() but not SetValue(). I tried it anyway, but sure enough SetValue() gives me the old "function expected instead of nil" business. It's not a big deal, really, I'm just trying to preserve a value between the end of one turn and the start of the next. During the normal course of the game, of course, a local variable in the lua file works just fine. And it doesn't matter for user-initiated saves. But those darned Auto-Saves apparently occur after the end of the turn but before the next one has started.

without going in something too complex, here is an example:

from ScriptHSD.lua in a script context
Code:
local StartingEra = {}
function GetStartingEra(iPlayer)
    print("------------")
    local key = "StartingEra"..tostring(iPlayer)
    local value = GameConfiguration.GetValue(key)
    print("StartingEra[iPlayer] = "..tostring(StartingEra[iPlayer]))
    print("GameConfiguration.GetValue("..tostring(key)..") = "..tostring(value))
    return StartingEra[iPlayer] or value or 0
end

function SetStartingEra(iPlayer, era)
    LuaEvents.SetStartingEra(iPlayer, era)    -- saved/reloaded
    StartingEra[iPlayer] = era                 -- to keep the value in the current session, GameConfiguration.GetValue in this context will only work after a save/load
end

and from the couple InGameHSD.lua/InGameHSD.xml in an UI context
Code:
function SetStartingEra(iPlayer, era)
    local key = "StartingEra"..tostring(iPlayer)
    print ("saving key = "..key..", value = ".. tostring(era))
    GameConfiguration.SetValue(key, era)
end
LuaEvents.SetStartingEra.Add( SetStartingEra )

and to "save" something manually before the normal saves:
From SaveLoad.lua in another WIP mod
Code:
----------------------------------------------
-- Events for saving
----------------------------------------------
-- This Lua event is called when listing files on the save/load menu
function SaveMyTables()
    print("Calling LuaEvents.SaveTables() on FileListQueryComplete...")
    LuaEvents.SaveTables()
end
LuaEvents.FileListQueryComplete.Add( SaveMyTables )

-- This should happen just before the autosave
function SaveOnBarbarianTurnEnd(playerID)
    local player = Players[playerID]
    if player:IsBarbarian() then
        print("Calling LuaEvents.SaveTables() on Barbarian Turn End...")
        LuaEvents.SaveTables()
    end
end
Events.RemotePlayerTurnEnd.Add( SaveOnBarbarianTurnEnd )


-- This event to handle quick saving
function OnInputAction( actionID )
    if actionID == Input.GetActionId("QuickSave") then
        print("Calling LuaEvents.SaveTables() on QuickSave action...")
        LuaEvents.SaveTables()
    end
end
Events.InputActionTriggered.Add( OnInputAction )
 
Back
Top Bottom