Adding XP to Unit

sparrow8332

Chieftain
Joined
Nov 23, 2016
Messages
12
Ive been trying to add XP to a selected unit via button click this is what i have so far but no matter what i do i just cant seem to get it to work. Please help

UI file
Code:
function ChangeXP()
    local playerID = Game.GetLocalPlayer()
    local pPlayer = Players[playerID]
    local pUnit = GetSelectedUnit();
    local pUnitXP = pUnit:GetExperience(); 
    local pNewXP = 1000
    if pPlayer:IsHuman() then
        LuaEvents.ChangeUnitXP(playerID, pUnit, pNewXP)
    else end
end


GameplayScript
Code:
function ChangeXP(playerID, pUnit, pNewXP)
    local playerID = Game.GetLocalPlayer()
    local pPlayer = Players[playerID]
    local pUnit = GetSelectedUnit();
    local pUnitXP = pUnit:GetExperience();
    if (pUnit ~= nil) then
        pUnitXP:ChangeExperience(pNewXP)
    else end 
end
 LuaEvents.ChangeUnitXP.Add( ChangeXP )
 
Last edited:
What is showing in lua.log ?

GetSelectedUnit() is a tuner panel function. pUnit therefore is probably always a nil value when you attempt to run your code.

In UI context try as Firaxis does here:
Code:
local pSelectedUnit :table= UI.GetHeadSelectedUnit();
 
oh yea never thought off that thanks :) im a step closer now. Now their isnt any errors in firetuner but it dose nothing to unit :undecide:

when no unis is selected i get this
Code:
Runtime Error: C:\Users\.******\Documents\My Games\Sid Meier's Civilization VI\Mods\1528155583\UI\Panels\CheatMenuWorldTrackerPanel.lua:101: attempt to index a nil value
stack traceback:

When a unit is selected nothing happens and no errors in lua log :-/
 
Last edited:
Is adding XP to a selected unit via a button click even possible ?
Ive tried so many things but nothing seems to be working.
 
Since Button Clicks are UI and Unit:GetExperience():ChangeExperience(Int) is a GameplayScripts method, you're fighting an uphill battle. Attempting to use LuaEvents in order to make communications between the UI Context and the GameplayScripts context is possible, but the problem is there will always be issues of possible lag since the two sides of the lua-engine run on different computer processing threads.

The reason some things work in FireTuner that do not work within an actual mod is that FireTuner is its own hybrid beast with a hybrid lua implementation not shared with the UI or GameplayScripts implementations.

The first solution when lua code is not functioning is to lace your code with print statements to the lua.log so you can better see what data is being processed at what point in the code.

It's been a while since I fooled around with lua events because of this potential lag issue but are you sure you are not attempting to send the data from the GameplayScript to the UI Script and not the other way around ?
 
At first i did think off the lag but when i got gold faith and city population to work i just assumed it must be something else as these communicated fine between the gameplay script and the UI lua.
Im missing something or its how im doing it i this thats a major issue. Still rather new to .lua and ive mainly been looking at how the code works in game to distinguish how mine should look and act.
I would be amazingly grateful if you could not necessary give a working code but show or hit how i may do it myself as you never learn by copy paste from others.
Thanks so far for your advice and input :-)
 
untested, but try this:

UI file
Code:
function ChangeXP()
    local playerID = Game.GetLocalPlayer()
    local pPlayer = Players[playerID]
    local pUnit = UI.GetHeadSelectedUnit();
    local unitID = pUnit:GetID()
    local pNewXP = 1000
    if pPlayer:IsHuman() then
        LuaEvents.ChangeUnitXP(playerID, unitID, pNewXP)
    else end
end


GameplayScript
Code:
function ChangeXP(playerID, unitID, pNewXP)
    local pUnit = UnitManager.GetUnit(playerID, unitID)
    if (pUnit ~= nil) then
        local pUnitXP = pUnit:GetExperience();
        pUnitXP:ChangeExperience(pNewXP)
    else end
end
 LuaEvents.ChangeUnitXP.Add( ChangeXP )

As you want to use an unit function from the gameplay script context, you can't pass and use the unit object you've got using UI.GetHeadSelectedUnit() in the UI context, so you pass the player/unit IDs, you get the unit object in the script context and call the (gameplay context) function to change XP.
 
This No longer works with latest update :-( Please Help - No errors in firetuner and it seems its just not reading from the script file any more since the latest update
 
Back
Top Bottom