Ulixes
Prince
- Joined
- May 2, 2010
- Messages
- 312
While I was working on another code, following (partially) an hint of Bobert13, I made a code that should spawn a building in your cities when you have access to a resource (owned or traded) and delete it when the resource is no more avalaible.
14. function ResourcesSpawnBuildings(playerID)
15. local player = Players[playerID];
16. local pCity = player:GetCityByID(cityID);
17.
18. if(player:GetNumResourceAvailable(GameInfoTypes["RESOURCE_SILK"], true) > 0) then
19. pCity:SetNumRealBuilding(50, 1)
20. end
21. if(player:GetNumResourceAvailable(GameInfoTypes["RESOURCE_SILK"], true) <= 0) then
22. pCity:SetNumRealBuilding(50, 0)
23. end
24. end
25. GameEvents.PlayerDoTurn.Add(ResourcesSpawnBuildings);
(I used, for testing, the Library. "50" is its ID)
The code works as intended, but has two problems:
1) it works only for the capital
(although this can be resolved using xml I would like to understand how to apply it to all cities)
2) Lua.log show this error more times:
Runtime Error: C:\Users\Windows 7\Desktop\Documents\My Games\Sid Meier's Civilization 5\MODS\My - Changes (v 1)\LUA/MyLuaChanges.lua:22: attempt to index local 'pCity' (a nil value)
Another strange thing is that this error disappear if I delete the second 'if' statement (21-23)
So, it give a value to the first 'pCity' but not to the second, why?
Any help would be really appreciated!
Thanks in advance for your time, Ulixes
14. function ResourcesSpawnBuildings(playerID)
15. local player = Players[playerID];
16. local pCity = player:GetCityByID(cityID);
17.
18. if(player:GetNumResourceAvailable(GameInfoTypes["RESOURCE_SILK"], true) > 0) then
19. pCity:SetNumRealBuilding(50, 1)
20. end
21. if(player:GetNumResourceAvailable(GameInfoTypes["RESOURCE_SILK"], true) <= 0) then
22. pCity:SetNumRealBuilding(50, 0)
23. end
24. end
25. GameEvents.PlayerDoTurn.Add(ResourcesSpawnBuildings);
(I used, for testing, the Library. "50" is its ID)
The code works as intended, but has two problems:
1) it works only for the capital
(although this can be resolved using xml I would like to understand how to apply it to all cities)
2) Lua.log show this error more times:
Runtime Error: C:\Users\Windows 7\Desktop\Documents\My Games\Sid Meier's Civilization 5\MODS\My - Changes (v 1)\LUA/MyLuaChanges.lua:22: attempt to index local 'pCity' (a nil value)
Another strange thing is that this error disappear if I delete the second 'if' statement (21-23)
So, it give a value to the first 'pCity' but not to the second, why?
Any help would be really appreciated!
Thanks in advance for your time, Ulixes