Skip to reading
GDScript Programming & Godot

42. Add a native 3D object to the room

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView illustrations
Code example
var cube: MeshInstance3D
Code example
func run() -> bool:
    var room: Node3D = Runtime.get("active_room_root") as Node3D
    if room == null or not is_instance_valid(room):
        return false
    if is_instance_valid(cube):
        cube.queue_free()
    cube = MeshInstance3D.new()
    var mesh := BoxMesh.new()
    mesh.size = Vector3(1.2, 1.2, 1.2)
    cube.mesh = mesh
    var material := StandardMaterial3D.new()
    material.albedo_color = Color("3fb9b1")
    cube.material_override = material
    cube.position = Vector3(0, 1.2, 0)
    room.add_child(cube)
    return true
Code example
func _exit_tree() -> void:
    if is_instance_valid(cube):
        cube.queue_free()

Run this after a 3D room has loaded. active_room_root is a version-specific runtime member, so the cast and validity check matter. The cube is a native MeshInstance3D in the current room, with its own BoxMesh and material.

It is not automatically a PCA object definition, hotspot, blocking volume or saved item. Use Create Primitive/Add Asset to Scene when you want the adventure engine to own those behaviors. Use native nodes when your extension genuinely owns their rendering and lifecycle.

Figure 13. A native Godot MeshInstance3D created by a global GDScript record in the running PCA room.

Page 51 of 378Permalink
Go to page