Programming Languages

Any language Godot supports should be able to work with Terrain3D via the GDExtension interface. This includes C#, and several others.

Here are some tips for integrating with Terrain3D.

../_images/integrating_gdextension.jpg

Detecting If Terrain3D Is Installed

To determine if Terrain3D is installed and active, ask Godot.

GDScript

     print("Terrain3D installed: ", EditorInterface.is_plugin_enabled("terrain_3d"))

C#

     GetEditorInterface().IsPluginEnabled("terrain_3d") 

You can also ask ClassDB if the class exists:

GDScript

     ClassDB.class_exists("Terrain3D")
     ClassDB.can_instantiate("Terrain3D")

C#

     ClassDB.ClassExists("Terrain3D");
     ClassDB.CanInstantiate("Terrain3D");

Instantiating & Calling Terrain3D

Terrain3D is instantiated and referenced like any other object.

GDScript

     var terrain: Terrain3D = Terrain3D.new()
     terrain.assets = Terrain3DAssets.new()
     print(terrain.get_version())

See the CodeGenerated.tscn demo for an example of initiating Terrain3D from script.

C#

You can instantiate through ClassDB, set variables and call it.

     var terrain = ClassDB.Instantiate("Terrain3D");
     terrain.AsGodotObject().Set("assets", ClassDB.Instantiate("Terrain3DAssets"));
     terrain.AsGodotObject().Call("set_show_region_grid", true);

You can also check if a node is a Terrain3D object:

GDScript

    if node is Terrain3D:

C#

private bool CheckTerrain3D(Node myNode) {
        if (myNode.IsClass("Terrain3D")) {
            var collisionMode = myNode.Call("get_collision_mode").AsInt32();

For more information on C# and other languages, read Cross-language scripting in the Godot docs.

Finding the Terrain3D Instance

These options are for programming scenarios where a user action is intented to provide your code with the Terrain3D instance.

  • If collision is enabled in game (default) or in the editor (debug only), you can run a raycast and if it hits, it will return a Terrain3D object. See more in the raycasting section.

  • Your script can provide a NodePath and allow the user to select their Terrain3D node.

  • You can search the current scene tree for nodes of type “Terrain3D”.

     var terrain: Terrain3D # or Node if you aren't sure if it's installed
     if Engine.is_editor_hint(): 
          # In editor
          terrain = get_tree().get_edited_scene_root().find_children("*", "Terrain3D")
     else:
          # In game
          terrain = get_tree().get_current_scene().find_children("*", "Terrain3D")

     if terrain:
          print("Found terrain")

Detecting Terrain Height

See Collision for several methods.

Getting Updates on Terrain Changes

Terrain3DData has signals that fire when updates occur. You can connect to them to receive updates.