Skip to content
WhyTry313 edited this page Aug 24, 2023 · 4 revisions

How to duplicate an object without its attached script?

To duplicate an object and avoid binding errors, you need to remove its script with set_script()

    const objWithoutScript = this.duplicate();
    objWithoutScript.set_script(null); // Or set a new script


How to set a material to all children of an object?

In order to override all materials of an object, loop through all its elements and change all materials of its MeshInstance child nodes.
In order to revert, pass a null material to let the MeshInstace set back its original material

Note: Make sure this function doesn't run inside _process or _physics_process as these operations can become heavy on complex objects, it should run on action-driven events to ensure it runs only once when needed

    const treeWalker = (node, material) => {
        const children = node.get_children();
        // Loop through its children if any
        if (children.length > 0) { children.forEach(child => treeWalker(child, material)); }

        // Make sure the current node is a MeshInstance
        if (node.__class__ === "MeshInstance") {
            // Make sure it has materials to override
            const nbMaterials = node.get_surface_material_count();
            for (let i = 0; i < nbMaterials; i++) {
                node.set(`material/${ i }`, material);
            }
        }
    };
    // To unset the material override, pass a null material
    treeWalker(object, myMaterial);


How to export a curve?

As I encountered this problem many times, I though it would be helpful to talk about it. Exporting a curve can be tricky, setting a godot.TYPE_REAL as type won't work, exporting a plain godot.Curve won't store it, and not setting a default value will end up in a float slider.
Here's how to export a curve without errors:

    godot.register_property(myClass, "myVar", {
        type: Number, // For some reason Number works
        hint: godot.PROPERTY_HINT_EXP_EASING, // Hint for exponential easing function
        default: new godot.Curve() // Important to declare
    });