Add gold to the player?

fb2017

Chieftain
Joined
Oct 8, 2017
Messages
53
I am pretty new to LUA and the modding in general and I was wondering how you could add gold to the player for each trade routes the player has. This is what I have so far (and it dosen't work):

Code:
function CheckTrading(iPlayer)
    local player = Players[iPlayer]
    if not player:IsMinorCiv() and player:IsAlive() then
        local tradeRoutes = player:GetTradeRoutes()
            for id, x in pairs(tradeRoutes) do
                player:ChangeGold (500) -- 500 is just for testing
            end
    end
end

GameEvents.PlayerDoTurn.Add(CheckTrading)
 
Last edited:
check with player:IsMajor(), there is no IsMinorCiv function for player (and if there was, you'd wanted a "no" before it)

and I think (untested, but GetTradeRoutes does not exist AFAIK) you'll have to use player:GetTrade():CountOutgoingRoutes() from a script context (or player:GetTrade():GetNumOutgoingRoutes() from a UI context, but to change gold you need to be in a script context anyway, ho, and use either pPlayer or player, but not both, you've started with "player", stick with it)

finally player:GetTreasury():ChangeGoldBalance(500) instead of player:ChangeGold

check and post your lua.log if it's still not working
 
check with player:IsMajor(), there is no IsMinorCiv function for player (and if there was, you'd wanted a "no" before it)

and I think (untested, but GetTradeRoutes does not exist AFAIK) you'll have to use player:GetTrade():CountOutgoingRoutes() from a script context (or player:GetTrade():GetNumOutgoingRoutes() from a UI context, but to change gold you need to be in a script context anyway, ho, and use either pPlayer or player, but not both, you've started with "player", stick with it)

finally player:GetTreasury():ChangeGoldBalance(500) instead of player:ChangeGold

check and post your lua.log if it's still not working

Well, it doesn't work! And nothing appears in the lua.log! I have used "InGameUIAddin" and added the script, but it doesn't load it, I think!
 
You want a GamePlayScript.

But just to be certain: you're making a mod for civ6 or civ5 ?
 
forget all that I said then, it was for civ6...

Moderator Action: and moved to civ5 C&C
 
Your script works perfectly fine for me. Have you actually started the trade routes when you tested it? Because just having trade units is not enough, they must be moving between cities.

More as a side note, you do not need to loop through the table and can instead just do this:

Code:
local GoldPerTradeRoute = 500

function CheckTrading(iPlayer)
    local player = Players[iPlayer]
    if not player:IsMinorCiv() and player:IsAlive() then
        local tradeRoutes = player:GetTradeRoutes()
        player:ChangeGold (#tradeRoutes * GoldPerTradeRoute) -- # before a table gives the number of objects in it
    end
end
GameEvents.PlayerDoTurn.Add(CheckTrading)
 
# before a table gives the number of objects in it

Only for the portion of the table that conforms to an array. In the case of trade-routes it is OK because Firaxis structures the data into an array.
 
...and I have no idea what that means! :D

All I can say is, so far it has worked fine for me.
 
...and I have no idea what that means! :D

All I can say is, so far it has worked fine for me.

# only works when the table indexes (the k of k, v in ipairs()) start at 1 and increment by 1 each time. Same goes for ipairs; both work through until they find a nil value, which in a 1-based table will be the end but in a non-1-based table that could easily be before your first value.
Example:
Code:
local t = {}
local t2 = {}
table.insert(t, "bee")
table.insert(t, "tree")
t2[1] = "bee"
t2[3] = "tree"

print(#t) -- 2
print(#t2) -- 1

for k, v in ipairs(t) do print(k,v) end -- (1, "bee"), (2, "tree")
for k,v in ipairs(t2) do print(k,v) end -- (1, "bee")
for k,v in pairs(t) do print(k,v) end -- (2, "tree"), (1, "bee") -- afaik pairs iterates in a sorta-random order
for k,v in pairs(t2) do print(k,v) end -- (3, "tree"), (1, "bee") -- afaik pairs iterates in a sorta-random order
 
Hey guys!
Thanks for all your feedback, but interesting enough, this doesn't work for me!
I have made a trade route (as you can see here)
upload_2017-10-9_14-19-23.png

(Big image, I know, but was to lazy to resize it!)
I think the problem may be that civ doesn't load my script, which I have done in Properties > Content.
Lua.log contains just

[535894.531] Initializing Lua 5.1.4

Do you guys know what I've done wrong? (I did the same as Valessa, btw)
I am sorry if this is just SO obvious, but I am a noob when it comes to modding xD
 
Trade Routes are for Cargo Ships and Caravans, not city road-network connections. And also they are for BNW.

Your screenshot is showing you are running either Vanilla or Gods & Kings (but I cannot remember which one looks like that interface-wise)
 
Last edited:
Try:
Code:
local GoldPerConnection = 500

function CheckTrading(iPlayer)
	local player = Players[iPlayer]
	if (player:IsMinorCiv() == false) and (player:IsBarbarian() == false) and player:IsAlive() then
		for city in player:Cities() do
			if (not city:IsCapital() and player:IsCapitalConnectedToCity(city) and not city:IsBlockaded()) then
				player:ChangeGold(GoldPerConnection) 
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(CheckTrading)
Assuming I have not made typos and that the method I am using to check whether the city is connected is valid pre-BNW. I believe it is but I am not 100% sure.

Also, enable logging because it sounds like you may not have already done so. whoward69's enable error logging tutorial
 
T
Try:
Code:
local GoldPerConnection = 500

function CheckTrading(iPlayer)
    local player = Players[iPlayer]
    if (player:IsMinorCiv() == false) and (player:IsBarbarian() == false) and player:IsAlive() then
        for city in player:Cities() do
            if (not city:IsCapital() and player:IsCapitalConnectedToCity(city) and not city:IsBlockaded()) then
                player:ChangeGold(GoldPerConnection)
            end
        end
    end
end
GameEvents.PlayerDoTurn.Add(CheckTrading)
Assuming I have not made typos and that the method I am using to check whether the city is connected is valid pre-BNW. I believe it is but I am not 100% sure.

Also, enable logging because it sounds like you may not have already done so. whoward69's enable error logging tutorial

Thank you! It works!
 
Back
Top Bottom