0 votes

I'm sending UDP packets to a PC that runs a Godot app. The packets contain 4 byte floats. I'm using PacketPeerUDP to receive the packets. The problem is that, PacketPeerUDP has only a method (getpacket()) to read packets as bytes and not as floats.... How to reconstruct the the packets as floats? (I don't want to use getvar() because it introduces extra overhead to the sender)
Any ideas are highly appreciated.

in Engine by (18 points)

2 Answers

+1 vote
by (28,735 points)

The actual implementation could be something like this:

var spb = StreamPeerBuffer.new()
spb.data_array = pkt
var single_float = spb.get_float()
0 votes

I had to resort to brute-forcing the solution, according to the IEEE754 specifications.

func bin2float(pkt:PoolByteArray) -> float:
#Convert packet to an array of bools
var arr:Array=[]
for byte in pkt:
    for _i in range(0,8):
        arr.append(byte%2)
        byte/=2

#Extract sign
var r_sign:int
if arr[31]==1:
    r_sign=-1
else:
    r_sign=1

#Extract exponent
var r_exp:int=0
for i in range(30,22,-1):
    r_exp*=2
    r_exp+=arr[i]
r_exp-=127

#Extract mantissa
var r_mant:float=0
for i in range(0,23):
    r_mant+=arr[i]
    r_mant/=2
r_mant+=1

return r_sign*pow(2,r_exp)*r_mant

Of course this is terribly slow, and a real native solution would still be greatly appreciated, but this works in the meantime.

by (19 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.