Help a beginner with classes

Godot Version

4

Question

I want to make a Bullets class so that I can set parameters. For example, I call the shoot(position, damage, speed, crit) function. But I don’t understand how to write the code

I can give you an example in C# if it is relevant for you

Instead of thinking in terms of classes, you should think in terms of scenes/nodes.
So you can create a scene “Bullet”, add a script to it and export the desired properties to the inspector (“@export var speed = 10” for example). Other user-defined functions can also be written in this script, e.g. “shoot” (although this function would be better stored in a scene “Rifle” or something like that ;-)).
I hope this helps you get started. I’m still a beginner myself, I hope I didn’t write any nonsense :wink:

I figured it out. Adding a class for beginners

class_name classBullets
extends Area2D

var speed : int
var damage : int

func _init(_speed):
	speed = _speed


func _physics_process(delta):
	position += transform.x * speed * delta
	queue_redraw()
	
func _ready():
	var CPUParticles = CPUParticles2D.new()
	var area = Area2D.new()
	create_shape_owner(area)
	add_child(CPUParticles)
	
func _draw():
	draw_circle(Vector2(0,0), 10, Color.GREEN)

In the code for the character

var c_bullets = load("res://scripts/classBullets.gd")

func shoot()
	var pistol_bullet = c_bullets.new(100)
	owner.add_child(pistol_bullet)
	pistol_bullet.transform = $muzzle.global_transform

I know that it is possible to create a separate Scene and reference it. But for some reason I want to do it through code. Thanks for the help to everyone

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.