Create periodic or one shot timers with gameticks.

Modding, Map Editor, IES Scripting and Other Tutorials
Post Reply
User avatar
Loewenherz
Posts: 244
Joined: 23 Sep 2017, 17:26
Has thanked: 7 times
Been thanked: 21 times

Create periodic or one shot timers with gameticks.

Post by Loewenherz »

Yesterday i have search after a Method to to actively measure the game time for scripts and to create gametime. The Thing is, that Mad Doc have use for his Scripts a special Int, called "cTimerInt" and "cPeriodicInt" which set a single Timer or evaluate for set a trigger periodicaly secunds. Without Description i dident have work with this special Ints. There dident have work in my .ies script, but i have found thanks to Mona a other Method, which is very exactly and work wounderful. Thanks to this you can chanche in a script the perdiodically evaluation or set a simple timer for a script.

Here is the Script:

Code: Select all

	
	
////////////////////////////////////////////////////////////
// INITIALIZATION 
////////////////////////////////////////////////////////////
	EveryMinute = 18.00;
	EveryThirtySeconds = 9.00; 
	EveryTenSeconds = 3.00;   
	EveryFiveSeconds = 1.50;
	EveryThreeSeconds = 0.90;
	EveryTwoSeconds = 0.60;
	EverySecond = 0.30;
	
////////////////////////////////////////////////////////////
// Get the Gametime in Ticks for Triggers and Effects
////////////////////////////////////////////////////////////

RULE CountPeriodicInt
  if (true)
    then actionPeriodicInt
END_RULE
ACTION actionPeriodicInt
  SCRIPT WORLD
	EveryMinute  = EveryMinute - 0.01;
	EveryThirtySeconds   = EveryThirtySeconds - 0.01;
	EveryTenSeconds   = EveryTenSeconds - 0.01;
	EveryFiveSeconds  = EveryFiveSeconds - 0.01;
	EveryThreeSeconds   = EveryThreeSeconds - 0.01;
	EveryTwoSeconds   = EveryTwoSeconds - 0.01;
	EverySecond  = EverySecond - 0.01;
  END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EveryMinute
////////////////////////////////////////////////////////////
RULE SemaphorePeriodicInt1
  if (EveryMinute <= 0) 
    then actionSemaphorePeriodicInt1
END_RULE
ACTION actionSemaphorePeriodicInt1
SCRIPT WORLD
EveryMinute = 18.00;
bEveryMinute = true;
DoDelay(1);
STEP
bEveryMinute = false;
END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EveryThirtySeconds
////////////////////////////////////////////////////////////

RULE SemaphorePeriodicInt2
  if (EveryThirtySeconds <= 0)
    then actionSemaphorePeriodicInt2
END_RULE
ACTION actionSemaphorePeriodicInt2
SCRIPT WORLD
EveryThirtySeconds = 9.00;
bEveryThirtySeconds = true;
DoDelay(1);
STEP
bEveryThirtySeconds = false;
END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EveryTenSeconds
////////////////////////////////////////////////////////////

RULE SemaphorePeriodicInt3 
  if (EveryTenSeconds <= 0)  
    then actionSemaphorePeriodicInt3
END_RULE
ACTION actionSemaphorePeriodicInt3
SCRIPT WORLD
EveryTenSeconds = 3.00;
bEveryTenSeconds = true;
DoDelay(1);
STEP
bEveryTenSeconds = false;
END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EveryFiveSeconds
////////////////////////////////////////////////////////////

RULE SemaphorePeriodicInt4 
  if (EveryFiveSeconds <= 0)
    then actionSemaphorePeriodicInt4
END_RULE
ACTION actionSemaphorePeriodicInt4
SCRIPT WORLD
EveryFiveSeconds = 1.50;
bEveryFiveSeconds = true;
DoDelay(1);
STEP
bEveryFiveSeconds = false;
END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EveryThreeSeconds
////////////////////////////////////////////////////////////

RULE SemaphorePeriodicInt5
  if (EveryThreeSeconds <= 0)
    then actionSemaphorePeriodicInt5
END_RULE
ACTION actionSemaphorePeriodicInt5
SCRIPT WORLD
EveryThreeSeconds = 0.90;
bEveryThreeSeconds = true;
DoDelay(1);
STEP
bEveryThreeSeconds = false;
END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EveryTwoSeconds
////////////////////////////////////////////////////////////

RULE SemaphorePeriodicInt6 
  if (EveryTwoSeconds <= 0)
    then actionSemaphorePeriodicInt6
