• Our friends from AlphaCentauri2.info are in need of technical assistance. If you have experience with the LAMP stack and some hours to spare, please help them out and post here.

Should this code be working?

kingchris20

Wisdom Seeker
Joined
Jul 30, 2012
Messages
1,343
Location
Resident of Heaven; Currently in the Waiting Room
Can someone let me know if something is wrong with either of these codes?

I have a building and I need this code to check for 1,000 energy. if the player has 1,000 energy or more in reserve, the building should be added to the city.

I know next to nothing of LUA, the 2nd code is from AgressiveWimp, and the first code is the same, but changed up and based on some of Ryika's LUA.

Code:
function CheckStargateUSAF(pPlayer)
	local ActivePlayer = Players[pPlayer]
	if ActivePlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_STARGATE_USAF"] then
		-- print("ZPM Activation at 1,000 Energy")
		for iCity in Players[i]:Cities() do
			if (ActivePlayer:GetEnergy() >= 1000) then
				-- print("Activating Shield of Atlantis!)
				city:SetNumRealBuilding(GameInfoTypes["BUILDING_SHIELD_OF_ATLANTIS"], 1) 
			else
				city:SetNumRealBuilding(GameInfoTypes["BUILDING_SHIELD_OF_ATLANTIS"], 0)
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(CheckStargateUSAF)

I have also tried it this way to no avail...

Code:
GameEvents.PlayerDoTurn.Add(
function(iPlayer)
	local pPlayer = Players[iPlayer]
	if not (pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_STARGATE_USAF) then return end
	for pCity in pPlayer:Cities() do
		if (pPlayer:GetEnergy() >= 1000) then
			pCity:SetNumRealBuilding(GameInfoTypes.BUILDING_SHIELD_OF_ATLANTIS, 1) 
		else
			pCity:SetNumRealBuilding(GameInfoTypes.BUILDING_SHIELD_OF_ATLANTIS, 0)
		end
	end
end)
 
OffTopic first:
I'd first check for the energy threshold and then loop through all cities because that number can't change during the check anyway. That way you only need to check for the Energy threshold once, the way you're doing it it checks the energy threshold for each and every city. That's a really minor thing, but still.

To the actual code itself (first version only) - This line will produce an error:
Code:
for iCity in Players[i]:Cities() do
...because you have not defined a variable "i". The variable you're using is pPlayer.

So you have two possible solutions:
Code:
for iCity in Players[pPlayer]:Cities() do

Code:
for iCity in ActivePlayer:Cities() do

You're also making the same error with 'city' a bit lower in the code, you have not defined that variable, you have defined iCity.

So the corrected (but untested, there may be more errors that I've missed) code should be:
Code:
function CheckStargateUSAF(pPlayer)
	local ActivePlayer = Players[pPlayer]
	if ActivePlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_STARGATE_USAF"] then
		-- print("ZPM Activation at 1,000 Energy")
		for iCity in Players[pPlayer]:Cities() do
			if (ActivePlayer:GetEnergy() >= 1000) then
				-- print("Activating Shield of Atlantis!)
				iCity :SetNumRealBuilding(GameInfoTypes["BUILDING_SHIELD_OF_ATLANTIS"], 1) 
			else
				iCity :SetNumRealBuilding(GameInfoTypes["BUILDING_SHIELD_OF_ATLANTIS"], 0)
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(CheckStargateUSAF)

You should either use firetuner (a tool which can be found in your SDK-Folder, use google for a quick setup guide) or lua.log found in ocuments\my games\Sid Meier's Civilization Beyond Earth\Logs to debug errors. Without these tools working with lua will be extremely annoying.
 
Thanks for the response Ryika!

I'm not seeing anything in the Lua.log or Firetuner about the lua file-----not seeing any of the print commands.

These are the entry points found in the modinfo file, I think I am making the mod "see" the file correctly....

Code:
<EntryPoints>
    <EntryPoint type="PersonalityTraits" file="LUA/HammondPersonalityTrait.lua">
      <Name>HammondPersonalityTrait</Name>
      <Description>
      </Description>
    </EntryPoint>
    [B]<EntryPoint type="InGameUIAddin" file="LUA/ShieldOfAtlantis.lua">
      <Name>ShieldOfAtlantis</Name>[/B]
      <Description>
      </Description>
    </EntryPoint>
  </EntryPoints>
 
The print statements are all commented out in the excerpt you posted. Which would be why you don't see any messages in the logs.
 
The print statements are all commented out in the excerpt you posted. Which would be why you don't see any messages in the logs.

Yup, that was silly :mischief:. Okay, I added a Print statement to show that the script loaded, and that showed up in the FireTuner log. but still no luck on the code giving the building to me when I hit 1,000 energy...
 
PlayerDoTurn only triggers at the beginning of a turn so if you get past the 1000 Energy in the middle of the turn you'll need to end the turn and see whether you get the building at the beginning of the next turn.

If that doesn't help and the code shows no errors just add some more print-statements between steps to see where exactly the code "stops working". Can't spot any obvious error though, so make sure your building-code is actually loaded properly as well. (Or maybe I'm just blind)
 
I'm using IGEBERT to help test this. I will see that I am making +3 Energy per turn, so I will give myself 997 energy and hit next turn. That gives me 1,000, then I will click next turn a couple more times to see if the building shows up and it never does.

I can add the building through IGEBERT and it functions correctly once I have the building.

The only Print Statement I ever see is "Shield of Atlantis Script Loaded".

Code:
--========================================================================================
-- Initialization
--========================================================================================

[B][U]print("Shield of Atlantis Script loaded.")[/U][/B]  ---- [B][U][COLOR="YellowGreen"]I See this Print Statement at the beginning of Turn 1 (just before turn 1, right after the game loads up)[/COLOR][/U][/B]
	
--========================================================================================
-- Function
--========================================================================================

function CheckStargateUSAF(pPlayer) ---- [B][U][COLOR="RoyalBlue"](Maybe I can add a print statement here to see if it shows up?)[/COLOR][/U][/B]
	local ActivePlayer = Players[pPlayer]
	if ActivePlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_STARGATE_USAF"] then
		[B][U]print ("ZPM Activation at 1,000 Energy")[/U][/B] ---- [B][U][COLOR="YellowGreen"]Haven't seen this statement in the logs[/COLOR][/U][/B]
		for iCity in Players[pPlayer]:Cities() do
			if (ActivePlayer:GetEnergy() >= 1000) then
				[B][U]print ("Activating Shield of Atlantis!")[/U][/B] --- [B][U][COLOR="YellowGreen"]And subsequently, haven't seen this one either...[/COLOR][/U][/B]
				iCity :SetNumRealBuilding(GameInfoTypes["BUILDING_SHIELD_OF_ATLANTIS"], 1) 
			else
				iCity :SetNumRealBuilding(GameInfoTypes["BUILDING_SHIELD_OF_ATLANTIS"], 0)
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(CheckStargateUSAF)
 
Added a couple more Print Statements...

Code:
--========================================================================================
-- Initialization
--========================================================================================

print("Shield of Atlantis Script loaded.")
	
--========================================================================================
-- Function
--========================================================================================

function CheckStargateUSAF(pPlayer)
	[B][U]print ("Checking Code Functionality")[/U][/B] ---- ADDED
	local ActivePlayer = Players[pPlayer]
	[B][U]print ("Checking Code Functionality 2")[/U][/B] ---- ADDED
	if ActivePlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_STARGATE_USAF"] then
		print ("ZPM Activation at 1,000 Energy")
		for iCity in Players[pPlayer]:Cities() do
			if (ActivePlayer:GetEnergy() >= 1000) then
				print ("Activating Shield of Atlantis!")
				iCity :SetNumRealBuilding(GameInfoTypes["BUILDING_SHIELD_OF_ATLANTIS"], 1) 
			else
				iCity :SetNumRealBuilding(GameInfoTypes["BUILDING_SHIELD_OF_ATLANTIS"], 0)
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(CheckStargateUSAF)

I am seeing these 2 print statements appear correctly, checking each player between the turn.

Code:
 StationQuestManager.OnPlayerDoTurn(playerType) player 1
 Checking Code Functionality
 Checking Code Functionality 2
 StationQuestManager.OnPlayerDoTurn(playerType) player 2
 Checking Code Functionality
 Checking Code Functionality 2
 StationQuestManager.OnPlayerDoTurn(playerType) player 3
 Checking Code Functionality
 Checking Code Functionality 2
 StationQuestManager.OnPlayerDoTurn(playerType) player 4
 Checking Code Functionality
 Checking Code Functionality 2
 StationQuestManager.OnPlayerDoTurn(playerType) player 5
 Checking Code Functionality
 Checking Code Functionality 2
 StationQuestManager.OnPlayerDoTurn(playerType) player 62
 Checking Code Functionality
 Checking Code Functionality 2
 StationQuestManager.OnPlayerDoTurn(playerType) player 63
 Checking Code Functionality
 Checking Code Functionality 2
 Aliens: SpawnTableEntry:  AlienType: 48 PercentChance: 45
 Aliens: SpawnTableEntry:  AlienType: 12 PercentChance: 30
 Aliens: Found alien type to spawn. Type: 12 SpawnTable: table: 182296B0
 StationQuestManager.OnPlayerDoTurn(playerType) player 0
 Checking Code Functionality
 Checking Code Functionality 2


So it is failing here:

Code:
if ActivePlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_STARGATE_USAF"] then
		print ("ZPM Activation at 1,000 Energy")

And that is definitely my Civilization name:

Code:
<GameData>
	<Civilizations>
		<Row>
			<Type>CIVILIZATION_STARGATE_USAF</Type>
			<Description>TXT_KEY_CIV_STARGATE_USAF_DESC</Description>
			<ShortDescription>TXT_KEY_CIV_STARGATE_USAF_SHORT_DESC</ShortDescription>
			<Adjective>TXT_KEY_CIV_STARGATE_USAF_ADJECTIVE</Adjective>
			<CivilopediaTag>TXT_KEY_CIV_STARGATE_USAF</CivilopediaTag>
			<DefaultPlayerColor>PLAYERCOLOR_STARGATE_USAF</DefaultPlayerColor>
			<ArtDefineTag>ART_DEF_CIVILIZATION_AMERICA</ArtDefineTag>
			<ArtStyleType>ARTSTYLE_EUROPEAN</ArtStyleType>
			<PortraitIndex>0</PortraitIndex>
			<IconAtlas>STARGATE_USAF_COLOR_ATLAS</IconAtlas>
			<AlphaIconAtlas>STARGATE_USAF_ALPHA_ATLAS</AlphaIconAtlas>
			<GradientIconAtlas>STARGATE_USAF_GRADIENT_ATLAS</GradientIconAtlas>
			<GlowIconAtlas>STARGATE_USAF_GLOW_ATLAS</GlowIconAtlas>
			<MapImage/>
			<ArtStyleSuffix>_EURO</ArtStyleSuffix>
			<ArtStylePrefix>EUROPEAN </ArtStylePrefix>
			<IntroductionQuote>TXT_KEY_CIV_INTRO_STARGATE_USAF_TEXT%</IntroductionQuote>
			<IntroductionAudio>AS2D_DOM_SPEECH_STARGATE</IntroductionAudio>
			<Personality>PERSONALITY_STARGATE_USAF</Personality>
		</Row>
	</Civilizations>
 
I tried this short version:

Code:
function CheckStargateUSAF(pPlayer) 
	local ActivePlayer = Players[pPlayer]
	print(ActivePlayer:GetCivilizationType()) -- to check whether those are actually the same
	print(GameInfoTypes["CIVILIZATION_ARC"]) -- to check whether those are actually the same
	if ActivePlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_ARC"] then
		print ("ZPM Activation at 1,000 Energy")
	end
end
GameEvents.PlayerDoTurn.Add(CheckStargateUSAF)

...and it worked perfectly fine:

Code:
 StationQuestManager.OnPlayerDoTurn(playerType) player 0
 0
 0
 ZPM Activation at 1,000 Energy

So try this code and see what it gives you:

Code:
function Check(pPlayer) 
	local ActivePlayer = Players[pPlayer]
	print(ActivePlayer:GetCivilizationType()) 
	print(GameInfoTypes["CIVILIZATION_STARGATE_USAF"]) 
	if ActivePlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_STARGATE_USAF"] then
		print ("ZPM Activation at 1,000 Energy")
	end
end
GameEvents.PlayerDoTurn.Add(Check)

If that doesn't work then there's something wrong with the Civilization... or you're simply not playing as that Civilization. :mischief:
 
Uh-oh, got this

Code:
 StationQuestManager.OnPlayerDoTurn(playerType) player 0
 Checking Code Functionality
 Checking Code Functionality 2
 15
 nil
 
Ok, just tested it with this code:

Code:
function CheckStargateUSAF(pPlayer)
	print ("Checking Code Functionality")
	local ActivePlayer = Players[pPlayer]
	print ("Checking Code Functionality 2")
	print(ActivePlayer:GetCivilizationType()) 
	print(GameInfoTypes["CIVILIZATION_ARC"])
	if ActivePlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_ARC"] then
		print ("ZPM Activation at 1,000 Energy")
		for iCity in Players[pPlayer]:Cities() do
			if (ActivePlayer:GetEnergy() >= 1000) then
				print ("Activating Shield of Atlantis!")
				iCity :SetNumRealBuilding(GameInfoTypes["BUILDING_CYTONURSERY"], 1) 
			else
				iCity :SetNumRealBuilding(GameInfoTypes["BUILDING_CYTONURSERY"], 0)
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(CheckStargateUSAF)

I played as ARC and the code worked perfectly, I got a cytonursery after I reached 1,000 energy and it took it back away when I dropped below 1,000 energy.

So the LUA code is not recognizing my custom civ nor my custom building.

Yet they both work in the game otherwise...
 
Alright, so I did the "start the game, back out to the main menu, and restart the game" trick and now the code is recognizing my Stargate Civ and my Shield of Atlantis building, and functioning correctly.

Is there a way to get around this issue?

Make the LUA recognize custom additions without having to reload the game?
 
Back
Top Bottom