Set file on C++ node

Godot Version

v4.2.1.stable.mono.official [b09f793f5]

Question

I’m writing an emulator that displays its results on an object inside a godot scene. The eventual plan also includes controlling Godot objects using programs. The emulator is currently a GDExtension extending from the Node object.

I would like to be able to set the ROM code from within Godot, so I would like to make the ROM file a property on the node.

What I’ve tried

I tried to add a script to the node, which uses the export_file decorator to expose the property, and have the C++ CPU object call that to grab the ROM contents at boot. However on the C++ side I don’t seem to get the right object back.

In the script:

extends FakeCPU

@export_file("*.o") var rom_file

func _get_rom() -> String:
	print("Getting ROM")

	var file = FileAccess.open(rom_file, FileAccess.READ)
	return file.get_as_text()

In the C++ Code:

FakeCPU::FakeCPU()
{
    memset(this->memory, 0, sizeof(this->memory));

    UtilityFunctions::print("FakeCPU Boot");

    Variant script = this->get_script();
    String rom = script.call("_get_rom");

    this->pc = 0;
    this->a = 0;
    this->b = 0;
}

Which results in the following object coming back:
image

Which is not the right size, nor contents. Nor does it seem to matter as “Getting ROM” is never printed.

Of course if there is a way to expose the same property thing from C++ directly and not need an attached script at all, I’m all ears.

So, the problem here was that the FakeCPU is constructed, then the script attached later. I moved the call to a _ready() method on the C++ class and now the call works perfectly.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.