Is there something like sizeof() ?

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

I’d like to know how much memory a node or class takes.

:bust_in_silhouette: Reply From: Wakatta

Simply convert your node or class to a PoolByteArray

var sizeof = 0
for bit in var2bytes(node):
    sizeof += bit

I’m not sure if I am doing something wrong, but I always get the same results.

class Test:
	var a := 0

func _ready() -> void:
	var test := Test.new()
	var sizeof := 0
	for bit in var2bytes(test):
		sizeof += bit
	print(sizeof)

Output: 33


class Test:
	var a := "Test"

func _ready() -> void:
	var test := Test.new()
	var sizeof := 0
	for bit in var2bytes(test):
		sizeof += bit
	print(sizeof)

Output: 33

ggez | 2021-02-10 16:30

Interesting. seams like var2bytes was not incorrectly named

Wakatta | 2021-02-10 17:21

Looks like it may be correct. So i did a test of your posted code
using File.store_var(entity) and File.get_len() and in both tests got the same filesize even when saving it to disk so this may be the best option.

Wakatta | 2021-02-28 15:32

I will just mark this question as answered. Thanks for replying

ggez | 2021-03-01 09:59