Skip to content

Melee Weapon Resource

The WeaponResource class is a custom resource that defines the attributes of a melee weapon. It allows for easy creation, customization, and balancing of melee weapons by storing key properties such as damage, attack timing, and visuals.

By using a resource-based approach, weapon data can be reused across multiple characters, making it simple to balance weapons, modify stats, and manage weapon variations without altering any code.

  • Defines core weapon properties like damage, attack speed, and cooldown.
  • Supports different weapon models and UI elements for better customization.
  • Allows easy modification directly in the Godot Editor, no coding required.
  • Works seamlessly with the Weapon State Machine, allowing weapons to behave dynamically.

Each weapon has a set of attributes that determine its combat behavior. These can be edited directly in the Godot Editor without any coding knowledge.

  • damage (int) – The amount of damage the weapon deals per attack.
  • windup_time (float) – The delay before an attack is executed after pressing the attack button.
  • attack_duration (float) – The time during which the weapon is actively attacking.
  • cooldown_time (float) – The delay before the weapon can be used again after an attack.
  • model (PackedScene) – The visual representation of the weapon in-game.

## WeaponResource: Defines weapon properties that can be used across different weapons.
class_name WeaponResource
extends BaseResource
# Weapon Attributes
@export var damage: int
# Timing Variables
@export var windup_time: float
@export var attack_duration: float
@export var cooldown_time: float
# Visual & UI Elements
@export var model: PackedScene
# Assigns this resource as a weapon type upon initialization
func _init() -> void:
type = ItemEnums.ItemTypes.WEAPON

To add a weapon and or modify weapon properties, follow these steps:

Section titled “To add a weapon and or modify weapon properties, follow these steps:”
  • In the FileSystem, right-click and select New Resource.
  • Choose WeaponResource as the resource type.
  • Save it as a .tres file (for example, sword.tres).
  • Open the newly created resource.
  • Adjust values like damage, windup time, and cooldown in the Inspector Panel.
  • Drag and drop the weapon model scene into the WeaponResource model property.

Now, the weapon will automatically use the updated stats in-game without changing any code!