attempting to create code for a gun in a side-scrolling game

Godot Version

4.2.2

Question

Hello, Godot community.

I am currently working on a side-scrolling action game as of now and for a while, I have attempted to implement a gun in said game

so essentially, the way i want my gun to work is like this:
diagram

It’s really straightforward, the gun moves left and right in accordance to the player’s current direction, be unequipped any time, and have an ammo system

I have attempted at implementing the gun various times, although none of my attempts really worked, as I am still relatively new to GDScript.

and also, here’s my complete CharacterBody2D script as a reference (I have ripped some of the GDScript code from the official 2D Platformer Demo although I have made a half-baked implementation of the gun code from that):

extends CharacterBody2D


const SPEED = 300.0
const JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
@export var action_suffix := ""

var gun = get.node(gun)

# HALF LIFE 3 CONFIRMED 


func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y += delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	var is_shooting := false

	if Input.is_action_just_pressed("shoot" + action_suffix):
		is_shooting = gun.shoot()

	move_and_slide()
	

func _input(_event):
		if Input.is_action_just_pressed("ui_cancel"):
			get_tree().quit()
			print ("bye bye bitch!");

honestly, any help, suggestions, or advice would be helpful and pretty much welcome

probably meant one of these

@onready var gun = $gun
@onready var gun = get_node("gun")

Otherwise seems like a working script; did you run into any errors or problems?

I’ve done some of that in my 2D Helicopter demo, and will am happy to send you any code or bits you need.

I used a marker2D to attach the cannon to the front of the helicopter and created a separate scene for the rocket - rocket.tscn - as once the rocket leaves the player it needs to behave independently.

I’ll try to put a video on my YouTube channel today with more detail and my code.

cool, i honestly love having some mechanics be explained in video form

yes, i have run into an issue with this code before

image

so i have attempted to fix up the code recently, and here’s what I got:

and apparently, if I were to prefix is_shooting with an underscore

it gives me another error

perhaps my implementation is half-baked

You need to match the node name’s capitalization

@onready var gun = $Gun

is_shooting is set but never used; you could delete it or maybe you intend to use it later, what is it’s purpose?

1 Like

copied from the official Godot 2D platformer code I mentioned earlier

is_shooting isn’t used in the demo files either, I wouldn’t take the demo as gospel, maybe they disabled that warning but you can delete the variable.

Keep baking until it’s fully baked! You’ll get there.

I borrowed some of the code from here, and then made it much more complicated or “feature complete”

https://kidscancode.org/godot_recipes/4.x/2d/2d_shooting/

Case sensitivity has killed me several times!

Here’s some parts of my code:

var rocket = preload("res://scenes/rocket.tscn")
func shoot():
	shot_pause = true
	var b = rocket.instantiate()
	owner.add_child(b)
	b.transform = $choppa_sprite/cannon.global_transform
	await get_tree().create_timer(0.5).timeout 
	shot_pause = false

I’ve done a recording of my game code on YouTube. May or may not be helpful: