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

Discussion in 'Civ5 - Creation & Customization' started by martijnaikema, Dec 14, 2010.

  1. martijnaikema

    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?
     
  2. MouseyPounds

    MouseyPounds Prince

    Joined:
    Nov 8, 2010
    Messages:
    417
    Location:
    Maryland, USA
    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.
     
  3. martijnaikema

    martijnaikema Warlord

    Joined:
    Oct 30, 2010
    Messages:
    111
    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();
     
  4. martijnaikema

    martijnaikema Warlord

    Joined:
    Oct 30, 2010
    Messages:
    111
    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
     
  5. alpaca

    alpaca King of Ungulates

    Joined:
    Aug 3, 2006
    Messages:
    2,322
    Try re-entering the content line. VFS has to be set to true for both files
     
  6. martijnaikema

    martijnaikema Warlord

    Joined:
    Oct 30, 2010
    Messages:
    111
    For both the xml and the lua file?
     
  7. alpaca

    alpaca King of Ungulates

    Joined:
    Aug 3, 2006
    Messages:
    2,322
    Yes, if you use the xml at all.
     
  8. martijnaikema

    martijnaikema Warlord

    Joined:
    Oct 30, 2010
    Messages:
    111
    I use it for the TopPanel. I did not include the code cause it is not finished yet.
     
  9. martijnaikema

    martijnaikema Warlord

    Joined:
    Oct 30, 2010
    Messages:
    111
    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
     
  10. alpaca

    alpaca King of Ungulates

    Joined:
    Aug 3, 2006
    Messages:
    2,322
    Yes. I don't know what's up, something like this works fine for me after I set VFS to true.
     
  11. martijnaikema

    martijnaikema Warlord

    Joined:
    Oct 30, 2010
    Messages:
    111
    I might do a re-install of the game then
     
  12. alpaca

    alpaca King of Ungulates

    Joined:
    Aug 3, 2006
    Messages:
    2,322
    That sounds a bit drastic. I would try creating a new mod first, with a hello world lua script just for testing.
     
  13. martijnaikema

    martijnaikema Warlord

    Joined:
    Oct 30, 2010
    Messages:
    111
    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.
     
  14. alpaca

    alpaca King of Ungulates

    Joined:
    Aug 3, 2006
    Messages:
    2,322
    Well I got that error, too, but it disappeared after I set the VFS flag to true. Meh
     
  15. martijnaikema

    martijnaikema Warlord

    Joined:
    Oct 30, 2010
    Messages:
    111
    I did a re-install of the game and the SDK. Got it working now.
     

Share This Page