Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Newby |
Example i make an area that keeps damaging the player as long he is in it how do i do that do i need a for or while loop for it
Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Newby |
Example i make an area that keeps damaging the player as long he is in it how do i do that do i need a for or while loop for it
Reply From: | SingingApple |
I don’t know how you can do that using a for/while loop but I think timers should work.
In the player script,
timer = null
health = 1000
func health_decrement():
health -= 50 # Amount to decrement from health in 0.25 secs
func _ready():
timer = Timer.new()
timer.set_one_shot(false)
timer.set_wait_time(0.25) # 0.25 is just an example.
timer.connect("timeout", self, "health_decrement")
add_child(timer)
In the area2d script,
timer = get_node("player/timer")
func on_area_body_enter(body):
timer.start()
func on_area_body_exit(body):
timer.stop()
This should work!
Another option:
On fixed/physics process check for overlapping bodies and apply your damage there (this means constant overlapping body checks).
And you do not need a loop, the game loop does that for you.
eons | 2018-02-09 09:50
@eons I agree.
SingingApple | 2018-02-09 10:25
can you give an example pls
Newby | 2018-02-09 13:20
Reply From: | Footurist |
I would follow the advice of eons.
Here’s an example on how to do that in code:
func _physics_process(delta): # get's called every physics step
if Area2D.overlaps_body(PhysicsBody2D): # returns bool true/false
damage_player()
if you’re writing that code in the Area2D node, you won’t need to call the function explicitly on the object (as in self.call() like with other languages).
I was considering this while writing my answer. I am still kind of a noob in Godot but have you considered frame-rates. Won’t devices with higher frame-rates ‘damage_player()’ quicker than other devices? Or will it not?
SingingApple | 2018-02-12 18:07
You can suffer the fate of some AAA games at different framerates if using regular process, physics process is usually locked at 60 fps, but yes, a safe option could be to do damage_player(raw_damage_per_second/delta)
.
eons | 2018-02-13 16:55
@eons Got it! Thanks!
SingingApple | 2018-02-14 08:21