Variable wont become true

Godot Version

4.2.1

Question

The heal variable wont turn true. The damagenow variable will however. Here’s the scripts:

Code for the health

extends Label
var damagenow = false
var damagePerSecond = 0.1
var health: = 250
var heal = false
func _ready():
	
	pass




func set_health_label():

	
	text = str(health) 
	


func _process(delta):
	set_health_label()
	if damagenow == true:
		health -= delta
	if heal == true:
		health += delta
	
	##if Input.is_action_just_pressed("ui_accept"):
		##heal = true
	if health < 0:
		health = 0
	if health == 0:
		notify_property_list_changed()
	pass


func _on_ghost_area_entered(area):
	if area.is_in_group("PlayBullet"):
		heal = true
		pass # Replace with function body.


func _on_ghost_body_entered(body):
	if body.is_in_group("Player"):
		damagenow = true



		pass # Replace with function body.


func _on_ghost_body_exited(body):
	if body.is_in_group("Player"):
		damagenow = false
	pass # Replace with function body.





Code for ghost: (enemy that damages you and heals you when you kill them)

extends Area2D
var disappear = false
@onready var body = $CollisionShape2D
@onready var disappeartimer = $disappeartimer

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


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if disappear == false:
		$AnimationPlayerGhost.current_animation = "GhostFloat"
	else:
		$AnimationPlayerGhost.current_animation = "GhostFade"
	pass













func _on_area_entered(area):
	if area.is_in_group("PlayBullet"):
		disappear = true
		$CollisionShape2D.queue_free()
		$Explosion.emitting = true
		$Explosion2.emitting = true
		area.queue_free()
		disappeartimer.start()
		
	pass # Replace with function body.




func _on_timer_timeout():
	queue_free()
	pass # Replace with function body.


func _on_body_entered(body):

	pass # Replace with function body.

did you check if this actually called?
try print(“something”)

Yes, I already checked if it’s called by adding health to my charater like this.

func _on_ghost_area_entered(area):
	if area.is_in_group("PlayBullet"):
		health += 20

I want the health to be gained gradually instead of instantly. This is the reason why this is in the code

if heal == true:
		health += delta

Don’t use delta.
delta is a float value and if it is less than 1 it gets integerized and you add 0 to health.
Note this code:

var f:float
var n:int = 250
f = .345
printt(f,n, n + f)
n += f
print(n)

Yields this output:

0.345	250	250.345
250

You are either going to have to change health into a float or use an accumulating variable and add to health when delta + accumulator reaches >= 1.

And use static typing not :=

var health:int = 250     

You will make debugging significantly easier. You gain nothing by type inferencing using :=

1 Like

I took what you said and reserached about it and thius is what Ive came up with:

extends Label
var damagenow:bool = false #made this bool
var damagePerSecond: = 1 
var health:int = 250     ## turned this into int
var heal:bool = false #made this bool
func _ready():
	
	pass




func set_health_label():

	
	text = str(health) 
	


func _process(delta):
	set_health_label()
	if damagenow == true:
		health -= delta 
	if heal == true:
		health += 100 * delta # got big number and times it by delta
		var timer : Timer = Timer.new() #made timer it can add only a certain amount.
		add_child(timer) 
		timer.one_shot = true
		timer.autostart = false
		timer.wait_time = 1
		timer.timeout.connect(func():  heal = false)
		timer.start()
	
	##if Input.is_action_just_pressed("ui_accept"):
		##heal = true
	if health < 0:
		health = 0
	if health == 0:
		notify_property_list_changed()
	pass


func _on_ghost_area_entered(area):
	if area.is_in_group("PlayBullet"):
		heal = true
		pass # Replace with function body.


func _on_ghost_body_entered(body):
	if body.is_in_group("Player"):
		damagenow = true



		pass # Replace with function body.


func _on_ghost_body_exited(body):
	if body.is_in_group("Player"):
		damagenow = false
	pass # Replace with function body.




Thanks for your help!

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