[LUA] Use a return by a LUA-function in a new function

martijnaikema

Warlord
Joined
Oct 30, 2010
Messages
111
Hi,

I have a basic LUA-question. I have a LUA-function "CalculateTraitGold" which returns two values:

return iPlayerID
return iTraitGold

Say I have a new function "UpdateTraitGold" which needs to add the iTraitGold to the player with ID iPlayerID. How do I need to set this function?

Shoud it be:

function UpdateTraitGold ()
CalculateTraitGold()
iPlayerID:ChangeGold (iTraitGold)
end

And if I set the Serial Events, which function(s) should I add? I guess only the UpdateTraitGold-function? Because it calls the CalculateTraitGold-function?
 
If you are returning 2 values you should do it in a single statement. i.e.
Code:
function CalculateTraitGold()
 -- whatever
 return iPlayerID, iTraitGold
end
Then the other function would look something like
Code:
function UpdateTraitGold()
 local iPlayerID, iTraitGold = CalculateTraitGold()
 Players[iPlayerID]:ChangeGold(iTraitGold)
end
I would agree that UpdateTraitGold() would be the function to add as the event handler.
 
I tried this but I can´t get it to work.

Here are both functions. Maybe you can have a look at what I am doing wrong

Spoiler :

function CalculateTraitGold()

iTraitGold = 0
iCivsTotal = Game:CountCivTeamsAlive()

for index,mPlayer in pairs(Players) do
if mPlayer:IsAlive() then
if (mPlayer:IsMinorCiv() or mPlayer:IsBarbarian()) then
else
if mPlayer:GetCivilizationAdjectiveKey() == "TXT_KEY_CIV_DUTCH_ADJECTIVE" then
iPlayerTraitID = mPlayer:GetID()

if iCivsTotal > 24 then
iTraitGold = 1
elseif iCivsTotal < 13 then
iTraitGold = 3
else
iTraitGold = 2
end

iEra = mPlayer:GetCurrentEra()
if iEra < 2 then
iMultiplyGold = 1
elseif iEra > 3 then
iMultiplyGold = 3
else
iMultiPlyGold = 2
end

iTraitGold = iTraitGold * iMultiplyGold

iCivsMet = Teams[mPlayer:GetTeam()]:GetHasMetCivCount()
iCivsAtWar = Teams[mPlayer:GetTeam()]:GetAtWarCount()
iCivsAtPeace = iCivsMet - iCivsAtWar

iTraitGold = iTraitGold * iCivsAtPeace

print (iPlayerTraitID)
print (iTraitGold)
return iPlayerTraitID, iTraitGold


end
end
end
end

end -- function


And the function to use the returns:

Spoiler :

function UpdateTraitGold ()
local iPlayerTraitID, iTraitGold = CalculateTraitGold()

print (iPlayerTraitID)
print (iTraitGold)

Players[iPlayerTraitID]:ChangeGold(iTraitGold)
end


And the Event Handler:
Spoiler :

Events.SerialEventGameDataDirty.Add(UpdateTraitGold);
Events.SerialEventTurnTimerDirty.Add(UpdateTraitGold);
Events.SerialEventCityInfoDirty.Add(UpdateTraitGold);

UpdateTraitGold();
 
I now noticed the runtime error in FireTuner:
Runtime Error: [string "Assets/UI/InGame.lua"]:1063: attemp to index local 'addinFile' (a nil value)
Runtime Error: Error loading Assets/UI/Ingame/InGame.lua

Here are my Mod Buddy setting

2 files:
GoldTrait.lua - tried both Import into VFS true and false
GoldTrait.xml

Actions: -
Content: InGameUIAddin - GoldTrait.lua

I am not using any subfolders in the MOD
 
Try re-entering the content line. VFS has to be set to true for both files
 
Tried it but still the same error. I still need the InGameUIAddin in the Content-tab right?

Edit: I even tried a simple test.lua file to lead. It only contained print ("Hello World").

I got the same error :S
 
That sounds a bit drastic. I would try creating a new mod first, with a hello world lua script just for testing.

I know :)

But I did test it in a new MOD with the "Hello World" print. Still the same error. Also tried to clear the Cache of the game, but nothing seems to work.

It seems like, judging to the error, that the game is trying to add a file that does not exist. I also saw robk was having the same error in this thread:
http://forums.civfanatics.com/showthread.php?t=403062

But I can't relate his sollution to my problem.
 
Well I got that error, too, but it disappeared after I set the VFS flag to true. Meh
 
Back
Top Bottom