END_RULE
ACTION actionSemaphorePeriodicInt6
SCRIPT WORLD
EveryTwoSeconds = 0.60;
bEveryTwoSeconds = true;
DoDelay(1);
STEP
bEveryTwoSeconds = false;
END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EverySecond
////////////////////////////////////////////////////////////

RULE SemaphorePeriodicInt7
  if (EverySecond <= 0) 
    then actionSemaphorePeriodicInt7
END_RULE
ACTION actionSemaphorePeriodicInt7
SCRIPT WORLD
EverySecond = 0.30;
bEverySecond = true;
DoDelay(1);
STEP
bEverySecond = false;
END_SCRIPT
END_ACTION
	
Empire Earth 2 measures the time from a second in ticks. EE2 has 30 ticks per second, so function is called 30 times during 1. So we only must calculate the time we need for a timer in tickets. And write a simple to function which a game tick deducts or add to a int and set a bool true, if the timer ends.
The advantage of this method is, that she is very exactly and that you can start in the background from the game a timer without complicated calculation of the time. So far that was a bit too bulky for me with the previous script functions. I hope, it help you. :D
List of tutorials, useful threads and utilities (look here before posting): Here

Add Pics to Map: viewtopic.php?f=54&p=25184#p25184

Loews Work: viewtopic.php?f=54&t=5160

User avatar
Dr.MonaLisa
High Representative
Posts: 8697
Joined: 17 Jun 2010, 11:21
Location: Poland
Has thanked: 49 times
Been thanked: 108 times

Re: Create periodic or one shot timers with gameticks.

Post by Dr.MonaLisa »

The script is incomplete. I had to add sections and Initialization to test it:

Code: Select all

RULES FOR WORLD

SCENARIO

END_SCENARIO

DEFINITIONS
bool bEveryMinute
bool bEveryThirtySeconds
bool bEveryTenSeconds
bool bEveryFiveSeconds
bool bEveryThreeSeconds
bool bEveryTwoSeconds
bool bEverySecond

float EveryMinute
float EveryThirtySeconds
float EveryTenSeconds
float EveryFiveSeconds
float EveryThreeSeconds
float EveryTwoSeconds
float EverySecond

END_DEFINITIONS

INITIALIZATION
////////////////////////////////////////////////////////////
// INITIALIZATION 
////////////////////////////////////////////////////////////
	EveryMinute = 18.00;
	EveryThirtySeconds = 9.00; 
	EveryTenSeconds = 3.00;   
	EveryFiveSeconds = 1.50;
	EveryThreeSeconds = 0.90;
	EveryTwoSeconds = 0.60;
	EverySecond = 0.30;
	
////////////////////////////////////////////////////////////
// Get the Gametime in Ticks for Triggers and Effects
////////////////////////////////////////////////////////////

END_INITIALIZATION

RULE CountPeriodicInt
  if (true)
    then actionPeriodicInt
END_RULE
ACTION actionPeriodicInt
  SCRIPT WORLD
	EveryMinute  = EveryMinute - 0.01;
	EveryThirtySeconds   = EveryThirtySeconds - 0.01;
	EveryTenSeconds   = EveryTenSeconds - 0.01;
	EveryFiveSeconds  = EveryFiveSeconds - 0.01;
	EveryThreeSeconds   = EveryThreeSeconds - 0.01;
	EveryTwoSeconds   = EveryTwoSeconds - 0.01;
	EverySecond  = EverySecond - 0.01;
  END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EveryMinute
////////////////////////////////////////////////////////////
RULE SemaphorePeriodicInt1
  if (EveryMinute <= 0) 
    then actionSemaphorePeriodicInt1
END_RULE
ACTION actionSemaphorePeriodicInt1
SCRIPT WORLD
EveryMinute = 18.00;
bEveryMinute = true;
DoDelay(1);
STEP
bEveryMinute = false;
END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EveryThirtySeconds
////////////////////////////////////////////////////////////

RULE SemaphorePeriodicInt2
  if (EveryThirtySeconds <= 0)
    then actionSemaphorePeriodicInt2
END_RULE
ACTION actionSemaphorePeriodicInt2
SCRIPT WORLD
EveryThirtySeconds = 9.00;
bEveryThirtySeconds = true;
DoDelay(1);
STEP
bEveryThirtySeconds = false;
END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EveryTenSeconds
////////////////////////////////////////////////////////////

RULE SemaphorePeriodicInt3 
  if (EveryTenSeconds <= 0)  
    then actionSemaphorePeriodicInt3
