Page 1 of 1

DOW and WarEffort

Posted: Sun Dec 29, 2019 9:22 pm
by Kossatx
Hi again, I'm trying to write an script to set the WarEffort after a DOW. I have write this one in the function "PhaseStart(alliance)", but it don't works. Is necessary to write an specific event for DOWspain?

Code: Select all

	if GetEvent("DOWspain") == game.turn then
 	  SetWarEffort(15, 60)
    end

Re: DOW and WarEffort

Posted: Sun Dec 29, 2019 10:28 pm
by Robotron
Looks good to me. What makes you think it does not work?

Note that war effort can only be changed from turn 2 onward.

Re: DOW and WarEffort

Posted: Sun Dec 29, 2019 11:04 pm
by Kossatx
Well, I've written the script as I said but there is no setting for the 60% WarEffort when the turn of the DOWspain begins. I don't know If I have to do any with "DOWspain" event, I haven't written any "DOWspain" event because I think isn't necessary :|

Re: DOW and WarEffort

Posted: Sun Dec 29, 2019 11:59 pm
by Robotron
You mean there is no report window for raised war effort at the end of turn?

This only will show if the nation in question is controlled by the player.

Here's a way you can check whether the war effort was set correctly:

Code: Select all

if GetEvent("DOWspain") == game.turn then
 	  SetWarEffort(15, 60)
