Skip to content

Instantly share code, notes, and snippets.

@DoctorGester
Created September 4, 2020 00:27
Show Gist options
  • Save DoctorGester/acb148096fd8b2db773ad1ede41fec49 to your computer and use it in GitHub Desktop.
Save DoctorGester/acb148096fd8b2db773ad1ede41fec49 to your computer and use it in GitHub Desktop.
declare let scheduler: Scheduler;
if (!scheduler) {
scheduler = {
tasks: new Map<Coroutine<any>, Task>()
};
}
type Scheduler = {
tasks: Map<Coroutine<any>, Task>;
}
type Task = {
is_waiting: boolean;
}
type Fork<T> = {
has_finished: false
} | {
has_finished: true
result: T
}
function resume_coroutine(routine: Coroutine<void>) {
const [execution_result, possible_error] = coroutine.resume(routine);
if (execution_result == false) {
print("Error when executing coroutine");
print(debug.traceback(routine));
print("", possible_error);
log_chat_debug_message(`Error when executing coroutine: ${possible_error}`);
}
}
function update_scheduler() {
scheduler.tasks.forEach((task, routine) => {
if (task.is_waiting) {
task.is_waiting = false;
resume_coroutine(routine);
}
if (coroutine.status(routine) == Coroutine_Status.dead) {
scheduler.tasks.delete(routine);
}
});
}
function fork<T>(code: () => T): Fork<T> {
const task: Task = {
is_waiting: false
};
const fork: Fork<T> = {
has_finished: false
};
const routine = coroutine.create(() => {
const result = code();
const completed: Fork<T> = {
has_finished: true,
result: result
};
Object.assign(fork, completed);
});
scheduler.tasks.set(routine, task);
resume_coroutine(routine);
return fork;
}
function wait_one_frame() {
const routine = coroutine.running();
const task = scheduler.tasks.get(routine as Coroutine<any>);
if (task && routine) {
task.is_waiting = true;
coroutine.yield(routine);
} else {
throw "Not in a fork";
}
}
function wait(time: number) {
if (time == 0) {
print("Can't wait for 0! Defaulting to 1 frame wait");
wait_one_frame();
return;
}
const start_time = GameRules.GetGameTime();
wait_until(() => GameRules.GetGameTime() - start_time >= time);
}
function wait_until(condition: () => boolean) {
while (!condition()) {
wait_one_frame();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment