damage boost powerup not working on pickup

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By blitzar12

hey guys, my damage boost powerup is not increasing the damage of the bullets when the player picks it up.
this is my damage powerup script which inherits a base powerups class:

# inherits the base class
extends Powerups

func _physics_process(delta):
	applyPowerup()

func applyPowerup():
	# checks if the bullet instance is in the scene before increasing the damage
	if is_instance_valid(Globals.bullet):
		Globals.bullet.damage = 100

any help would be appreciated

Edited to fix code formatting. In future posts, after pasting your code, select the code block, press the {} button to format it for the forum, and then check the Preview window before submitting to ensure it looks correct.

jgodfrey | 2023-02-11 17:18

There’s really not much that can go wrong in the posted code. I assume you’re just not getting inside this if block…

if is_instance_valid(Globals.bullet):
    Globals.bullet.damage = 100

… or, you’re not using the updated value of Globals.bullet.damage elsewhere in your code.

If you’re not getting inside that if block (add a print statement there if you’re not sure), it’s a matter of finding out why the is_instance_valid() check is failing.

There’s really not much else to suggest based on the limited code posted.

jgodfrey | 2023-02-11 17:23

ahh thank you, first time posting so wasnt sure how to format the code.

blitzar12 | 2023-02-12 11:05

ah k, so i tried putting a print inside the if statement and it worked. also i printed the damage in the bullet_body_entered function in the bullet script and it printed the boosted damage value 100, but then when i picked up the powerup the damage returned to its normal value 50 which is a bit weird.

this is the bullet script:

extends RigidBody2D

var explosion = preload("res://Explosion.tscn") # creates reference to the animation
export (int) var damage = 50

func _ready():
	Globals.bullet = self

func _on_Bullet_body_entered(body): # runs whenever the bullet collides with an object
	
	if !body.is_in_group("player"): # makes sure the collision object is not the player
		if "health_unit" in body:
				
				
				print(damage)
				body.health_unit.emit_signal("take_damage", damage)
		
		var explosion_instance = explosion.instance() # creates an instance of the animation
		
		explosion_instance.position = get_global_position() # sets the position of the animation
		# to play wherever the bullet is at that time
		
		get_tree().get_root().add_child(explosion_instance) # adds the instance to the game scene
		
		queue_free()  # removes the bullet from the scene

also i dont know if this will help but this is the script for the health unit that both my player and enemy scenes have:

extends Node

signal take_damage(amount)  # created a signal which is emitted when taking damage

export(int) var health := 100  # set the health value 
 
func _ready():
	connect("take_damage", self, "_on_take_damage") # connects the signal to its class
	
func _on_take_damage(amount):
	health -= amount  # decrements the health by a fixed amount each time a bullet collides
	
	if health <= 0:
		health = 0 # make sure the health does not become negative

blitzar12 | 2023-02-12 11:19

wait, i just realised i uncommented the physics_process function in the damage boost script:

func _physics_process(delta):
    applyPowerup()

which was the thing that was making my bullet’s damage always the boosted value and then return to the normal value when i picked up the powerup.

when i delete that physics function, the bullet’s damage is always the normal value 50 even when i pick the powerup so it doesnt change.

blitzar12 | 2023-02-12 11:37

https://www.reddit.com/r/godot/comments/lsosyi/i_am_trying_to_change_the_damage_of_the_bullet/

this is a very similar problem to mine, and the comment by “levirules” is a very good suggestion. i made a damage multiplier variable in the player script and added the line Globals.bullet.damage *= damageMultiplier after i added the instance of the bullet into the main game scene.

then in the damage boost script i set the multiplier to 3:

func applyPowerup():
	# checks if the bullet instance is in the scene before increasing the damage
	if is_instance_valid(Globals.bullet):
		print("bullet access")
		#Globals.bullet.damage = 100
		Globals.player.damageMultiplier = 3

but this still didnt work - the damage didnt change after i picked up the powerup

blitzar12 | 2023-02-12 11:55

So, it looks like you’ve gone to trouble to ensure that Globals.bullet.damage has the value you want, but I don’t see that you’re using that value when you assign the damage. In the _on_Bullet_body_entered(body) function, you’re simply assigning a local damage value (which is set to 50).

jgodfrey | 2023-02-12 16:28