end
local spain = game:GetFactionById(15)
LogUi("SPANISH WAR EFFORT IS SET TO: " .. spain.luaData.warEffort
Reload the game and press end turn (end the game turn).
At the start of the next turn open the logfile and search for "SPANISH WAR EFFORT IS SET TO:" in the logfile
There should be the number value printed there which you set before: 60.

You can use LogUi("INSERT TEXT" .. variable) to print values for any variable to the logfile using the above method. This is helpful to control if things are processed as planned.

Re: DOW and WarEffort

Posted: Mon Dec 30, 2019 9:52 am
by Kossatx
I've written this in function "PhaseStart(alliance)", in Potzblitz.lua file (as you know I only play your mod with a few modifications). But nothing happens when Spain allies with player's faction. There is no reduction to 60% warEffort, and there is no report from "LogUi" :|

Code: Select all

	if GetEvent("DOWspain") == game.turn then
 	  SetWarEffort(15, 60)
    end
	local spain = game:GetFactionById(15)
	LogUi("SPANISH WAR EFFORT IS SET TO: " .. spain.luaData.warEffort)
I suppose the reference to "DOWspain" event don't need to write any specific event :?:

Re: DOW and WarEffort

Posted: Mon Dec 30, 2019 1:18 pm
by Robotron
Oh, now I understand...

You cannot do checks for stuff happening on the SAME gameturn in PhaseStart(alliance) because PhaseStart(alliance) goes BEFORE all events, so the check for

Code: Select all

GetEvent("EXAMPLE") == game.turn
will never pass and that's the reason why the value for war effort has never changed.

What you could do is either put SetWarEffort(15, 60) directly into your event in function StandardDOW(attacker, defender)

Code: Select all

if GetEvent("DOWitaly") == game.turn then
	SetEvent("SpainJoinsWar", game.turn)      
end
if GetEvent("SpainJoinsWar") >0 
	and GetEvent("SpainJoinsWar") +2 == game.turn then
	if italy.alliance.id == 2 then 
		local luck = math.random(1,6)
		if luck <= 3 then	
			SetFactionAlignment(15, math.random(95,100)) 
			SetEvent("SpainJoinsWar1", game.turn)
			SetWarEffort(15, 60)

		elseif luck > 3 and luck <= 5 then
			SetEvent("SpainJoinsWar2", game.turn)
			SetWarEffort(15, 60) 

		elseif luck == 6 then
			SetFactionAlignment(15, math.random(0,5))  
			SetEvent("SpainJoinsWar3", game.turn)
			SetWarEffort(15, 60)      
		end	
	end
end

or, if you absolutely want to use PhaseStart(alliance) then do a check for an event that lies in the PAST like for example:

Code: Select all

if GetEvent("SpainJoinsWar") >0 
	and GetEvent("SpainJoinsWar") +2 ==  game.turn then
		SetWarEffort(15, 60)      
end
This WILL be checked because the event lies 2 turns in the past.

Note that any events that have not yet happened, and also events that do not even exist, have a value of 0 so you can NOT just check for

Code: Select all

if GetEvent("SpainJoinsWar") +2 ==  game.turn then
		SetWarEffort(15, 60)      
end
because then the event would fire on turn 2 (0+2 = 2), that's why there's an additional check in the example for

Code: Select all

GetEvent("SpainJoinsWar") >0 


Fascinating stuff, eh? :mrgreen:

Re: DOW and WarEffort

Posted: Mon Dec 30, 2019 8:12 pm
by Kossatx
I enjoy a lot with your masterclasses :D Finally I think the better solution is, as you say, to write the script in "StandardDOW" function in events.lua file. But in order to include the 60% war Effort in any DOWspain, I have written it in these two different scripts:

Code: Select all

if GetEvent("DOWitaly") == game.turn then
	SetEvent("SpainJoinsWar", game.turn)      
end
if GetEvent("SpainJoinsWar") >0 
	and GetEvent("SpainJoinsWar") +2 == game.turn then
	if italy.alliance.id == 2 then 
		local luck = math.random(1,6)
		if luck <= 3 then	
			SetFactionAlignment(15, math.random(95,100)) 
			SetEvent("SpainJoinsWar1", game.turn)
			SetEvent("SpainJoinsWar", -1)

		elseif luck <= 5 then
			SetEvent("SpainNeutral", game.turn) 
			SetEvent("SpainJoinsWar", -1)

		elseif luck == 6 then
			SetFactionAlignment(15, math.random(0,5))  
			SetEvent("SpainJoinsWar2", game.turn) 
			SetEvent("SpainJoinsWar", -1)			
		end	
	end
end

if GetEvent("DOWspain") == game.turn then
	SetWarEffort(15, 60)     
end
It now works fine, thanks a lot Robotron :!:

Re: DOW and WarEffort

Posted: Mon Dec 30, 2019 8:17 pm
by Kossatx
In order to close definitively the event, I must fix this last script in events.lua file:

Code: Select all

function SpainWarEffort()
if GetEvent("DOWspain") > 0
and spain.luaData.warEffort < 100 then
	local luck = math.random(1,12)
	if luck <= 12 then
	if spain.alliance.id == 1 then
	spain.luaData.warEffort = spain.luaData.warEffort +	10
	france.ConsumeProductionPoints(5)
	britain.ConsumeProductionPoints(10)
	elseif spain.alliance.id == 2 then
	spain.luaData.warEffort = spain.luaData.warEffort +	10
	germany.ConsumeProductionPoints(15)
	end
	end
end
end
It has an error I don't know how to fix:

Code: Select all

[19:22:37][11348]game/game_events.lua:14646(global SpainWarEffort) game/game_events.lua:14646: attempt to index global 'spain' (a nil value)
Any idea Robotron?

Re: DOW and WarEffort

Posted: Mon Dec 30, 2019 8:59 pm
by Robotron
I will translate the relevant parts of the error message for you so you will know in the future because this error is very common.

Code: Select all

[19:22:37][11348]game/game_events.lua:14646(global SpainWarEffort) game/game_events.lua:14646: attempt to index global 'spain' (a nil value)
1. There was a problem in script: game/game_events.lua

2. the error occurred in function SpainWarEffort()

3. the error is in line 14646

4.

Code: Select all

attempt to index global 'spain' (a nil value)
this means:

there is a reference to a variable called "spain"

Code: Select all

and spain.luaData.warEffort < 100 then
which was not defined before, you forgot to do first:

Code: Select all

local spain = game:GetFactionById(15)
A variable that was not defined before has no value/content, it does not exist, it is "nil" (nothing).
The script tried to compare "spain" (which is nil) with 100.
But you can't compare 100 with nil and because of that the crash happened.

Always remember to first define all variables you will need at the start of every new function or when you introduce new stuff into existing functions.


P.S.:

Code: Select all

local luck = math.random(1,12)
	if luck <= 12 then
		(...)
	end
This is a test that will always succeed, because 12 is the highest possible random number you have defined before with math,random(1,12).
Try math.random(1,20).

Re: DOW and WarEffort

Posted: Mon Dec 30, 2019 10:18 pm
by Kossatx
Great Robotron, it already works ok :D

Code: Select all

function SpainWarEffort()
local spain = game:GetFactionById(15)
local germany = game:GetFactionById(2)
if spain.alliance.id ~= 0
and spain.luaData.warEffort < 100 then
	local luck = math.random(1,12)

	if luck == 12 then
	if spain.alliance.id == 1 then
	spain.luaData.warEffort = spain.luaData.warEffort +	10
	france:ConsumeProductionPoints(5)
	britain:ConsumeProductionPoints(10)
	SetEvent("SpainWarEffort", game.turn)
	elseif spain.alliance.id == 2 then
	spain.luaData.warEffort = spain.luaData.warEffort +	10
	germany:ConsumeProductionPoints(15)
	SetEvent("SpainWarEffort", game.turn)
	end
	end
end
end
The reason of "if luck <= 12" was because it was the modified script for testing the event, an I forgot to change it before paste it :roll: If you come to Barcelona I must invite you the beers you want, you have helped me a lot :D