#WHY

69 messages · Page 1 of 1 (latest)

harsh shadow
#
extends Sprite

var speed = 80

var velocity = Vector2()
var stun = false
var hp = 3

var blood_particles = preload("res://Blood_Particles.tscn")

onready var explode_sound = $Explode

func _process(delta):
    if Global.player != null and !stun:
        velocity = global_position.direction_to(Global.player.global_position)
        
    elif stun:
        velocity = lerp(velocity, Vector2(0, 0), 0.3)
        
    global_position += velocity * speed * delta

    if hp <= 0:
        Global.points += 10
        if Global.node_creation_parent != null:
            var blood_particles_instance = Global.instance_node(blood_particles, global_position, Global.node_creation_parent)
            blood_particles_instance.rotation = velocity.angle()
            
            if explode_sound:
                explode_sound.play()  
            
        queue_free()

func _on_Hitbox_area_entered(area):
    if area.is_in_group("Enemy_damager") and !stun:
        modulate = Color.white
        velocity = -velocity * 6
        hp -= 1
        stun = true
        $Stun_Timer.start()
        area.get_parent().queue_free()


func _on_Stun_Timer_timeout():
    modulate = Color("ff4848")
    stun = false

faint coral
#

did you make a thing which stops it from restarting constantly?

harsh shadow
#

wdym?

faint coral
#

like

harsh shadow
faint coral
#

lets say you want to play a jump sound
if you are not touching the ground the sound plays, but it keeps trying to play the same audio again and again while youre in the air

#

something like that

sudden elm
#

As an explanation of what's (likely) happening, any code you put in process will run every frame (60 times a second). The way it's set up it's running the code to start the audioplayer every frame.

harsh shadow
#

uhm

#

the sound doesnt even play tho😂

faint coral
sudden elm
#

You won't hear it. An audiostreamplayer has a single track, and that track is being restarted every 1/60th of a second

faint coral
#

coudl be the case

harsh shadow
#

;0

sudden elm
#

So it's playing, it's just playing the first 1/60th of the file over and over, basically

harsh shadow
#

oh

#

so..how do I play the sound normally?

faint coral
#

maybe try something like this

harsh shadow
#

shall I just take the code outside the _process func

#

hmm

faint coral
sudden elm
#

