P.S. A step-by-step explanation of what everything does would be most appreciated!
A complete step-by-step would be well beyond anything I (or anyone else) would have time to do as an organized whole, but this thread is a decent intro to how lua scripts are structured:
http://forums.civfanatics.com/showthread.php?t=533853
As a first step in creating an lua script you have to know what game-core 'event' will be best to use to run your code under. In Civ5, lua scripts need to be subscribed to one event at the least. The most common event is the one called PlayerDoTurn, which runs for every player in sequence (including City-States and Barbarians) when the "NEXT TURN" button is pressed. See here for a list of the most-commonly-used 'GameEvents' that lua scripts can be subscribed to:
http://modiki.civfanatics.com/index.php?title=GameEvents_(Civ5_Type) This a sub-page from the basic modding lua reference in the forum's modwiki:
http://modiki.civfanatics.com/index.php/Lua_and_UI_Reference_(Civ5)
So, If I want to create a function that runs from the PlayerDoTurn event, my basic script has to look something like this
Code:
function PlayerTurnProcessing(iPlayer)
--we want to do something in this function related to player turn processing
print(iPlayer)
end
GameEvents.PlayerDoTurn(PlayerTurnProcessing)
This would print the player ID# for active players in the game for each player still alive as each player's turn is processed by the game.
'iPlayer' is a variable I created to take the parameter that is passed by the core game to any code subscribed to the PlayerDoTurn 'Game Event'. I could just as easily have called it 'Cheeseburgers' instead of 'iPlayer'
The value that will be passed will be an assigned player ID# made when the current game is set-up. In single-player games, the human player (regardless of the civilization they are playing as) is always PlayerID# 0, and the Barbs are always PlayerID# 63. Major players (including the human player) use Player ID#s 0 - 21, City-States use #s 22 - 62. But if there are only three major civilizations set-up to be used in a game, they will use only Player ID#s 0, 1, and 2. (City-States will still use #22 - #62, Barbs will still use #63).
Once you know what game event you need to use, there are still a boatload of 'methods' that have to be looked-at to determine if they are what you need to get information from the game into your lua script so you can manipulate it. The big ones most-commonly-used are
So, going back to the previous bit of code, if I only want to print the ID#s for players that are
not the Barbarians, I do as will follow, but as a 1st action I need to translate the PlayerID# the game sends to the PlayerDoTurn event into a specific player 'object' or 'pointer', so that we are pulling the information for a specific player rather than just the generic 'Player' that is shown in all the Player methods, like so:
Code:
local pPlayer = Players[iPlayer]
- Players is a meta-table (for lack of better description) that Firaxis provides as part of the Civ5 lua, and which holds all player information for all players in the current game, indexed by the player ID#.
- The code just shown (local pPlayer = Players[iPlayer]) creates a localized variable that holds all the 'specific player' information we will need to be able to access, so our overall code can thus become
Code:
function PlayerTurnProcessing(iPlayer)
local pPlayer = Players[iPlayer]
--we want to do something in this function related to player turn processing
print(iPlayer)
end
GameEvents.PlayerDoTurn(PlayerTurnProcessing)
I have not done anything else yet with sorting as to whether or not this player is the Barbarians. There is a player method that can tell us this by returning a true/false boolean as to whether the specific player is the Barbarians:
http://modiki.civfanatics.com/index.php?title=Player.IsBarbarian_(Civ5_API)
So I just do a check for whether the player is the Barbarians, and exclude them from the 'print' line:
Code:
function PlayerTurnProcessing(iPlayer)
local pPlayer = Players[iPlayer]
--we want to do something in this function related to player turn processing
if not pPlayer:IsBarbarian() then
print(iPlayer)
end
end
GameEvents.PlayerDoTurn(PlayerTurnProcessing)
By itself, even in placed in an lua-file within your mod's project, the code won't do anything because you also need to tell modbuddy how to activate this lua-file. It needs to be set up as an InGameUIAddin entry in the 'Content' tab of modbuddy, and the setting for Import Into VFS should be 'false'. See
whoward69's what ModBuddy setting for what file types tutorial for more detailed information on that. Once the file is properly set-up as an InGameUIAddin, every time the human presses the 'NEXT TURN' button, the code will run for every player still alive in numerical sequence, and print their player ID# into the game's lua.log file. But the Barbarians will be excluded, so you should never see '63' being printed in the lua.log.
Hope that at least is a good start.