Godot Version
4.2
Question
Working on a Heroes of Might & Magic kind of thing. There are dozens of types of Units, each with their own properties (name, cost, level, health, attack, etc.). You create your army by choosing Units from the dozens of unit types in the Unit database (dictionary, list, whatever). That database is just abstract data, not something on the screen that has transforms and stuff.
In other languages i’d create a class but i’m told that’s not very Godoty. What’s the proper way to do this?
Note: It takes a long time to create dozens of Units (items, spells, etc.) so hopefully whatever format i use won’t prevent me from creating a Unit editor i can use to build them.
A class sounds perfectly fine, especially if it’s extending a base unit class that holds that common information like name, cost, level, health, attack, etc.
Extending your class will let you program unique functions for each unit which you may want.
1 Like
Great! Now… um… i’m really new so i’m not entirely sure what to google to learn how to build classes. Sorry
. i’ve been trying to read the official docs and look at examples but a lot still goes over my head. Is it just something like:
The class:
class_name Unit extends Resource
@export var name: String
@export_multiline var description: String
..etc..
Then i create a bunch of instances of those and save each instance as a resource? i’m unclear on this part but i can google gdscript resources
.
Using it:
var units = ["res://Units/Angel.tres", "res://Units/Bandit.tres"]
func load_unit_database():
for i in units.size():
var unit := Unit.new()
unit.init(load(units[i]))
unit_db[unit.name] = unit
Is that heading in the right direction? Or am i way off here?