Godot Version
4.5
Question
I’m having a little trouble understand how to use this two together to create units for my strategy game. An explanation would be appreciated!
4.5
I’m having a little trouble understand how to use this two together to create units for my strategy game. An explanation would be appreciated!
class_name is needed if you want to create instances of the class.
Export variables allow values to be set in the editor. I also use them to specify the variables I intend for use in other classes.
class_name CharacterAttributes
extends Resource
@export var health: float = 100
@export var move_speed: float = 150.0
@export var awareness_distance: float = 250.0
You can use the export variables to modify the instance data in your code.
var char = CharacterAttributes.new()
char.health = 250
print(str("Char health is ", char.health))
What is the purpose of the “extends Resource”?
It means that CharacterAttributes class inherits Resource class. It’s like it has all the functionality of Resource class but you can add extra functionality to it only specific to CharacterAttributes.
A tutorial about class inheritence:
Not strictly needed. You can new() script resources as well:
load("res://my_script_class.gd").new()
You can also inherit anonymous classes by just specifying the script resource filename:
extends "res://my_script_class.gd"
Of course, using class names is much nicer and more readable.