The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

hi there!

i'm a bit noob Godot so I want to ask for help...

i want to instance a scene as node by code.
My aim is to generate level portions (scenes) procedurally.

but i'm stuck on the first step, here is what i did :

func _ready():
    var scene = preload("res://portions/portion1.scn");
    var node = scene.instance();    
    add_child(node);

(The scene exist and contain a tilemap)
Unfortunately, when I run the code, nothing appears on-screen.

Maybe i did something wrong?

in Engine by (55 points)
edited by

Does your portions/portion.scn produce anything on screen when it is run alone?

1 Answer

0 votes
Best answer

code looks ok so far..

please check

  • visibility of portions.scn and portion1.scn
  • position of portion1.scn

In my game I have used it like this:

var mapscene = load("res://scenes/maps/map"+str(map_number)+".scn")
var map = mapscene.instance()
get_node("level").add_child(map)

If you like to do something cool..

tool

extends Node2D

export var portion_number = 1 setget set_portion

func set_portion(new_value):
    portion_number = new_value
    if has_node("portions"):
        var portionscene = load("res://portions/portion"+str(portion_number)+".scn")
        if (portionscene != null):
            if(has_node("portions/portion")):
                get_node("portions/portion").free()
            var portion = portionscene.instance()
            get_node("portions").add_child(portion)

With this you can change dynamically your portions directly in editor.

by (699 points)
selected by

It worked fo me!

I made a mistake, my main Scene Camera position was not 0,0 so when i load my portions, they are pushed into the scene but was offscreen.

i'll check you code for the procedural creation, it seem's cool!

thx a lot!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.