Last active
July 23, 2019 07:25
-
-
Save TheDuriel/8cb20a39932d47cd28c8669a13583619 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
class_name MainLoopThread | |
""" | |
MainLoopThread Class Script | |
Creates a continously running thread (until it causes a stack overflow) | |
With a configurable timestep | |
Thread can recieve inputs to work on each tick, and provide output | |
TODO: Move thread internal functions, and thread_out signal, to seperate class | |
DONT TRY THIS AT HOME THIS IS A MEME | |
""" | |
signal thread_in | |
signal thread_out | |
var _thrd: Thread = Thread.new() | |
var _thrd_timer: Timer = Timer.new() | |
# Set this to give data to the thread. | |
var thread_input = null | |
func _ready() -> void: | |
add_child(_thrd_timer) | |
_thrd_timer.connect("timeout", self, "_on_timeout") | |
_thrd_timer.wait_time = 1 | |
_thrd_timer.one_shot = false | |
connect("thread_out", self, "_on_thread_out") | |
# warning-ignore:return_value_discarded | |
var err: int = _thrd.start(self, "t_loop", _thrd_timer) | |
assert(err == OK) | |
_thrd_timer.start() | |
func _on_thread_out(thread_time, thread_out) -> void: | |
printt("Thread Output: ", thread_time, thread_out) | |
func _on_timeout() -> void: | |
emit_signal("thread_in", thread_input) | |
func t_loop(thrd_timer) -> void: | |
var time: int = 0 | |
while true: | |
var in_val = yield(thrd_timer, "thread_in") | |
time += 1 | |
# Emit [time, outval] | |
emit_signal("thread_out", [time, in_val]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment