[scripting] endless loops


(nUllSkillZ) #1

Hi,

today two script-“structures” have come to my mind which seem to create endless loops.
Although I’m not sure how the script-engine handles scripts, I think these structures should never ever be used (“no no’s”) without any control mechanism:


...
ENTITY01_SCRIPTNAME_VALUE
{
	...
	EVENT01
	{
		...
		trigger self EVENT01 // or: "trigger ENTITY01_SCRIPTNAME_VALUE EVENT01"
		...
	}
	...
}
...


...
ENTITY01_SCRIPTNAME_VALUE
{
	...
	EVENT01
	{
		...
		trigger ENTITY02_SCRIPTNAME_VALUE EVENT02
		...
	}
	...
}

ENTITY02_SCRIPTNAME_VALUE
{
	...
	EVENT02
	{
		...
		trigger ENTITY01_SCRIPTNAME_VALUE EVENT01
		...
	}
	...
}
...

Reason for posting this:
In the forum I usually visit there are posts which suggests / have the statements that scripts are copied and pasted (whole or partial).
(Is scripting that evil?)
And changing scripts without any or minor knowledge can cause errors.


(sock) #2

Sure you can create endless loops if you want! As you said cutting and pasting code without understanding can also cause problems. But with a good break mechanism in place it should be easy to avoid.

Try this little example. (Its untested as I dont have a copy of ET to hand) Create 2 script_mover entities and targetname/scriptname to these two routines. Compile into a box map and have some fun testing it. It should work! (fingers crossed)


endless_loop
{
	spawn
	{
		wait 2000		// 2s delay
		trigger self looping	// endless loop time
	}
	
	trigger looping
	{
		wm_announce "Looping looping looping ..."
		wait 50		// 1 frame delay
		trigger self looping	// Keep on going
	}
	
	trigger stopme
	{
		wait 2000		// 2s delay
		wm_announce "Stopped"
	}
}

interrupting
{
	spawn
	{
		wait 10000		// 10s delay
		wm_announce "I want to break free!"
		trigger endless_loop stopme
	}
}

The last function will interrupt the first function because only one function can run at once inside of a routine. The wait statement takes the control back to the “stopme” function. Have a look at the example code with “g_scriptdebug 1” console command so you can see the function being interrupted.

Sock
:moo: