[ZScript] Scheduler

Post your example zscripts/ACS scripts/etc here.
Forum rules
The Projects forums are only for projects. If you are asking questions about a project, either find that project's thread, or start a thread in the General section instead.

Got a cool project idea but nothing else? Put it in the project ideas thread instead!

Projects for any Doom-based engine (especially 3DGE) are perfectly acceptable here too.

Please read the full rules for more details.
Post Reply
User avatar
Gustavo6046
Posts: 136
Joined: Sat May 13, 2017 3:11 pm
Location: Brazil
Contact:

[ZScript] Scheduler

Post by Gustavo6046 »

Good afternoon. I have been doing a bot in ZScript (ZetaBot), and I found out that, to make sure it'd work with weapon switching (weap.Use() can't be followed immediately by FireWeapon() or FireWeaponAlt() if I recall correctly), I had to create a Scheduler. It is basically a class that will check for a condition every tick (based on a ScheduleChecker or a WeaponChecker), and, if this condition is met, it will set its state to a done state. Its state can be polled every tick (it calls Destroy() on itself if the state is SS_DONE). I believe this will make it easier to create sequence of actions in a Thinker, where actor states are for some reason not yet supported. (I know, they are called actor states, but Thinkers could have some sort of finite state machine too, like invisible states, idk!)

Why are they going to be used by ZetaBots? Because otherwise they might not fire at all. They do have weapon rating code (it's rather bad, but then blame the actor state API - I wanted to get the amount of times that A_FireProjectile/A_FireBullets/etc are called, and their projectile classes, as part of an equation!), but you know how it's difficult to create a Thinker to control ("possess") a PlayerPawn - especially with weapon handling!

So Schedulers were made to make the job slightly simpler. I hope you enjoy them as well :)

TL;DR something inspired by JavaScript Promises.

Code: Select all

class ScheduleChecker : Thinker
{
	bool bDone;
	
	void BeginPlay()
	{
		bDone = false;
	}
	
	void fire()
	{
		bDone = true;
	}
}

class WeaponChecker : ScheduleChecker
{
	PlayerPawn __p;
	Weapon __w;

	void link(PlayerPawn other, Weapon weap)
	{
		__p = other;
		__w = weap;
	}
	
	static WeaponChecker create(PlayerPawn other, Weapon weap)
	{
		let res = new("WeaponChecker");
		res.link(other, weap);
		
		return res;
	}
	
	override void Tick()
	{
		Super.Tick();
		
		if ( !bDone && __p != null && __p.player.ReadyWeapon == __w )
			fire();
	}
}

class Scheduler : Thinker
{
	enum ScheduleState
	{
		SS_PENDING,
		SS_RUNNING,
		SS_DONE
	};
	
	ScheduleState __s;
	ScheduleChecker checker;
	
	void BeginPlay()
	{
		__s = SS_PENDING;
	}
	
	static Scheduler create(ScheduleChecker checker, bool bAutoStart)
	{
		let res = new("Scheduler");
		res.checker = checker;
		
		if ( bAutoStart )
			res.__s = SS_RUNNING;
			
		return res;
	}
	
	ScheduleState pollState()
	{
		if ( __s == SS_DONE )
			Destroy();
			
		return __s;
	}
	
	override void Tick()
	{
		Super.Tick();
	
		if ( __s != SS_RUNNING )
			return;
			
		if ( checker.bDone )
			__s = SS_DONE;
	}
}
EDIT: added recent changes
Post Reply

Return to “Script Library”