Cant instance child sprite

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TKT

oMain------------------------- o GemControl
-gem_container------------- Gem

–GemControl

  extends Node2D
        var lastpos
        var positions=[]
        var screenSize
        onready var gems=get_node("Gem")
        
        func _ready():
        	screenSize=get_viewport_rect().size
        	positions=gems.get_position()
        	
        	
        func _on_gem_collided():
        	gems.free()
        
        func _get_gems_position(pos):
        	pass
        func spawn_gems(num):
        	for i in range(num):
        		
        		var g=gems.instance() #--------Problem-----
        		g.set_scale(Vector2(0.5,0.5))
        		g.set_modulate(Color(0,1,0))
        		add_child(g)
        		g.set_position(Vector2(rand_range(20,screenSize.x-20),rand_range(20,screenSize.y-20)))

–Main

func _ready():
	var g=gem.instance()
	add_child(g)
	g.spawn_gems(10)

There is an error says Nonexistent function ‘instance’ in base ‘Sprite’. Line …var g=gems.instance()… creates a problem.
I am trying to understand why i cant make instance of sprite(GEM) from its parent Gem_control. I am new to godot. Thanks in advance.

:bust_in_silhouette: Reply From: Dlean Jeans

Because gems is a node. You can’t instance a node, you can only instance scenes, which is saved on disk.
You may wanna duplicate the node instead:

var g = gems.duplicate()

Thank you.You are right.I confused since every scene is a node. I didnt think it was different. gems.duplicate() works and if someone want to maintain node properties there are flags to add ( gems.duplicate(15))

TKT | 2019-06-29 15:15