ws.get_peer(1).put_var()
is always inside (or most often)
func _process(delta) which is a loop
if we want to get_var() we do
if ws.get_peer(1).get_available_packet_count() > 0:
var test = ws.get_peer(1).get_var()
which only gets the variable once, inside the loop the same loop.
Using put_var() we have to create our own if statements with counters and it gets messy in big projects. It would be nice to have a condition created by Godot to ensure that put_var() gets sent once just like
if ws.get_peer(1).get_available_packet_count() > 0: for get_var() that way put_var doesn't get executed more than once unless we remove the condition.
I'm not sure I could do it since I've never worked on this project, but I'm willing to try and work on it if this feature is approved
With the following code, you can spam send_once and set_send_once functions anywhere in your code as long as each variant to be sent is associated with an individual key string. The ID is necessary to differentiate calls with different variables to the send_once function. If used correctly, you wont need any counters nor extensive if-else statement chains to send data once to the other client.
EDIT: Fixed typos and program logic
extends Node
var peer_id : int = 1
var send_states : Dictionary= {}
var my_var : Array = ["Hello", "World"]
#------------------------------------------------------------------
# _process function loops and continuously runs send_once, but
# the data will be sent to the client only once, until the helper
# function re-enables sending data to the server.
#------------------------------------------------------------------
func _process(delta):
send_once(peer_id, my_var, "Greeting")
#------------------------------------------------------------------
# Here we define a signal from a button, which re-enables
# sending data to the client once, when the _process function
# runs the send_once method.
#-------------------------------------------------------------------
func _on_Button_pressed():
set_send_once("Greeting", true)
#------------------------------------------------------------------
# This is the main function which enables sending once to a client
# by keeping track of the sent status in the send_states dictionary
#-------------------------------------------------------------------
func send_once(id:int, my_var, key:String) -> void:
if send_states.has(key) == false:
send_states[key] = true
if send_states[key] == true:
send_states[key] = false
ws.get_peer(id).put_var(my_var)
#------------------------------------------------------------------
# This is the helper function which is used to re-enable sending data once
# to the client, when the _process function runs the send_once method.
#-------------------------------------------------------------------
func set_send_once(key:String, b:bool) -> void:
send_states[key] = b
Wow! that's really awesome thank you!
np :)
EDIT: Would like to clarify, that you probably need separate dictionaries for each client.
Also, when the dict grows huge, you might experience slight performance issues, if there are a lot of clients. In this case, it is possible to switch the dict for an array and use integer indexes to track send states, which is faster than accessing a dict. Also, you could declare an enum which maps human-readable words to integers, so you could retain clarity if using arrays and numerical indexes.
ws.get_peer(1).put_var()is always inside (or most often)
func _process(delta)which is a loop
Why? I mean, it's there, if you need to send something on every frame, otherwise you would just put it in a function, in a signal, and so on.
I'm sorry, but I really do not understand what the feature would be here.
Could you elaborate further?
@aspenforest Thank you so much for the tips!
@Faless
I can't retest now, but last time I tried, putvar() would only send messages to the server, if it's inside a
func _process(delta), in order to test, download this simple websocket basic example and run it, run the server too.
You'll notice an infinite loop sending "HI", now remove func _process(delta), and place the code either in a button _on_Button_pressed() or in a ready function, you'll notice nothing gets to the server.
Therefore, at least when dealing with WebSockets (haven't tried anything else), putvar() won't send anything unless it's a loop, or perhaps the server won't receive the message unless putvar() is in a loop, either way, func _process(delta) is vital for putvar() to send data to WebSockets server
If you want to run the same example that I linked to, run npm install in the server folder so that it installs the nodejs serialization library for godot.
Therefore, at least when dealing with WebSockets (haven't tried anything else),
putvar()won't send anything unless it's a loop, or perhaps the server won't receive the message unlessputvar()is in a loop, either way,func _process(delta)is vital forputvar()to send data to WebSockets server
Well, you want poll and get_var in process so data can be received, but if you only want to send once, simply move put_var out of the loop.
if Global.ws.get_connection_status() == Global.ws.CONNECTION_CONNECTING || Global.ws.get_connection_status() == Global.ws.CONNECTION_CONNECTED:
Global.ws.poll()
Retested again, you're right, that's the code that should be in the loop and get_var of course. Thanks!
I'm glad it's working now.
I've been working on a long overdue official tutorial, which has been
delayed due to some personal issues, I hope to publish it this week, so
stay tuned ;-) .
On Tue, Jul 23, 2019, 18:18 Elie Obeid notifications@github.com wrote:
if Global.ws.get_connection_status() == Global.ws.CONNECTION_CONNECTING || Global.ws.get_connection_status() == Global.ws.CONNECTION_CONNECTED:
Global.ws.poll()Retested again, you're right, that's the code that should be in the loop
and get_var of course. Thanks!—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/godotengine/godot/issues/30739?email_source=notifications&email_token=AAM4C3QHZ3NLMTEHBLGFR3DQA4VNJA5CNFSM4IFTRRZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2TU3PY#issuecomment-514280895,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAM4C3Q4EODGEQOMCJQO5GLQA4VNJANCNFSM4IFTRRZA
.
Of course please notify me as soon as the tutorial comes out as my entire game depends on WebSockets :)