END_RULE
ACTION actionSemaphorePeriodicInt3
SCRIPT WORLD
EveryTenSeconds = 3.00;
bEveryTenSeconds = true;
DoDelay(1);
STEP
bEveryTenSeconds = false;
END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EveryFiveSeconds
////////////////////////////////////////////////////////////

RULE SemaphorePeriodicInt4 
  if (EveryFiveSeconds <= 0)
    then actionSemaphorePeriodicInt4
END_RULE
ACTION actionSemaphorePeriodicInt4
SCRIPT WORLD
EveryFiveSeconds = 1.50;
bEveryFiveSeconds = true;
DoDelay(1);
STEP
bEveryFiveSeconds = false;
END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EveryThreeSeconds
////////////////////////////////////////////////////////////

RULE SemaphorePeriodicInt5
  if (EveryThreeSeconds <= 0)
    then actionSemaphorePeriodicInt5
END_RULE
ACTION actionSemaphorePeriodicInt5
SCRIPT WORLD
EveryThreeSeconds = 0.90;
bEveryThreeSeconds = true;
DoDelay(1);
STEP
bEveryThreeSeconds = false;
END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EveryTwoSeconds
////////////////////////////////////////////////////////////

RULE SemaphorePeriodicInt6 
  if (EveryTwoSeconds <= 0)
    then actionSemaphorePeriodicInt6
END_RULE
ACTION actionSemaphorePeriodicInt6
SCRIPT WORLD
EveryTwoSeconds = 0.60;
bEveryTwoSeconds = true;
DoDelay(1);
STEP
bEveryTwoSeconds = false;
END_SCRIPT
END_ACTION

////////////////////////////////////////////////////////////
// EverySecond
////////////////////////////////////////////////////////////

RULE SemaphorePeriodicInt7
  if (EverySecond <= 0) 
    then actionSemaphorePeriodicInt7
END_RULE
ACTION actionSemaphorePeriodicInt7
SCRIPT WORLD
EverySecond = 0.30;
bEverySecond = true;
DoDelay(1);
STEP
bEverySecond = false;
END_SCRIPT
END_ACTION

I don't understand the purpose of those timers?

It generally does the same as "PERIODICITY" rules?

Code: Select all

RULE ruleDumpResources PERIODICITY 0.5
Would trigger the event every half second...

Added after 28 minutes 29 seconds:
Maybe something like this instead?
mona_time_counter.ies
(4.23 KiB) Downloaded 131 times

Code: Select all

RULES FOR WORLD

SCENARIO

END_SCENARIO

DEFINITIONS
float fMonaEveryMinute
float fMonaEveryThirtySeconds
float fMonaEveryTenSeconds
float fMonaEveryFiveSeconds
float fMonaEveryThreeSeconds
float fMonaEveryTwoSeconds
float fMonaEverySecond
float fMonaTickPeriod

bool bMonaEveryMinute
bool bMonaEveryThirtySeconds
bool bMonaEveryTenSeconds
bool bMonaEveryFiveSeconds
bool bMonaEveryThreeSeconds
bool bMonaEveryTwoSeconds
bool bMonaEverySecond
bool bEnableMonaCounter
END_DEFINITIONS

INITIALIZATION
////////////////////////////////////////////////////////////
// INITIALIZATION 
////////////////////////////////////////////////////////////
	fMonaEveryMinute = 60.0;
	fMonaEveryThirtySeconds = 30.0;
	fMonaEveryTenSeconds = 10.0;
	fMonaEveryFiveSeconds = 5.0;
	fMonaEveryThreeSeconds = 3.0;
	fMonaEveryTwoSeconds = 2.0;
	fMonaEverySecond = 1.0;
	fMonaTickPeriod = 1.0 / 30;
	
	bMonaEveryMinute = false;
	bMonaEveryThirtySeconds = false;
	bMonaEveryTenSeconds = false;
	bMonaEveryFiveSeconds = false;
	bMonaEveryThreeSeconds = false;
	bMonaEveryTwoSeconds = false;
	bMonaEverySecond = false;
	bEnableMonaCounter = true;
	
	
////////////////////////////////////////////////////////////
// Get the Gametime in Ticks for Triggers and Effects
////////////////////////////////////////////////////////////

END_INITIALIZATION

