0 votes

I have something like

extends Node2D

export (Resource) var this_card
export (Texture) var back_texture = "res://assets/cards/cardBack_red1.png"

func _ready():
    piece_name = this_card.piece_name
    mana_cost = this_card.mana_cost
    $CardControl/Template/Artwork.texture = this_card.artwork

and I wish to instantiate the node from gdscript editing dynamically the resource and texture to set.
But I cannot figure it out how to do it.

I tried with _init(card) but it says :

Cannot convert from Object to int

Can you help me, pleease ?

in Engine by (27 points)

1 Answer

–1 vote

To access a node for instancing that it must be saved as a separate scene. (Right click on it in the scene tree and choose Instance Child Scene). The function _init(): is called once a node has been initialised and cannot be used to instance other nodes. Instead you use "path_to_the_instance_child_scene".instance(), which returns a reference to that node.

From there you can adjust any properties you like before finally adding it as a child of something (it won't be created until you do this). Here I've added it as a child of the object that is doing the instancing, but you could use get_tree().get_root() to put it somewhere else.

var new_card = preload("res://*path of the scene to be loaded*")
export (Texture) var back_texture = "res://assets/cards/cardBack_red1.png"

func _ready():
    var instantiated = new_card.instance()
    new_card.name = self.name
    new_card.mana_cost = self.mana_cost
    new_card.get_child(interger_index_of_child_sprite).texture = back_texture
by (109 points)
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 webmaster@godotengine.org with your username.