How to fire an event two turns later?

A forum to discuss custom scenarios, campaigns and modding in general.

Moderators: Slitherine Core, The Lordz

Post Reply
Kossatx
Administrative Corporal - SdKfz 232 8Rad
Administrative Corporal - SdKfz 232 8Rad
Posts: 157
Joined: Wed Nov 27, 2013 3:27 pm

How to fire an event two turns later?

Post by Kossatx » Thu Dec 26, 2019 8:04 pm

Hi, I'd like to make an event which fires two turns after it was triggered. Here is the example:

Code: Select all

function SpainJoinsWar()
local italy = game:GetFactionById(8)
local spain = game:GetFactionById(15)
local germany = game:GetFactionById(2)

if GetEvent("SpainJoinsWar") == 0 
	and italy.alliance.id == 2 then 
	local luck = math.random(1,6)
	if luck <= 3 then	
		ChangeFactionAlignment(game:GetFactionById(15), spain.alliance,   math.random(30,50))
		SetEvent("SpainJoinsWar", game.turn)
	elseif luck == 6 then
		ChangeFactionAlignment(game:GetFactionById(15), spain.alliance,  - math.random(30,50))
		SetEvent("SpainJoinsWar", game.turn)
	end	
end
end
I thought if I wrote "SetEvent("SpainJoinsWar", game.turn +2)" it will be fired two turns later but I was wrong :wink:

Robotron
Lieutenant Colonel - Panther D
Lieutenant Colonel - Panther D
Posts: 1285
Joined: Tue Nov 23, 2010 3:35 pm

Re: How to fire an event two turns later?

Post by Robotron » Fri Dec 27, 2019 10:14 pm

Code: Select all

function SpainJoinsWar()
local italy = game:GetFactionById(8)
local spain = game:GetFactionById(15)
local germany = game:GetFactionById(2)

if GetEvent("SpainJoinsWar") == 0 
	and italy.alliance.id == 2 then 
	local luck = math.random(1,6)
	if luck <= 3 then	
		SetFactionAlignment(15, 99.7) -------------------------------------- set Spain's alignment HIGH enough to join entente in 2 turns 

	elseif luck == 6 then
		SetFactionAlignment(15, 0.03) -------------------------------------- set Spain's alignment LOW enough to join CP in 2 turns 
	end	
end
end
Is this good enough for what you want to do?
Slitherine's Commander the Great War on steroids: POTZBLITZ mod!
http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610

Kossatx
Administrative Corporal - SdKfz 232 8Rad
Administrative Corporal - SdKfz 232 8Rad
Posts: 157
Joined: Wed Nov 27, 2013 3:27 pm

Re: How to fire an event two turns later?

Post by Kossatx » Fri Dec 27, 2019 11:09 pm

Thanks for your answer Robotron, but what I mean is once Italy joins CP the event is announced but is two turns later when the dice is thrown. I would like to do these two steps in a single event :roll:

Robotron
Lieutenant Colonel - Panther D
Lieutenant Colonel - Panther D
Posts: 1285
Joined: Tue Nov 23, 2010 3:35 pm

Re: How to fire an event two turns later?

Post by Robotron » Sat Dec 28, 2019 12:01 pm

Okay, normally you should write any consequences from an event into the corresponding function.
Since your SpainJoinsWar event is a direct consequence of the Italian DOW it would belong to the DOWitaly function, no need for a new SpainJoinsWar function.

Problem here: there is no specific function for Italian declaration of war, it is part of the standard DOW event function, so you must insert your stuff there.

Open events.lua then goto function:

Code: Select all

function StandardDOW(attacker, defender)
this is the function that defines what special stuff is happening when nations reach either 0 or 100 alignment value. This is different from the events that define what happens when a nation is attacked by another nation but for your event it does not matter how Italy got to join the war.

Insert into that function what you need, for example:

Code: Select all


local italy = game:GetFactionById(8)

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, 100) 
					SetEvent("SpainJoinsWar", game.turn)      

		elseif luck == 6 then
			SetFactionAlignment(15, 0)  
					  SetEvent("SpainJoinsWar", game.turn)      
		end	
	end
end
This will trigger the event picture for Spanish War declaration on the turn Italy joins the war, in this case pro-CP (note that the text for the Italy DOW event is not referring to what side Italy actually joined!)

2 turns later the dice will be rolled with the chances you defined (50% join Entente, 16,6% join CP).

Don't forget to provide a picture for the event with proper size (414x348 pixels) in PNG format and edit the english.txt file to provide title and text for the event.

Code: Select all

event_SpainJoinsWar_title = Ay, ay, ay, no es bueno!
event_SpainJoinsWar_descr = insert text here
If you forget to provide a picture or text (or get the syntax wrong in english.txt) the game will not start anymore without giving an error message in the logfile!

See ya.
Slitherine's Commander the Great War on steroids: POTZBLITZ mod!
http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610

Kossatx
Administrative Corporal - SdKfz 232 8Rad
Administrative Corporal - SdKfz 232 8Rad
Posts: 157
Joined: Wed Nov 27, 2013 3:27 pm

Re: How to fire an event two turns later?

Post by Kossatx » Sat Dec 28, 2019 6:24 pm

Thanks a lot Robotron, finally I've done the script in this way:

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)

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

		elseif luck == 6 then
			SetFactionAlignment(15, math.random(0,5))  
			SetEvent("SpainJoinsWar3", game.turn)      
		end	
	end
end
But I have a problem. When the dice is thrown, the announcement with the event photo appears twice, sometimes with the same result and sometimes not... I don't know why the photo appears two times :(

Robotron
Lieutenant Colonel - Panther D
Lieutenant Colonel - Panther D
Posts: 1285
Joined: Tue Nov 23, 2010 3:35 pm

Re: How to fire an event two turns later?

Post by Robotron » Sat Dec 28, 2019 7:36 pm

Looks like the the function gets checked several times a turn so "luck" may vary each time per check.
Easiest way might be to de-activate the SpainJoinsWar event after one of the three possibilities were achieved, so it won't be checked anymore.

To de-activate an event function so it is no longer checked you can use

Code: Select all

SetEvent("BLABLABLA", -1)
where "BLABLABLA" is the actual name of the event function of course.

In your case you should use:

Code: Select all

SetEvent("SpainJoinsWar", -1)
after each of the three possible outcome of your event because all further checks of the function depend on event SpainJoinsWar not being -1

Code: Select all

if GetEvent("DOWitaly") == game.turn then
	if GetEvent("SpainJoinsWar") == 0 then
		SetEvent("SpainJoinsWar", game.turn) 
	end     
end
if GetEvent("SpainJoinsWar") >0 
	and GetEvent("DOWitaly") +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 > 3 and luck <= 5 then
			SetEvent("SpainJoinsWar2", game.turn) 
			SetEvent("SpainJoinsWar", -1)

		elseif luck == 6 then
			SetFactionAlignment(15, math.random(0,5))  
			SetEvent("SpainJoinsWar3", game.turn)
			SetEvent("SpainJoinsWar", -1)      
		end	
	end
end
*final edit of this post at 21:12 GMT+1*
Slitherine's Commander the Great War on steroids: POTZBLITZ mod!
http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610

Kossatx
Administrative Corporal - SdKfz 232 8Rad
Administrative Corporal - SdKfz 232 8Rad
Posts: 157
Joined: Wed Nov 27, 2013 3:27 pm

Re: How to fire an event two turns later?

Post by Kossatx » Sat Dec 28, 2019 8:34 pm

Great Robotron! Now it works fine :D Frohe Weihnachten :!:

Post Reply

Return to “Commander the Great War : Mods & Scenario Design”