RULE MonaTimeCounter
if(bEnableMonaCounter)
then actionMonaTimeCounter
END_RULE
ACTION actionMonaTimeCounter
 SCRIPT WORLD
	fMonaEveryMinute = fMonaEveryMinute - fMonaTickPeriod;
	fMonaEveryThirtySeconds = fMonaEveryThirtySeconds - fMonaTickPeriod;
	fMonaEveryTenSeconds = fMonaEveryTenSeconds - fMonaTickPeriod;
	fMonaEveryFiveSeconds = fMonaEveryFiveSeconds - fMonaTickPeriod;
	fMonaEveryThreeSeconds = fMonaEveryThreeSeconds - fMonaTickPeriod;
	fMonaEveryTwoSeconds = fMonaEveryTwoSeconds - fMonaTickPeriod;
	fMonaEverySecond = fMonaEverySecond - fMonaTickPeriod;
	if(fMonaEveryMinute <= 0){
		bMonaEveryMinute = true;
		fMonaEveryMinute = 60.0;
	}
	if(fMonaEveryThirtySeconds <= 0){
		bMonaEveryThirtySeconds = true;
		fMonaEveryThirtySeconds = 30.0;
	}
	if(fMonaEveryTenSeconds <= 0){
		bMonaEveryTenSeconds = true;
		fMonaEveryTenSeconds = 10.0;
	}
	if(fMonaEveryFiveSeconds <= 0){
		bMonaEveryFiveSeconds = true;
		fMonaEveryFiveSeconds = 5.0;
	}
	if(fMonaEveryThreeSeconds <= 0){
		bMonaEveryThreeSeconds = true;
		fMonaEveryThreeSeconds = 3.0;
	}
	if(fMonaEveryTwoSeconds <= 0){
		bMonaEveryTwoSeconds = true;
		fMonaEveryTwoSeconds = 2.0;
	}
	if(fMonaEverySecond <= 0){
		bMonaEverySecond = true;
		fMonaEverySecond = 1.0;
	}
  END_SCRIPT
END_ACTION

RULE MonaEveryMinute
  if (bMonaEveryMinute) 
    then actionMonaEveryMinute
END_RULE
ACTION actionMonaEveryMinute
SCRIPT WORLD
bMonaEveryMinute = false;
PrintMessage("Every 1 minute triggered");
END_SCRIPT
END_ACTION

RULE MonaEvery30Secs
  if (bMonaEveryThirtySeconds) 
    then actionMonaEvery30Secs
END_RULE
ACTION actionMonaEvery30Secs
SCRIPT WORLD
bMonaEveryThirtySeconds = false;
PrintMessage("Every 30 seconds triggered");
END_SCRIPT
END_ACTION

RULE MonaEvery10Secs
  if (bMonaEveryTenSeconds) 
    then actionMonaEvery10Secs
END_RULE
ACTION actionMonaEvery10Secs
SCRIPT WORLD
bMonaEveryTenSeconds = false;
PrintMessage("Every 10 seconds triggered");
END_SCRIPT
END_ACTION

RULE MonaEvery5Secs
  if (bMonaEveryFiveSeconds) 
    then actionMonaEvery5Secs
END_RULE
ACTION actionMonaEvery5Secs
SCRIPT WORLD
bMonaEveryFiveSeconds = false;
PrintMessage("Every 5 seconds triggered");
END_SCRIPT
END_ACTION

RULE MonaEvery3Secs
  if (bMonaEveryThreeSeconds) 
    then actionMonaEvery3Secs
END_RULE
ACTION actionMonaEvery3Secs
SCRIPT WORLD
bMonaEveryThreeSeconds = false;
PrintMessage("Every 3 seconds triggered");
END_SCRIPT
END_ACTION

RULE MonaEvery2Secs
  if (bMonaEveryTwoSeconds) 
    then actionMonaEvery2Secs
END_RULE
ACTION actionMonaEvery2Secs
SCRIPT WORLD
bMonaEveryTwoSeconds = false;
PrintMessage("Every 2 seconds triggered");
END_SCRIPT
END_ACTION

RULE MonaEvery1Secs
  if (bMonaEverySecond) 
    then actionMonaEvery1Secs
END_RULE
ACTION actionMonaEvery1Secs
SCRIPT WORLD
bMonaEverySecond = false;
PrintMessage("Every 1 second triggered");
END_SCRIPT
END_ACTION
In my method it shows the correct variables (60 seconds is 60.0 float), it just reduces by the tick period.

I still don't find any use of it. Seems like less optimal method than PERIODICITY, since in every tick it needs to make extra calculations. Not like it would affect current processors performance in 2021, but still.
Best regards,
Dr.MonaLisa
Ministry of Game Affairs
Department of Control and Complains
Post Reply

Return to “Tutorials”