[vanilla] Execute code if a unit stands on a luxury resource.

fb2017

Chieftain
Joined
Oct 8, 2017
Messages
53
I'm trying to give the player horses if an unique unit is standing on a Deer plot.
This is my code right now:
Code:
local samiLeader = "LEADER_FEB_LARS"
function scoutUnit(iPlayer)
    local player = Players[iPlayer]
    if GameInfo.Leaders[player:GetLeaderType()].Type == GameInfo.Leaders[samiLeader].Type then
        for unit in player:Units() do
            if unit:GetUnitType() == GameInfoTypes["UNIT_FEB_SCOUT"] then
                local plot = unit:GetPlot()
                if plot:GetResourceType() == GameInfo.Resources["RESOURCE_DEER"].Type then
                    player:ChangeNumResourceTotal(GameInfo.Resources["RESOURCE_HORSE"], 2)
                    print("Standing on a deer")
                end
            end
        end
    end
end
GameEvents.PlayerDoTurn.Add(scoutUnit)

It doesn't work!

Thanks in advance!
 
Code:
if plot:GetResourceType() == GameInfo.Resources["RESOURCE_DEER"].Type then
This line will not work, as pPlot:GetResourceType() will return the ID of the Deer Resource and not the type. I.e. it returns 9 instead of 'RESOURCE_DEER'.
Change it to GameInfo.Resources["RESOURCE_DEER"].ID

Code:
player:ChangeNumResourceTotal(GameInfo.Resources["RESOURCE_HORSE"], 2)
This also won't work since the first paramter of pPlayer:ChangeNumResourceTotal(..) requires the ID of the resource, and not a table tuple. I.e. it should be GameInfo.Resources["RESOURCE_HORSE"].ID

Code:
if GameInfo.Leaders[player:GetLeaderType()].Type == GameInfo.Leaders[samiLeader].Type then
While this should work it is horribly inefficient. You could reduce it to: if player:GetLeaderType() == GameInfoTypes.LEADER_FEB_LARS then
It's also possible to check for the CivilizationType instead of LeaderType. I.e. if player:GetCivilizationType() == GameInfoTypes.CIVILIZATION_FEB_SAMI then


Note that there is no limit on this functionality, so if your UU would station on a Deer resource for 10 turns you'd get 20 horses!
 
Code:
if plot:GetResourceType() == GameInfo.Resources["RESOURCE_DEER"].Type then
This line will not work, as pPlot:GetResourceType() will return the ID of the Deer Resource and not the type. I.e. it returns 9 instead of 'RESOURCE_DEER'.
Change it to GameInfo.Resources["RESOURCE_DEER"].ID

Code:
player:ChangeNumResourceTotal(GameInfo.Resources["RESOURCE_HORSE"], 2)
This also won't work since the first paramter of pPlayer:ChangeNumResourceTotal(..) requires the ID of the resource, and not a table tuple. I.e. it should be GameInfo.Resources["RESOURCE_HORSE"].ID

Code:
if GameInfo.Leaders[player:GetLeaderType()].Type == GameInfo.Leaders[samiLeader].Type then
While this should work it is horribly inefficient. You could reduce it to: if player:GetLeaderType() == GameInfoTypes.LEADER_FEB_LARS then
It's also possible to check for the CivilizationType instead of LeaderType. I.e. if player:GetCivilizationType() == GameInfoTypes.CIVILIZATION_FEB_SAMI then


Note that there is no limit on this functionality, so if your UU would station on a Deer resource for 10 turns you'd get 20 horses!

Thanks for the response!

I'm planning to delete the luxury resource when you've recieved the horses or maybe make an array that remembers which luxury resource you've stood on.
 
Code:
if plot:GetResourceType() == GameInfo.Resources["RESOURCE_DEER"].Type then
This line will not work, as pPlot:GetResourceType() will return the ID of the Deer Resource and not the type. I.e. it returns 9 instead of 'RESOURCE_DEER'.
Change it to GameInfo.Resources["RESOURCE_DEER"].ID

Code:
player:ChangeNumResourceTotal(GameInfo.Resources["RESOURCE_HORSE"], 2)
This also won't work since the first paramter of pPlayer:ChangeNumResourceTotal(..) requires the ID of the resource, and not a table tuple. I.e. it should be GameInfo.Resources["RESOURCE_HORSE"].ID

Code:
if GameInfo.Leaders[player:GetLeaderType()].Type == GameInfo.Leaders[samiLeader].Type then
While this should work it is horribly inefficient. You could reduce it to: if player:GetLeaderType() == GameInfoTypes.LEADER_FEB_LARS then
It's also possible to check for the CivilizationType instead of LeaderType. I.e. if player:GetCivilizationType() == GameInfoTypes.CIVILIZATION_FEB_SAMI then


Note that there is no limit on this functionality, so if your UU would station on a Deer resource for 10 turns you'd get 20 horses!
Is it even possible to delete a luxury resource? I tried out the Plot:SetResourceType() and set the value to 0, but that didn't work!
 
"0" is Iron

"-1" is required to cancel all resources on a plot.

If you put any value greater than "-1" for the resource-type, you also need to give at least a resource amount of "1" or greater.
Code:
Plot:SetResourceType(iResourceID, iResourceNum)
There's also a possible third argument for "bForMinorCivPlot" which is a boolean that defaults to "false" but I've never used it.

And if there is an improvement on the plot you really need to cancel the improvement before cancelling the resource in order for everything to "register" properly with the rest of the game's systems.
 
Last edited:
"0" is Iron

"-1" is required to cancel all resources on a plot.

If you put any value greater than "-1" for the resource-type, you also need to give at least a resource amount of "1" or greater.
Code:
Plot:SetResourceType(iResourceID, iResourceNum)

And if there is an improvement on the plot you really need to cancel the improvement before cancelling the resource in order for everything to "register" properly with the rest of the game's systems.
Ty
 
Back
Top Bottom