How to construct a 'PoolByteArray'?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Skyfish
:warning: Old Version Published before Godot 3 was released.

Hi,

I want construct a String from ascii codes. I tried to construct a PoolByteArray to build and then convert to String with get_string_from_ascii().

(1) there is no way to create a new PoolByteArray which is unfortunate.

I made a workaround for (1) using a standard Array in GDScript:

var char_array = []
	for i in range (0,95): # 0x20-0x7E
		char_array.append(32+i)

(2) According to the documentation I then should be able to create:

PoolByteArray PoolByteArray ( Array from )

I tried everything:

var byte_array = PoolByteArray(char_array)
var byte_array = PoolByteArray().new(char_array)
var byte_array = PoolByteArray(char_array).new()

I always get:

Invalid call. Nonexistent function ‘PoolByteArray’

How can you construct one?

My workaround is currently to construct this String outside Godot and copy it into the script. It should somehow be possible to do this with GDscript. Any suggestions?

(3) The next problem i encounter is when i String.to_ascii() i get a RAWArray. but I need a PoolByteArray to use the compress() function i need.

:bust_in_silhouette: Reply From: Zylann

In 3.0, you can create a PoolByteArray the same way you build a Vector2, Color or Rect2:

var array = PoolByteArray()

Beware though, these use copy-on-write, unlike arrays made with [].

In 2.x, it’s RawArray, different name for the same class: http://docs.godotengine.org/en/stable/classes/class_rawarray.html#class-rawarray
There is no compress function in 2.x.

(also, note the “stable” vs “latest” in the doc link, one is 2.x, the other is 3.x)