Godot Version
Godot 4.3
Question
Hello! So, I am making the Items system for my game, and I watched a tutorial on how to make it using resources. So then I tried to implement this to my project, I made an Item class and a Weapon class that extends from the Item class:
item.gd:
extends Resource
class_name Item
@export var item_name: String
@export var item_description: String
@export var item_icon: Texture
@export var item_scene: PackedScene
func _ready() -> void:
pass
weapon.gd
extends Item
class_name Weapon
@export var weaponlevel: int
@export var weapontype: String
@export var fire_rate: float
@export var range_size: int
@export var damage: int
@export var knockback: int
const DAMAGE_VARIATION: float = 0.1 # 10% de variação fixa
func _ready() -> void:
pass
func dmg_var():
var variation = randf_range(-DAMAGE_VARIATION, DAMAGE_VARIATION) # Varia entre -10% e +10%
var final_damage = damage * (1.0 + variation)
return final_damage
Then I have the scene of one of my weapons, which is an Area2D node:
extends Area2D
@export var sprite: Node2D
@export var timer: Timer
@export var audio_stream: AudioStreamPlayer2D
@export var gunrange: CollisionShape2D
@export var animation_player: AnimationPlayer
@export var item_data: Weapon
func _ready() -> void:
print(item_data.item_name)
func _physics_process(delta: float) -> void:
gunrange.scale = Vector2(item_data.range_size, item_data.range_size)
var enemies_in_range = get_overlapping_bodies()
timer.wait_time = item_data.fire_rate
if enemies_in_range.size() > 0:
var target_enemy = enemies_in_range.front()
look_at(target_enemy.global_position)
# Verifica se a rotação exige espelhamento
update_weapon_flip()
# LĂłgica de disparo
if timer.is_stopped():
timer.start()
else:
# Se nĂŁo houver inimigos no range, desativa o timer
if not timer.is_stopped():
timer.stop()
func update_weapon_flip() -> void:
# Posição global do pivot (arma) e do inimigo
var enemy_pos = get_overlapping_bodies().front().global_position
var player_pos = global_position
# Verifica se o inimigo está à esquerda ou à direita do jogador
if enemy_pos.x < player_pos.x:
sprite.scale.y = -4 # Espelha se o inimigo está à esquerda
else:
sprite.scale.y = 4 # Mantém posição padrão se o inimigo está à direita
func shoot():
const BULLET = preload("res://scenes/bullet.tscn")
var new_bullet = BULLET.instantiate()
var final_damage = item_data.dmg_var()
new_bullet.dmg = final_damage
new_bullet.knockback = item_data.knockback
new_bullet.global_position = %ShootingPoint.global_position
new_bullet.global_rotation = %ShootingPoint.global_rotation
%ShootingPoint.add_child(new_bullet)
audio_stream.play()
if animation_player.is_playing():
animation_player.stop()
animation_player.play("shoot")
animation_player.queue("idle")
func _on_timer_timeout() -> void:
shoot()
The problem is: @export var item_scene: PackedScene
I can’t get the item_scene in my pistol script because of recursion, I wanted to be able to spawn my weapon scene and add it to my inventory at the same time by having the scene in the Weapon Resource, but I can’t find a way to get the stats for the weapon in other efficient way from it’s own weapon class resource. Is there a better and efficient way to do this than just removing the weapon scene from the class? Or is it better to just handle the weapon scene and the inventory separately? In this case I would add an instance of item_data to the inventory whenever the player obtains the weapon. Sorry if this is confusing, english is not my native language :")