I can't figure out why this lua file is not working

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
I can't figure out why this does not work. The log is clean and the mod starts normally... but Sweden and Denmark are neutral in their relations. And the print command is not printing out their names in the log. It does not work whether or not I put the print command in... so it is not that part.


Code:
-- Lua Script2
-- Author: Craig and Nancy
-- DateCreated: 7/26/2012 8:52:58 AM
--------------------------------------------------------------
GameEvents.GetScenarioDiploModifier1.Add(function(ePlayer1, ePlayer2)


	if (ePlayer1 == GameInfo.Civilizations["CIVILIZATION_DENMARK"].ID and ePlayer2 == GameInfo.Civilizations["CIVILIZATION_SWEDEN"].ID) or  (ePlayer2 == GameInfo.Civilizations["CIVILIZATION_DENMARK"].ID and ePlayer1 == GameInfo.Civilizations["CIVILIZATION_SWEDEN"].ID) then
		
		print(ePlayer1:GetName())
		print(pPlayer2:GetName())
		
		return -50
	else
		return 0
	end
end)

Advice would be appreciated. Thank-you.
 
the lua log should output an error on the print statement (ePlayer1 is not an object, and pPlayer2 is not defined)

ePlayer1 and ePlayer2 are player ID, not civilization ID. You first need to get the player object using pPlayer1 = Players[ePlayer1], then the civilization ID using player:GetCivilizationType() (Firaxis naming at work...)
 
the lua log should output an error on the print statement (ePlayer1 is not an object, and pPlayer2 is not defined)

