This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

[TUTE] Knitting With Threads

PlanetKillerPlanetKiller Posts: 25Member
edited August 2016 in Programming
<br />extends Spatial<br /><br /># member variables here, example:<br /><br /># var a=2	<br /># var b=textvar<br /><br />var playernode<br />var deltime = 0.0<br />var timerlen = 2.0<br />var opti_thread =<br /><br />func start():<br />	playernode = get_node(/root/42/Player)<br />	print(playernode)<br /><br />func _fixed_process(delta):<br />	deltime += delta<br />	if(deltime &gt;= timerlen):<br />		if(opti_thread.is_active()):<br />			opti_thread.wait_to_finish()<br />		else:<br />			opti_thread.start(self, optimize, opti, 1)<br />			print(opti_thread.is_active())<br />			deltime = 0.0<br />		pass<br /><br /><br />func _ready():<br />	start()<br />	optimize(opti)<br />	set_fixed_process(true)<br />	<br />	# Initialization here<br />	pass<br /><br />func optimize(word):<br />	print(word)<br />	for i in get_children():<br />		var dist = i.get_translation().distance_to(playernode.get_translation())<br />		if(dist &lt;= 350):# between 300-350 is best<br /><br />		else:<br />			i.hide()<br />			#return null<br />		pass<br /><br /><br />
<br /><br />Should be pretty obvious from the script that Im optimizing my game by hiding/showing objects based upon distance from the player so Im going to type about simple thread usage and go over some pitfalls. Kobuge has a thread tutorial but it doesn't explain as much as I had hoped, so lets get started.<br /><br />TIPS:<br />
    <br />[li]Threads seem to optimize the functions passed to them, and wont normally print anything in the functions.[/li]<br />[li]They will print values passed to the function if you have such a statement.[/li]<br />[li]You dont need to pass a value or print it (prints in the above code are for troubleshooting).[/li]<br />[li]You cannot start a thread that is already started (you have to wait for it to finish).[/li]<br />[li]print(thread.start(target, function, pass, priority)) will print an error, 0 = OK, 23 = in use.[/li]<br />[li]priority 0 wont run the thread while higher priorities make the thread take longer to complete.[/li]<br />
<br /><br />
<br />if(opti_thread.is_active()):<br />	opti_thread.wait_to_finish()<br />else:<br />	opti_thread.start(self, optimize, opti, 1)<br />
<br /><br />Lets go over the important part of the code. After declaring your thread as a variable (at the top) you'll want an if statement. First check if the thread is active and wait for it to finish. Else you'll want to start the thread and pass it parameters, you only need two parameters unless you are passing values or changing priority. This allows you to use threads for simple things while reducing the frame-rate impact. More complex examples can be found at the above link.<br />
[size=9pt]post fixed by:  Megalomaniak[/size]

Tags :

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.