0 votes

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.

asked Jun 29, 2019 in Engine by TKT (35 points)

1 Answer

+1 vote
Best answer

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()
answered Jun 29, 2019 by Dlean Jeans (3,911 points)
selected Jun 29, 2019 by TKT

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))

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 How to use this Q&A? before posting your first questions.