But due to the mistake in the condition of comparing playerId with civId the prints will never be executed, so will never generate an error (ain't dynamic languages ****)
 
Well, I've continued to work on this, and I believe I am making progress...

Thus far,

Code:
GameEvents.GetScenarioDiploModifier1.Add(function(ePlayer1, ePlayer2)

for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1 do 
    local pPlayer = Players[iPlayer]
    if (pPlayer:IsAlive()) then

	print("This is for pPlayer")
	print(pPlayer:GetName())


		function GetCivID(civType)
			local civ = GameInfo.Civilizations[civType];
			if(civ) then
				return civ.ID;
			else
				return -1;
			end
		end

		local playerCiv = pPlayer:GetCivilizationType();
		if(playerCiv == GetCivID("CIVILIZATION_DENMARK")) then

			local ePlayer1 = pPlayer;
	
		print("This is for ePlayer1")
		print(ePlayer1:GetName())

	
		elseif(playerCiv == GetCivID("CIVILIZATION_SWEDEN")) then

			local ePlayer2 = pPlayer;

		print("This is for ePlayer2")
		print(ePlayer2:GetName())



			if (ePlayer1 == GameInfo.Civilizations["CIVILIZATION_DENMARK"] and ePlayer2 == GameInfo.Civilizations["CIVILIZATION_SWEDEN"]) then
		
		return -50
	else
		return 0
		end
	end
	end
end
	end)

And I get the following in my log file at the end:

Spoiler :

[16119.130] Diplomacy: This is for pPlayer
[16119.130] Diplomacy: Harald Bluetooth
[16119.130] Diplomacy: This is for ePlayer1
[16119.146] Diplomacy: Harald Bluetooth
[16119.146] Diplomacy: This is for pPlayer
[16119.146] Diplomacy: OLAF
[16119.146] Diplomacy: This is for pPlayer
[16119.146] Diplomacy: TURLOUGH
[16119.146] Diplomacy: This is for pPlayer
[16119.146] Diplomacy: ANGUS
[16119.146] Diplomacy: This is for pPlayer
[16119.162] Diplomacy: CLOVIS
[16119.162] Diplomacy: This is for pPlayer
[16119.162] Diplomacy: RIDERCH
[16119.162] Diplomacy: This is for pPlayer
[16119.162] Diplomacy: WITTEKIND
[16119.162] Diplomacy: This is for pPlayer
[16119.162] Diplomacy: William
[16119.162] Diplomacy: This is for pPlayer
[16119.177] Diplomacy: BORU
[16119.177] Diplomacy: This is for pPlayer
[16119.177] Diplomacy: Kenny
[16119.177] Diplomacy: This is for pPlayer
[16119.177] Diplomacy: Alfred
[16119.177] Diplomacy: This is for pPlayer
[16119.177] Diplomacy: PENDA
[16119.177] Diplomacy: This is for pPlayer
[16119.193] Diplomacy: OSWALD
[16119.193] Diplomacy: This is for pPlayer
[16119.193] Diplomacy: Elisedd
[16119.193] Diplomacy: This is for pPlayer
[16119.193] Diplomacy: Haakon
[16119.193] Diplomacy: This is for pPlayer
[16119.193] Diplomacy: ERIK
[16119.208] Diplomacy: This is for ePlayer2
[16119.208] Diplomacy: ERIK
[16119.411] Diplomacy: This is for pPlayer
[16119.411] Diplomacy: Harald Bluetooth
[16119.411] Diplomacy: This is for ePlayer1
[16119.411] Diplomacy: Harald Bluetooth
[16119.411] Diplomacy: This is for pPlayer
[16119.411] Diplomacy: OLAF
[16119.411] Diplomacy: This is for pPlayer
[16119.411] Diplomacy: TURLOUGH
[16119.427] Diplomacy: This is for pPlayer
[16119.427] Diplomacy: ANGUS
[16119.427] Diplomacy: This is for pPlayer
[16119.427] Diplomacy: CLOVIS
[16119.427] Diplomacy: This is for pPlayer
[16119.427] Diplomacy: RIDERCH
[16119.427] Diplomacy: This is for pPlayer
[16119.442] Diplomacy: WITTEKIND
[16119.442] Diplomacy: This is for pPlayer
[16119.442] Diplomacy: William
[16119.442] Diplomacy: This is for pPlayer
[16119.442] Diplomacy: BORU
[16119.442] Diplomacy: This is for pPlayer
[16119.442] Diplomacy: Kenny
[16119.442] Diplomacy: This is for pPlayer
[16119.458] Diplomacy: Alfred
[16119.458] Diplomacy: This is for pPlayer
[16119.458] Diplomacy: PENDA
[16119.458] Diplomacy: This is for pPlayer
[16119.458] Diplomacy: OSWALD
[16119.458] Diplomacy: This is for pPlayer
[16119.458] Diplomacy: Elisedd
[16119.458] Diplomacy: This is for pPlayer
[16119.474] Diplomacy: Haakon
[16119.474] Diplomacy: This is for pPlayer
[16119.474] Diplomacy: ERIK
[16119.474] Diplomacy: This is for ePlayer2
[16119.474] Diplomacy: ERIK
[16119.474] TechTree: REFRESHING TECH DISPLAY
[16119.489] Diplomacy: This is for pPlayer
[16119.489] Diplomacy: Harald Bluetooth
[16119.489] Diplomacy: This is for ePlayer1
[16119.505] Diplomacy: Harald Bluetooth
[16119.505] Diplomacy: This is for pPlayer
[16119.505] Diplomacy: OLAF
[16119.505] Diplomacy: This is for pPlayer
[16119.505] Diplomacy: TURLOUGH
[16119.505] Diplomacy: This is for pPlayer
[16119.505] Diplomacy: ANGUS
[16119.520] Diplomacy: This is for pPlayer
[16119.520] Diplomacy: CLOVIS
[16119.520] Diplomacy: This is for pPlayer
[16119.520] Diplomacy: RIDERCH
[16119.520] Diplomacy: This is for pPlayer
[16119.520] Diplomacy: WITTEKIND
[16119.520] Diplomacy: This is for pPlayer
[16119.520] Diplomacy: William
[16119.536] Diplomacy: This is for pPlayer
[16119.536] Diplomacy: BORU
[16119.536] Diplomacy: This is for pPlayer
[16119.536] Diplomacy: Kenny
[16119.536] Diplomacy: This is for pPlayer
[16119.536] Diplomacy: Alfred
[16119.536] Diplomacy: This is for pPlayer
[16119.536] Diplomacy: PENDA
[16119.552] Diplomacy: This is for pPlayer
[16119.552] Diplomacy: OSWALD
[16119.552] Diplomacy: This is for pPlayer
[16119.552] Diplomacy: Elisedd
[16119.552] Diplomacy: This is for pPlayer
[16119.552] Diplomacy: Haakon
[16119.552] Diplomacy: This is for pPlayer
[16119.552] Diplomacy: ERIK
[16119.567] Diplomacy: This is for ePlayer2
[16119.567] Diplomacy: ERIK


This indicates to me that I am getting the appropriate ePlayer1 and ePlayer2 (Harald is the Denmark Leader while Erik is the Swedish one).

I'm not certain why they are not being taken the one step further to do the diplomatic function at the end... I play as Denmark and check Sweden, and they are simply neutral (no additional diplomatic text, red or green, shows up).

It occurs to me that I haven't played as Sweden... I should check from that end, although I do not think it will make a difference.

I think, likely, my output in ePlayer1 and ePlayer2 is still not of the proper type.

If not, I don't know. I've tried a couple of other methods as well:

Code:
GameEvents.GetScenarioDiploModifier1.Add(function(ePlayer1, ePlayer2)

for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1 do 
    local pPlayer = Players[iPlayer]
    if (pPlayer:IsAlive()) then

	print(pPlayer:GetName())

      if (pPlayer:GetCivilizationType() == GameInfo.Civilizations["CIVILIZATION_DENMARK"]) then
		local pPlayer=ePlayer1

		print(ePlayer1:GetName())
				
		elseif (pPlayer:GetCivilizationType() == GameInfo.Civilizations["CIVILIZATION_SWEDEN"]) then
			local pPlayer=ePlayer2

		print(ePlayer2:GetName())
		
	if (ePlayer1 == GameInfo.Civilizations["CIVILIZATION_DENMARK"] and ePlayer2 == GameInfo.Civilizations["CIVILIZATION_SWEDEN"]) then
		
		return -50
	else
		return 0
		end
		end
		end
	end
end)

Returned my whole list of civ leader three times, identically, as indicated by the log. I think the code previous on this page was closer to succeeding.

Anyhow, I'm still looking at the various scenario files to dig out what I can. Any further advice would be appreciated.

Thanks.
 
I think the code previous on this page was closer to succeeding.

yes :D

Try that:

Code:
-- Lua Script2
-- Author: Craig and Nancy
-- DateCreated: 7/26/2012 8:52:58 AM
--------------------------------------------------------------
GameEvents.GetScenarioDiploModifier1.Add(function(ePlayer1, ePlayer2)

	local iCiv1 = Players[ePlayer1]:GetCivilizationType()
	local iCiv2 = Players[ePlayer2]:GetCivilizationType()

	local iDenmark = GameInfo.Civilizations["CIVILIZATION_DENMARK"].ID
	local iSweden = GameInfo.Civilizations["CIVILIZATION_SWEDEN"].ID

	if (iCiv1 == iDenmark and iCiv2 == iSweden) or  (iCiv2 == iDenmark and iCiv1 == iSweden) then
		
		print(Players[ePlayer1]:GetName())
		print(Players[ePlayer2]:GetName())
		
		return -50
	else
		return 0
	end
end)
 
Just tested for Sweden... interesting result:

Again, Denmark maintained its neutral attitude. I find it interesting, though, that the lua file seems to have run when I opened the diplomatic screen. So something is happening, just not certain what. I think, I may be getting a return of "0" rather than the dipl0matic part of the code not working at all.... although that a "0" is what would result if: if (ePlayer1 == GameInfo.Civilizations["CIVILIZATION_DENMARK"] and ePlayer2 == GameInfo.Civilizations["CIVILIZATION_SWEDEN"]) then was not true, which could be for any number of reasons.

Spoiler :

[17066.790] Diplomacy: This is for pPlayer
[17066.790] Diplomacy: ERIK
[17066.790] Diplomacy: This is for ePlayer2
[17066.790] Diplomacy: ERIK
[17066.852] TechTree: REFRESHING TECH DISPLAY
[17066.868] Diplomacy: This is for pPlayer
[17066.868] Diplomacy: ERIK
[17066.868] Diplomacy: This is for ePlayer2
[17066.868] Diplomacy: ERIK
[17103.044] Advisors: Marking DIPLOMATIC_OVERVIEW_SCREEN as seen
[17104.651] Diplomacy: This is for pPlayer
[17104.651] Diplomacy: ERIK
[17104.667] Diplomacy: This is for ePlayer2
[17104.667] Diplomacy: ERIK
[17104.667] Diplomacy: This is for pPlayer
[17104.667] Diplomacy: ERIK
[17104.667] Diplomacy: This is for ePlayer2
[17104.667] Diplomacy: ERIK
[17104.667] LeaderHeadRoot: Handling LeaderMessage: 0, Let us begin.
 
Completely untested, try

Code:
GameEvents.GetScenarioDiploModifier1.Add(function(ePlayer1, ePlayer2)
  -- This is what we are trying to achieve
  -- if ePlayer1 is Denmark (or Sweden) and ePlayer2 is Sweden (or Denmark), return -50 otherwise 0

  -- We don't want player ids, we need civ ids
  local eCiv1 = Players[ePlayer1]:GetCivilizationType()
  local eCiv2 = Players[ePlayer2]:GetCivilizationType()

  if ((eCiv1 == GameInfoTypes.CIVILIZATION_DENMARK and eCiv2 == GameInfoTypes.CIVILIZATION_SWEDEN) or 
      (eCiv1 == GameInfoTypes.CIVILIZATION_SWEDEN and eCiv2 == GameInfoTypes.CIVILIZATION_DENMARK)) then
    return -50
  else
    return 0
  end
end)
 
I feel invisible right now :D
 
Code:
GameEvents.GetScenarioDiploModifier1.Add(function(ePlayer1, ePlayer2)

[COLOR="Red"]for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1 do 
    local pPlayer = Players[iPlayer]
    if (pPlayer:IsAlive()) then[/COLOR]
SNIP
	end)

As you already have two known player IDs the code in red is completely un-necessary. Why loop over all players when you already have the two you are interested in in hand?

Try writing in "pseudo code comments" and then translating into lua, eg

Code:
GameEvents.GetScenarioDiploModifier1.Add(function(ePlayer1, ePlayer2)
  -- If (ePlayer1 is Denmark or Sweden) then
    -- If (ePlayer2 is Sweden or Denmark) then
      -- return -50
    -- end
  -- end
  -- return 0
end)
 
Amazing... I worked, literally, for hours on the two bits I showed here... and you did it in a matter of minutes. Excellent and thank-you.

Just so you know, Sweden still shows as neutral even though the text associated with the function shows up and is green. Not certain why he would not show up as friendly. perhaps the bonus needs to be higher to achieve that, can't recall. I'll do further testing.

Another question... can this be iterated. There are only 3 different diplomatic functions:

GetScenarioDiploModifier1
GetScenarioDiploModifier2
GetScenarioDiploModifier3

I'm hoping that these can be used with many different pairs of civilizations. I'll do some work on that later.

Again, thank-you (although I had hoped to succeed myself, somehow).
 
Just realized I got two great responses. I used Gedemon's first, but will check out the Whoward's next... likely with no differences in the result.

The extra code is coming from my not having much of a clue as to what I am doing :). I look through the scenarios and pick what looks good or what worked in the past... not the best method I know, but I'm learning incrementally.

Just discovered how to paste into firetuner :).
 
Thank-you for the advice about commenting, too. I had a vague idea what I was trying to do from some of Gedemon's advice, but i never outlined it as you did, and kind of lost track of the order of things in my mind... I'm not certain I was achieving the goals elicited from your advice. I think a more studied approach will do me better... and writing comments as you mentioned, will lead to a more orderly process.
 
Back
Top Bottom