#Better way to make a class constructor initialize both the values and add the node to the scene

21 messages · Page 1 of 1 (latest)

void bobcat
#

I have a class called character

extends Node2D

var id
var selected
var health
var damage

func init(pos, health, damage, texture,id):
    self.position = pos
    self.health = health
    self.damage = damage
    self.get_node("Sprite").texture = load("res://assets/sprites/"+texture+".png")
    self.id = id```

which i want to add multiple of in a scene. My solution so far is to have in the main scene
```py
func _ready():
    characters.append(add_character(Vector2(500,500),100, 20, "blue", 0))
    characters.append(add_character(Vector2(700,500),70, 30, "red", 1))

...

func add_character(pos, health, damage, texture, id):
    var CharacterScene = load("res://scenes/character.tscn")
    # Create an instance of the Character scene
    var character_instance = CharacterScene.instantiate()
    # Set properties of the Character instance
    character_instance.init(pos, health, damage, texture, id)
    # Add the Character instance to the scene tree
    add_child(character_instance)
    return character_instance

is there a better way to do this?

oak vigil
#

I think it would be better to move add_character to a static method in Character, that way all the character creation logic is contained inside the correct script.

#

By the way, using swift for syntax highlighting generally gives better results than py

void bobcat
oak vigil
#

I think it is, it makes it so that all the character creation logic is inside a single script and it also makes it easier to reuse. Do be warned that if you want to use static functions and variables you'll need to name your class with class_name so it can be accessible by other classes

#

Like class_name Character extends Node2D

#

Then you could call Character.create(pos, health, damage, texture, id)

void bobcat
#

i see, thanks

#

though
Character.add_character(...)
raises an error even though i make the method static, says that identifier Character is not delcared in current scope

oak vigil
void bobcat
#

class_name Character
yeah

oak vigil
void bobcat
#
class_name Character extends Node2D

var id
var selected
var health
var damage

func init(pos, health, damage, texture,id):
    self.position = pos
    self.health = health
    self.damage = damage
    self.get_node("Sprite").texture = load("res://assets/sprites/"+texture+".png")
    self.id = id

...
static func create(scene, pos, health, damage, texture, id):
    var CharacterScene = load("res://scenes/character.tscn")
    var character_instance = CharacterScene.instantiate()
    character_instance.init(pos, health, damage, texture, id)
    scene.add_child(character_instance)
    return character_instance

oak vigil
#

Can you post where you call the method please?

#

Also, is that the entire thing?

void bobcat
oak vigil
#

Is everything saved?

#

It won't parse the global class until you save it

void bobcat
#

oh...

#

yeah that fixed it xd

#

thanks