Add Grenades #2

Open
opened 2026-04-27 20:06:21 +00:00 by OddlyTimbot · 1 comment
Owner

To add grenades, we will need the following:

  • Create a scene manager
  • Create a grenade
  • Scene manager limits how many grenades can be used

Player keyboard map:

	if Input.is_action_just_pressed("chuck"):
		print("ima chuck a grenade")
		match facing:
			FaceDirection.RIGHT:
				%SceneManager.makeGrenade(right_spawn.global_transform, 1)
			FaceDirection.LEFT:
				%SceneManager.makeGrenade(left_spawn.global_transform, -1)

In Scenemanager, grenade preload:
var grenade = preload("res://scenes/grenade.tscn")

Make a grenade and propel it:

func makeGrenade(_grenadePosition, _grenadeDirection)->void:
	print("SM make a grenade")
	var myGrenade:Grenade = grenade.instantiate()
	owner.add_child(myGrenade)
	myGrenade.apply_central_impulse(Vector2(_grenadeDirection, -1)*200)
	myGrenade.transform = _grenadePosition
To add grenades, we will need the following: - Create a scene manager - Create a grenade - Scene manager limits how many grenades can be used Player keyboard map: ``` if Input.is_action_just_pressed("chuck"): print("ima chuck a grenade") match facing: FaceDirection.RIGHT: %SceneManager.makeGrenade(right_spawn.global_transform, 1) FaceDirection.LEFT: %SceneManager.makeGrenade(left_spawn.global_transform, -1) ``` In Scenemanager, grenade preload: `var grenade = preload("res://scenes/grenade.tscn")` Make a grenade and propel it: ``` func makeGrenade(_grenadePosition, _grenadeDirection)->void: print("SM make a grenade") var myGrenade:Grenade = grenade.instantiate() owner.add_child(myGrenade) myGrenade.apply_central_impulse(Vector2(_grenadeDirection, -1)*200) myGrenade.transform = _grenadePosition ```
Author
Owner

Grenade code:

class_name Grenade extends RigidBody2D

var timer = Timer.new()

# Called when the node enters the scene tree for the first time.
func _ready():
	add_child(timer)
	timer.wait_time = 2
	timer.one_shot = true
	timer.connect("timeout", explode)
	timer.start()


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

func explode()->void:
	print("ima splode")
	self.queue_free()


func _on_body_entered(body):
	print("grenade hit something")
	if body.is_in_group("splodable"):
		body.queue_free()
		explode()
Grenade code: ``` class_name Grenade extends RigidBody2D var timer = Timer.new() # Called when the node enters the scene tree for the first time. func _ready(): add_child(timer) timer.wait_time = 2 timer.one_shot = true timer.connect("timeout", explode) timer.start() # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): pass func explode()->void: print("ima splode") self.queue_free() func _on_body_entered(body): print("grenade hit something") if body.is_in_group("splodable"): body.queue_free() explode() ```
OddlyTimbot added the
enhancement
label 2026-04-27 20:27:18 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: OddlyTimbot/AprilGameExample#2
No description provided.