#pragma once

#ifdef GAMEQC
	#include "oo.qh"
	#include "self.qh"

	entityclass(Defer);
	classfield(Defer).entity owner;
	classfield(Defer).void(entity) defer_func;

	/// Remove entity
	void SUB_Remove(entity this)
	{
		delete(this);
	}

	void defer_think(entity this)
	{
		setthink(this, SUB_Remove);
		this.nextthink = time;
		this.defer_func(this.owner);
	}

	/// Execute func() after time + fdelay.
	/// self when func is executed = self when defer is called
	void defer(entity this, float fdelay, void(entity) func)
	{
		entity e = new_pure(deferred);
		e.owner = this;
		e.defer_func = func;
		setthink(e, defer_think);
		e.nextthink = time + fdelay;
	}
#endif // GAMEQC
