1
\$\begingroup\$

I am trying to write some code, where upon running it will place a 3d obj. It is a tool to help build a map before game runtine.

It is a simple 3d obj arrayMesh I am trying to add to a tscn file.

In godot I have:

@tool
extends EditorScript

# scene to 3d obj to
var scene = preload("res://main.tscn").instantiate()

# load 3d pbject
var grass = preload("res://map/Models/OBJ format/grass.obj")

func _run():
    # get node to append map to
    var map_holder_node = scene.get_node("map_holder")
    
    map_holder_node.add_child(grass.new())
    

I get the error:

res://map/generate_map.gd:14 - Invalid call. Nonexistent function 'new' in base 'ArrayMesh'.

Any ideas?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The error message refers to line 17 in your script, however, you only shared 15 lines of code in your question. \$\endgroup\$
    – liggiorgio
    Commented Jan 18 at 11:12
  • \$\begingroup\$ You are right @liggiorgio - I just removed some comments I made for myself here in the example, so that line number should refierence the map_holder_node.add_child(grass.new()) line \$\endgroup\$
    – nammerkage
    Commented Jan 18 at 11:33

1 Answer 1

3
\$\begingroup\$

Anything you load or preload is a Resource. That includes, for example, PackedScene, which you instantiate, and Scripts which you usually create with new (in general you create instances of classes with new, and Scripts are classes).

In this case you have an ArrayMesh, which is neither a PackedScene nor a Script.

I understand you want some Node, given that you are trying to use add_child. So the question is: what kind of Node can hold a ArrayMesh?

I want to highlight that Godot is not making assumptions about how Resources will be used. For example, you could create a script that exports a variable of ArrayMesh so you can set it. However, I don't think that is what you want.

Well, ArrayMesh is a kind of Mesh, and here are three build-in Node types that can use it: CSGMesh3D, GPUParticles3D and MeshInstance3D.

The most usual case is MeshInstance3D, which you create and then set itsmesh property:

    var mesh_instance := MeshInstance3D.new()
    mesh_instance.mesh = grass
    map_holder_node.add_child(mesh_instance)

You might want to set other of the properties of MeshInstance3D, but I cannot guess which or how.

And, of course, using the ArrayMesh directly in a Node is not the only thing you can do with it. For example, you might use it to create other resources... Notably, you might be interested in MultiMesh.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .