How to create weapons with different damage [BEGGINER QUESTION]

Godot Version



Hey! Im making my first top-down shooter game. Im pretty new to godot but i have quite experience with programming, as im studying it. Im currently working on my weapon system: I have created a Base Class “Gun”, and i have some weapons like pistol or rifle, etc.
On the other hand, i have a “bullet” scene that inherits from RigidBody2D and defines its Sprite2D and CollisionShape2D.
My problem is, i want different weapons to make different amount of damage, every weapon has its own damage attribute. So, i made a class “Bullet”, wich inherits from RigidBody2D as well, and i created a “damage” attribute, wich i pass in the constructor. Anyway, thats not quite working. because at the moment of instanciating the bullet escene, im not sure how to instantiate my bullet class, and i keep instantiating a PackedScene. If i try to create a directly, the sprite and collitionShape do not exist. I have to clarify that my bullet scene Base Node is a Bullet type node. So i dont understand.

Anyhow, im sure this is not the right way to do this, i was just playing around trying to understand everything. I would apreciate word on my problem, or a different solution, cause i havent found tutorials on this.

Thanks in advance guys! (you may have noticed english is not my first tongue so please excuse my mistakes)

This is a discussed problem, PackedScene cannot be instantiate with parameters.
You cannot add attributes in the constructor (_init function) or the instantiation will fail. But you can define variable just after the instanciation and before adding your bullet as a child of something.

Instantiate the .tscn file, apply required changes, then add as a child in the scene tree

# gun script
@export var damage: int = 3

const BULLET_SCENE = preload("res://Bullet.tscn")
func shoot() -> void:
    var bullet = BULLET_SCENE.instantiate()
    bullet.damage = damage
    # position and rotation may need tweaking
    bullet.position = position
    bullet.rotation = rotation

    # adding to scene

im doing exactly this but it doesnt work, i think im referencing something wrong:

disclaimer: you ll see i code in spanish,
Bala = Bullet
fuego = fire
danio = damage

this is my fire function:

const bala = preload("res://bala.tscn")

func fuego():
	var inst_bala = bala.instantiate()
	inst_bala.danio = danio
	inst_bala.position = get_node("Punta").global_position
	inst_bala.rotation_degrees = rotation_degrees + 90
	#le aplico impulso fisico en la direccion del jugador (uso el atributo rotation)
	inst_bala.apply_impulse(Vector2(velocidad_bala, 0).rotated(get_parent().get_parent().rotation))
	#le aplico impulso fisico en la direccion del jugador (uso el atributo rotation)
	#la instancio como hija de la escena principal

this is my Bullet class:

extends RigidBody2D

class_name Bala
var danio = 0

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.

func _init(danio_arma):
	danio = danio_arma

func setear_danio(danio_arma):
	danio = danio_arma

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):

dont bother on the constructor i realised PackedScene doesnt work that way jeje

and the error when i shoot:
Invalid set index 'danio' (on base: 'RigidBody2D') with value of type 'int'

I mean, its like its not taking the Bullet class, but RigidBody2D, but i defined the Bullet class

hey, and thanks for answering