|
|
|
|
Reply From: |
selamba |
Completely rewriting the answer since the question wasn’t clear.
CharacterStats.gd:
class_name CharacterStats
var name: String = "N/A"
var health: int = -1
var mana: int = -1
func _init(name: String, health: int, mana: int):
self.name = name
self.health = health
self.mana = mana
func get_stats() -> String:
return name + " has " + str(health) + " health and " + str(mana) + " mana."
func print_stats() -> void:
print(self.get_stats())
Node.gd:
extends Node
var characters_data: Dictionary = {
"Cole": CharacterStats.new("Cole", 100, 30)
}
func _ready():
print(characters_data["Cole"].get_stats())
characters_data["Kaya"] = CharacterStats.new("Kaya", 40, 150)
if characters_data["Kaya"].has_method("print_stats"):
characters_data["Kaya"].print_stats()
CharacterStats.gd is not attached to any node (it also doesn’t extend anything), but Node.gd is attached to a Node. In this design CharacterStats.gd is sort of a header-file, like in c++.
Thanks for answering, but this wasn’t exactly what I was going for. Sorry if I wasn’t very clear.
I want to create a dictionary that stores all of the character stats. I could do something like this:
var character_data = {
character1={name='Name',speed=400...},
character2={name='name',speed=250...},
...
}
But that would be repetitive and take a very long time. I want to make a function that takes in the separate stats as parameters, compile them into a dictionary, and then return the dictionary, so its entries can be accessed. I originally tried this:
func character_new(name:String,speed:int,...)
var character_data = {
name=name,
speed=speed,
...}
return character_data
var character1 = character_new('Name',400...)
But it didn’t work. How can I make something like this that works? Thanks, once again.
Huckleberry256 | 2020-06-28 16:15
I edited the answer.
selamba | 2020-06-28 18:19
That makes a lot of sense! Thanks so much!!!
Huckleberry256 | 2020-06-30 16:21