1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

Few lua lines

Discussion in 'Civ5 - SDK / LUA' started by Krajzen, Jan 30, 2014.

  1. Krajzen

    Krajzen Deity

    Joined:
    Oct 23, 2013
    Messages:
    2,517
    Location:
    Poland
    Ok, so I started learning lua today :p

    I want to create Chinese alternate UA, Mandate of Heaven:

    "Each time you gain 10 citizen in cities or settle a new city, receive science and culture boost; Great Generals are stronger. If happiness drops below zero, Mandate is lost and rebels start appearing. -20% strenght against barbarians."

    I figured out part with "tenth citizen", thanks to Leugi's Aymara which has something similiar ;) and I guess (PlayerCityFounded) is proper command for "new city settled" ; also I want to experiment with science/culture boost a bit.
    However I have two other questions about lua:
    1) How make something like that:

    if X or Y

    then Z

    2) How to make rebels appearing below 0 happiness (only for China) and -20% against rebels (only for China)? I didn't find any mods with something similar, something dealing with barbs.
     
  2. Machiavelli24

    Machiavelli24 Mod creator

    Joined:
    May 9, 2012
    Messages:
    818
    Even with lua certain things are easy to change while other things are essentially fixed. Identifying when a city's population changes, or a new city is founded are both easy with lua. There are events for both of those things (but there are a few similarly named events and only specific ones work correctly). Giving out lump sum culture is also easy.

    Changing the benefit Great Generals provide in the way you describe is not really feasible in lua.

    For your "if x or y" question, do you mean how to use conditional statements?
    Code:
    if ([i]condition[/i]) then
      ...stuff...
    else
      ...other stuff...
    end
    As for making rebels appear, you would need to fake it. Spawn new barbarian units in yourself when the specific conditions are met. It is possible but a bit tricky and something you may want to as your third bit of lua rather than part of your first.
     
  3. Krajzen

    Krajzen Deity

    Joined:
    Oct 23, 2013
    Messages:
    2,517
    Location:
    Poland
    ...Well I meant, I would simply leave current Chinese great generals bonus without any changes:lol:

    Thanks for that, but in the meanwhile I figured it out and my ability is more and more advanced :p

    Currently, each time Chinese city is build or gains 10 citizens, China receives culture boost equal to half culture needed for the next policy.

    Now, I would like it also to give China science boost - either "equal to half science needed for the next technology" (it would be perfect one) or "equal to 10 turns of player's science output" (similar to great scientist ability). But I am seeking and seeking and I cannot find line which would do that :/

    I was trying Player:GetScienceFromCitiesTimes100() but it didn't work :/


    Also, what should I do to have China having -20% in unit strenght against barbarians? I tried "obvious" one here, Player:SetBarbarianCombatBonus(-20), but this one also didn't work ;)
     
  4. Krajzen

    Krajzen Deity

    Joined:
    Oct 23, 2013
    Messages:
    2,517
    Location:
    Poland
    Uhm... Anyone knows how to code some kind of science boost in lua?

    EDIT
    For barbarians one, I tried that code:

    function Mandate(iPlayer)
    local pPlayer = Players[iPlayer]
    if (pPlayer:IsAlive()) then
    if (pPlayer:GetCivilizationType() == GameInfo.Civilizations["CIVILIZATION_CHINA"].ID) then
    pPlayer:ChangeBarbarianCombatBonus(-20)
    end
    end
    end

    I tried also: pPlayer:GetBarbarianCombatBonus(-20) and pPlayer:SetBarbarianCombatBonus(-20) but they don't work. I simply don't know which of these ones modify combat strenght against barbs :p

    For science one, i tried

    TeamTechs.ChangeResearchProgress (Civ5 API)
    TeamTechs.ChangeResearchProgressPercent (Civ5 API)

    And few other, they simply didn't work. How do you, advanced modders, figure out which of lua lines reference to the things you are interested in? :p
     
  5. Pazyryk

    Pazyryk Deity

    Joined:
    Jun 13, 2008
    Messages:
    3,584
    These should be correct. Does your code define "TeamTechs"?

    local player = Players[iPlayer]
    local team = Teams[player:GetTeam()]
    local teamTechs = team:GetTeamTechs()
    teamTechs:ChangeResearchProgress(GameInfoType.TECH_XXXXX, int, iPlayer)

    Or pPlayer, pTeam, pTeamTechs, if you pPrefer...
     
  6. Krajzen

    Krajzen Deity

    Joined:
    Oct 23, 2013
    Messages:
    2,517
    Location:
    Poland
    Thanks!

    So, to sum up, the proper code is



    Code:
    function Mandate(pPlayer)
        local pPlayer = Players[pPlayer]
        if (pPlayer:IsAlive()) then
            if (pPlayer:GetCivilizationType() == GameInfo.Civilizations["CIVILIZATION_CHINA"].ID) then
                local pNumFounded = load(pPlayer, "NumFounded")
                if pNumFounded == nil then
                pNumFounded = 0
                end
                if pNumFounded < 2 then
                [I]local player = Players[pPlayer]
                local team = Teams[player:GetTeam()]
                local teamTechs = team:GetTeamTechs()
                teamTechs:ChangeResearchProgressPercent(GameInfoType.TECH _XXXXX, int, pPlayer)[/I]
                pNumFounded = pNumFounded
                save(pPlayer, "NumFounded", pNumFounded)
                end
            end
        end
    end
    
    GameEvents.PlayerCityFounded.Add(Mandate)
    
    ?
    I guess no, because I simply don't know how to set Percent of that tech :lol:
    Could you fix my code so it will be responsible for "when you settle a city, receive research progress equal to half of the current technology cost"? (I hope this works that way, if so - I will be proud of my correctly found TeamTechs ;) )


    Also I would love to receive any tip about this barbarian thing - I see that nobody ever did this, nad giving my difficulties with coding that, I am not surprised :lol: (to be honest, it seems for me that only few people can mod this game in lua - most of the civs in workshop use XML abilities, even the top ones ;) )
     
  7. Pazyryk

    Pazyryk Deity

    Joined:
    Jun 13, 2008
    Messages:
    3,584
    Ouch! There are mistakes on almost every line here. I'm not going to be much help here, since I'm not willing to spend the hour to write the code from scratch.

    First, you really need print statements all over the place. No matter how good you think you are, everyone makes mistakes and how will you know without some print statements? How do you even know the code is running at all?

    Mandate should have 3 integer arguments that follow the pattern here. Don't call an integer pPlayer because that's just horribly confusing. "pSomething" generally refers to an object, not an integer. Most coders would call it iPlayer or playerID. Assuming you use iPlayer, the next line would be:

    pPlayer = Players[iPlayer]

    or

    player = Players[iPlayer]

    Again, it's totally up to you whether you want to call the player object "pPlayer" or "player". But do make up your mind. You have player and pPlayer all over the place. This is an object, so many modders would use pPlayer (I happen to prefer player).

    For pNumFounded, you are again using a variable name that implies an object rather than an integer. It's terribly confusing. I'm guessing from context that this is supposed to be an integer. Call it iNumFounded. And don't just change it in one place. I don't want to be blamed in the next iteration when you are now using iNumFounded and pNumFounded.

    I can't help with these two lines:

    local pNumFounded = load(pPlayer, "NumFounded")
    save(pPlayer, "NumFounded", pNumFounded)

    "load" and "save" are not standard Lua functions, so these must be defined somewhere else in the mod's code.


    Some other lines that don't make sense...
    pNumFounded = pNumFounded
    What are you trying to do with this line?
     
  8. Krajzen

    Krajzen Deity

    Joined:
    Oct 23, 2013
    Messages:
    2,517
    Location:
    Poland
    Lol, surprisinly it seems that I have to learn lua in order to mod in lua :lol:

    Thanks xd
     

Share This Page