Scene instanced by code doesn't show up

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By c4n4r
:warning: Old Version Published before Godot 3 was released.

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?

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

genete | 2016-04-01 11:56

:bust_in_silhouette: Reply From: puppetmaster-

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.

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!

c4n4r | 2016-04-01 12:47