The easy way is to wrap the method call to start the player in an if statement that checks if it's already playing.

         explode_player.play()```
#

Don't copy/paste, the indenting is all wrong. I'm on phone.

harsh shadow
#

    if hp <= 0:
        Global.points += 10
        if Global.node_creation_parent != null:
            var blood_particles_instance = Global.instance_node(blood_particles, global_position, Global.node_creation_parent)
            blood_particles_instance.rotation = velocity.angle()
            
            if explode_sound && !explode_sound.playing:
                explode_sound.play()
            
        queue_free()
harsh shadow
#

Invalid get index 'is_stopped' (on base: 'AudioStreamPlayer').

#

this happen :<

sudden elm
#

Oh, I think it's a method, not a var. Try adding parentheses to the end of is_stopped()

#

(Sorry. At work)

#

It should call the method is_stopped() on the audioplayer and return true or false for the if to evaluate

#

Try if explode_sound && explode_sound.is_stopped():

harsh shadow
#
    if hp <= 0:
        Global.points += 10
        if Global.node_creation_parent != null:
            var blood_particles_instance = Global.instance_node(blood_particles, global_position, Global.node_creation_parent)
            blood_particles_instance.rotation = velocity.angle()
            

            if explode_sound && explode_sound.is_stopped():
                explode_sound.play()
            
        queue_free()

#

Invalid call. Nonexistent function 'is_stopped' in base 'AudioStreamPlayer'.

#

this still happens 🥲

sudden elm
#

Oh, you're in 4 aren't you

#

For godot 4, try checking not is_playing

faint coral
#

oh right

sudden elm
#

is_stopped is 3.x, my bad

harsh shadow
#

oh

#

I am on 3.5

sudden elm
#

I feel so dumb, as I just used the is_stopped functionality on my own project in 3.5 this morning

harsh shadow
#

oh XD

#

hmmm so hard on these to solve..

sudden elm
#

Eh, it's something obvious that'll make us kick ourselves when we solve it. That's dev 🙂

#

When you tried is_stopped did you try it with and without parentheses? I swear one of those should work.

harsh shadow
#
    if hp <= 0:
        Global.points += 10
        if Global.node_creation_parent != null:
            var blood_particles_instance = Global.instance_node(blood_particles, global_position, Global.node_creation_parent)
            blood_particles_instance.rotation = velocity.angle()
            
        if explode_sound && explode_sound.is_stopped:
                explode_sound.play()
#

like this?

#

Invalid get index 'is_stopped' (on base: 'AudioStreamPlayer').

#

this happens still :(

sudden elm
#

Hm. I'm probably missing something very obvious.

harsh shadow
#
extends Sprite

var speed = 80

var velocity = Vector2()
var stun = false
var hp = 3
var explode_sound_instances = []

var blood_particles = preload("res://Blood_Particles.tscn")

onready var explode_sound = $Explode

func _process(delta):
    if Global.player != null and !stun:
        velocity = global_position.direction_to(Global.player.global_position)
        
    elif stun:
        velocity = lerp(velocity, Vector2(0, 0), 0.3)
        
    global_position += velocity * speed * delta

    if hp <= 0:
        Global.points += 10
        if Global.node_creation_parent != null:
            var blood_particles_instance = Global.instance_node(blood_particles, global_position, Global.node_creation_parent)
            blood_particles_instance.rotation = velocity.angle()
            
            var sound_instance = explode_sound.play()
            if sound_instance != null:
                explode_sound_instances.append(sound_instance)
                
        queue_free()

func _on_Hitbox_area_entered(area):
    if area.is_in_group("Enemy_damager") and !stun:
        modulate = Color.white
        velocity = -velocity * 6
        hp -= 1
        stun = true
        $Stun_Timer.start()
        area.get_parent().queue_free()

func _on_Stun_Timer_timeout():
    modulate = Color("ff4848")
    stun = false

I have also tried this way as well but still
sound doesnt play

sudden elm
#

I'm sorry, I'm still at work for an hour or so. I'll check back when I can sit down at my battlestation and see what I see. I've done this...yesterday 😛

harsh shadow
#

:D sure!

#

take ur time

ionic falcon
#

Explode_sound is part of your enemy scene. You are playing the sound effect and then queue_freeing the enemy scene which is deleting the sound effect before it can play. You either need to wait to queue_free the enemy until the sound effect has finished playing or have the sound effect somewhere that does not get deleted.

sudden elm
harsh shadow
#

oh...

#

hmmm

harsh shadow
#

what will be the best idea to deal with?

#

I need the enemy to die right away when it's hp reaches 0

#

but if I set the time for the time for the sound to play and let queue_free

#

I think it would stay on the screen for a while?

#

I tried this on global script

extends Node

var node_creation_parent = null
var player = null
var camera = null
var explode_sound = preload("res://Sounds/Explode.mp3")
var enemy_death = false
var explode_sound_instances = []

var points = 0

func instance_node(node, location, parent):
    var node_instance = node.instance()
    parent.add_child(node_instance)
    node_instance.global_position = location
    return node_instance
    
func _process(delta):
    if enemy_death == true:
        var sound_instance = explode_sound.play()
        if sound_instance != null:
            explode_sound_instances.append(sound_instance)
        
    ```
#
func _process(delta):
    if Global.player != null and !stun:
        velocity = global_position.direction_to(Global.player.global_position)
        
    elif stun:
        velocity = lerp(velocity, Vector2(0, 0), 0.3)
        
    global_position += velocity * speed * delta

    if hp <= 0:
        if Global.camera != null:
            Global.camera.screen_shake(20, 0.1)
            
        Global.enemy_death = true
        Global.points += 10
        if Global.node_creation_parent != null:
            var blood_particles_instance = Global.instance_node(blood_particles, global_position, Global.node_creation_parent)
            blood_particles_instance.rotation = velocity.angle()
            
            var sound_instance = explode_sound.play()
            if sound_instance != null:
                explode_sound_instances.append(sound_instance)
                
        queue_free()

and this on enemy

#

any idea..?

harsh shadow
#
func _ready():
    explosion_sound = $Explode

func _process(delta):
    if Global.player != null and !stun:
        velocity = global_position.direction_to(Global.player.global_position)
        
    elif stun:
        velocity = lerp(velocity, Vector2(0, 0), 0.3)
        
    global_position += velocity * speed * delta

    if hp <= 0:
        if Global.camera != null:
            Global.camera.screen_shake(40, 0.1)
            
        Global.points += 10
        if Global.node_creation_parent != null:
            var blood_particles_instance = Global.instance_node(blood_particles, global_position, Global.node_creation_parent)
            blood_particles_instance.rotation = velocity.angle()
        
        explode()
        queue_free()
        
func explode():
    if explosion_sound:
        explosion_sound.play()
    while explosion_sound.is_playing():
        yield(get_tree(), "idle_frame")
    
    queue_free()
#

or is this a thing

#

still doesnt work..