[LUA] Free +%

RampantGeth

Chieftain
Joined
Aug 15, 2014
Messages
13
Hey again fanatics; I need a quick spot of help if you have a second!

I need a way to give +10% production of melee units to a civ, pretty sure I need a LUA code to sort through all the cities of the player to give them a dummy building, but I don't know enough code to pull it off.

I'm also open to other options if there is a better way to get it functioning, though.

Thanks in advance everyone!
 
If you meant as a Unique Ability, then the quick way to do so would be to use Lua to give your chosen Civ a free dummy policy that increases the production of melee units (the policy itself would use Policy_UnitCombatProductionModifiers.... refer to Warrior Code from the Honor Tree as a template).
 
Perfect, That's what I thought would be easiest! But i'm still really new to LUA, how do i tell the game to search through the players Cities?

I think I know how to add the building and stuff, but I've only got Bane_'s code that he gave me in a previous threads code to play around with and he only ref's the Capital.
 
Perfect, That's what I thought would be easiest! But i'm still really new to LUA, how do i tell the game to search through the players Cities?

Code:
for [B][COLOR="Blue"]pCity[/COLOR][/B] in [B][COLOR="Green"]pPlayer[/COLOR]:[COLOR="Red"]Cities()[/COLOR][/B] do
...

This is the iterator that you will use in Lua in order to loop over a given player's Cities.

I've highlighted a few sections for you.

'pCity' in blue is the variable that we are going to store each incremental result from our loop into. This name can be different depending on what notation you use (or it could be 'puppies' for all Lua cares) but it should be able to tell you what you're looking for. In this case, we're looking for what's called the City object -- it's a table that the game uses to store everything about a particular City.

If pCity is the City object, then pPlayer in green is the Player object. Just like the City object, the Player object is a table that contains all the details about a particular player.

Then we get to the Cities() part in red. This is the method that we are calling, but if you notice, we have a colon between pPlayer and Cities() -- this is effectively telling the game to run a function on that player object.

Functions you may notice typically have the parentheses around them. They can either be empty, or have several arguments in them, which are passed into the function to use.

In this case, let's say that a player has 3 Cities.

The above code would then loop repeatedly, and each time pCity would be different.

On the first loop, pCity would contain the data for City#1.
On the second loop, pCity now contains the data for City#2.
On the final loop, pCity contains the data for CIty#3.

At each loop, you need to have code that tells your function what you want to do to each City, and it will try to apply those same instructions to every City it goes through.

One more note:
pPlayer I mentioned was the player object -- a table that stores all of the information for a given player.

But.. which player? This is where iPlayer comes into play -- this would be the player ID.

In a normal, single-player game, the human player is usually Player 1. However, internally, player 1's ID number is 0.

In order to pick the right player object, we need to know which player you are interested in. This is accomplished by assigning something to the pPlayer variable.

Code:
pPlayer = [B][COLOR="Blue"]Players[[/COLOR][COLOR="Red"]iPlayer[/COLOR][COLOR="Blue"]][/COLOR][/B]

Again, pPlayer can be whatever you want, but if you, for example, change it to cats, then your code from above needs to read cats:Cities() instead of pPlayer:Cities().

In this case, all the players' information is stored in a table simply called Players.
We are looking through this Players table, using the bracket syntax, to find the data whose table index matches our player ID number. Lua then adds a reference to this data with the name of the variable you chose (pPlayer in our case.)

Of course, this still leaves a problem -- what is iPlayer?
Well, you'll need to figure that one out as you begin your function, but sometimes this data is given to you by the game.

One of the most frequently used event hooks is GameEvents.PlayerDoTurn(). This event gives you the iPlayer ID of whatever player's turn is running. All you need to do is define your function to utilize this argument.

Code:
function MyFunctionName()
function MyFunctionName(iPlayer)

These two define a function called MyFunctionName, but the first one is not built to rely on any arguments, while the second one relies on the presence of iPlayer.

If all else fails, Game.GetActivePlayer() will usually return the ID of the human player, and you can use that to define iPlayer.

Hope that helps.
 
This doesn't even need Lua. You can set the FreeBuilding field of the leader's Trait to be the desired dummy building, and it will work without a hitch.

:bowdown: I can't believe I didn't realize that! Awesome!

Thanks Darkscythe for walking me through the LUA, I'm sure that'll come in handy at some point!

As always, you guys never fail to impress!
 
Back
Top Bottom