Skip to content

Instantly share code, notes, and snippets.

@belzecue
Forked from gruebite/example.gd
Created July 28, 2025 15:56
Show Gist options
  • Save belzecue/832667f0efa764c9b4e710c8b2259321 to your computer and use it in GitHub Desktop.
Save belzecue/832667f0efa764c9b4e710c8b2259321 to your computer and use it in GitHub Desktop.

Revisions

  1. @gruebite gruebite revised this gist Jul 27, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions instantiate.gd
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ static func scene(clss_name: Variant) -> Node:

    static func scene_path(clss_name: Variant) -> String:
    var clss_path := (clss_name as Resource).resource_path
    assert(clss_path.ends_with(".gd"), "can't instantiate non-class")
    assert(clss_path.ends_with(".gd"), "missing script for class")
    var scn_path := clss_path.substr(0, clss_path.rfind(".gd")) + ".tscn"
    assert(ResourceLoader.exists(scn_path), "can't find scene from class")
    assert(ResourceLoader.exists(scn_path), "missing scene for class")
    return scn_path
  2. @gruebite gruebite revised this gist Jul 27, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion example.gd
    Original file line number Diff line number Diff line change
    @@ -3,5 +3,5 @@ extends Node

    func _ready() -> void:
    # MyScene is is a `class_name` in a `.gd` script with an associated `.tscn` file
    var scene = MyScene.instantiate()
    var scene = Instantiate.scene(MyScene)
    add_child(scene)
  3. @gruebite gruebite revised this gist Jul 27, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion example.gd
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,6 @@ extends Node


    func _ready() -> void:
    # MyScene is is a `class_name` in a `.gd` script with an associated `.tscn` file
    # MyScene is is a `class_name` in a `.gd` script with an associated `.tscn` file
    var scene = MyScene.instantiate()
    add_child(scene)
  4. @gruebite gruebite revised this gist Jul 27, 2025. No changes.
  5. @gruebite gruebite revised this gist Jul 27, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion example.gd
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,4 @@ extends Node
    func _ready() -> void:
    # MyScene is is a `class_name` in a `.gd` script with an associated `.tscn` file
    var scene = MyScene.instantiate()
    add_child(scene)
    add_child(scene)
  6. @gruebite gruebite revised this gist Jul 27, 2025. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions example.gd
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    extends Node


    func _ready() -> void:
    # MyScene is is a `class_name` in a `.gd` script with an associated `.tscn` file
    var scene = MyScene.instantiate()
    add_child(scene)
  7. @gruebite gruebite created this gist Jul 27, 2025.
    19 changes: 19 additions & 0 deletions instantiate.gd
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    class_name Instantiate
    extends Object


    ## Instantiates a scene based on [param clss_name]. Scene must be located next
    ## to the associated class.
    static func scene(clss_name: Variant) -> Node:
    var scn_path := scene_path(clss_name)
    var scn: PackedScene = ResourceLoader.load(scn_path)
    var node := scn.instantiate()
    return node


    static func scene_path(clss_name: Variant) -> String:
    var clss_path := (clss_name as Resource).resource_path
    assert(clss_path.ends_with(".gd"), "can't instantiate non-class")
    var scn_path := clss_path.substr(0, clss_path.rfind(".gd")) + ".tscn"
    assert(ResourceLoader.exists(scn_path), "can't find scene from class")
    return scn_path