How do I add a worker script to a node?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Plutospark

I want to add a worker script to certain nodes in the game to perform small work packets. Is it possible to send work packets to this script over the internet and have it relay the solution to a server? Like the S.E.T.I. volunteer computing system. I am new to godot and was looking for a similar demo or tutorial.

:bust_in_silhouette: Reply From: sxkod

I guess you can get data on HTTP as below and use posting or UDP packet to send the data back to server.
If you know the packets are small then you can just use UDP to start with.

With re: to doing it periodically, I guess you can create a variable and increment that with delta. Once it reaches a value, reset the counter, get a packet, process and send it back.

var http
var headers = ["User-Agent: Pirulo/1.0 (Godot)","Accept: */*"]

func gethttp(myurl):
	var err = 0
	var ctries=0
	var http = HTTPClient.new() 
	err = http.connect_to_host(myurl[0],myurl[1])
	while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
		ctries+=1
		http.poll()
		OS.delay_msec(500) #perhaps use a better non blocking delay
		if ctries>10:
			return "server didnot respond"
	if http.get_status()==HTTPClient.STATUS_CONNECTION_ERROR or http.get_status()==HTTPClient.STATUS_CANT_RESOLVE or http.get_status()==HTTPClient.STATUS_CANT_CONNECT:
		return("cant find the server!")
	err = http.request(HTTPClient.METHOD_GET, myurl[2], headers)

	while http.get_status() == HTTPClient.STATUS_REQUESTING:
		http.poll()
		OS.delay_msec(500)
	
	if http.has_response():
		headers = http.get_response_headers_as_dictionary() 
		var respcode=http.get_response_code() 
		var rb = PoolByteArray() 
		while http.get_status() == HTTPClient.STATUS_BODY:
			http.poll()
			var chunk = http.read_response_body_chunk() 
			if chunk.size() == 0:OS.delay_usec(1000)
			else:rb = rb + chunk
		return [myurl,respcode,rb]
		
	

You can use it as below.
var resp=gethttp([“localhost”,80,“/getdata.php”])

Hope it helps.

Thank you. I will try it and provide feedback of my implementation.

Plutospark | 2019-01-10 12:20