0 votes

If I want to roll my own network protocol and send packets as in PacketPeer's put_packet, how would I get primitive types like int into a RawArray?

in Engine by (238 points)

2 Answers

0 votes
Best answer

I left it open just in case but I'm fairly sure now that it isn't possible to do these kinds of conversions.

In 3.0, it is possible using StreamPeerBuffer and the StreamPeer methods.

by (238 points)
edited by

It has been ported to 2.1 and will be available in the next release.

0 votes

This seems to work:

var float_in = -0.352141175
#float to RawArray
var float_raw = String(float_in).to_ascii()
#RawArray to float
var float_out = float_raw.get_string_from_ascii().to_float()

var int_in = 352141175
#int to RawArray
var int_raw = String(int_in).to_ascii()
#RawArray to int
var int_out = int_raw.get_string_from_ascii().to_int()
by (1,544 points)

On second try it doesn't work that great for floats, small values get truncated / rounded to zero.

I don't want a textual representation of the number I want:
- For ints: typical bit vector representation of fixed width integers
- For floats: IEEE representation of single-precision floating point number

Outputting it as text bloats the size of the data. For example, 50000 in text takes up 5 bytes, as a 32-bit machine representation it is only 4 bytes. It is even worse for floating point, where the text representation can be very long if the entire precision is output as decimal. Something like 1.4534636 (made up number) takes up 9 bytes as opposed to 4 bytes in machine format, bloating it by more than 100%.

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.