#dynamically setup an AudioStreamPlayer

66 messages · Page 1 of 1 (latest)

gaunt scarab
#

hello everyone! I currently am trying to dynamically set up an audio stream player so that different weapon types play their own respective sound when hitting an enemy. I currently have this setup working for otgher various data types but I have not been able to get it to work with audio. Any advice would be greatly appreciated! Code below for reference. The code below here is the Damage Component swift # allows for various attack attributes from the home brewed Attack class func new_attack(area): enemy_knockback() var attack = Attack.new() var hitbox : HitboxComponent = area attack.attack_damage = attack_damage attack.knockback_force = knockback_force attack.attack_size = attack_size attack.hp = hp attack.target = target attack.angle = angle attack.hit_sound = hit_sound attack.knockback_direction = knockback_direction hitbox.damage(attack) hitbox.enemy_knockback(attack) attack.hit_sound = hit_sound inside this code block above we are creating an attack instance with the values that are passed via other methods. the code below will show where the relevant code is coming from. the code below here extends the damage component. swift ``` func _ready():
angle = global_position.direction_to(target)
rotation = angle.angle() + deg_to_rad(135)

match level:
    1:
        hp = 1
        speed = 150
        attack_damage = 5
        knockback_force = 100
        attack_size = 1.0
        HurtBoxType = 1
        hit_sound = $HitSound ``` The hit sound when printed shows a unique audiostream player however when I pass this information to its parent class aka the Damage component when I try to call this value I get a NIL value for some reason. if you have any other questions let me know!!
#

this is also an image of the structure of the arrow that I want to have the unique sound. the idea is that I could have different weapons and drop in the specific audio file to be passed on so the correct sound plays when hit.

quiet maple
#

im here

gaunt scarab
#

this is my entire damage component script ```func _on_area_entered(area):
if area is HitboxComponent:
match HurtBoxType:
0: #Cooldown
collsion.set_deferred("disabled", false)
disableTimer.start()
1: #HitOnce
if hit_once_array.has(area) == false:
hit_once_array.append(area)
enemy_hit(area, 1)

                if area.has_signal("remove_from_array"):
                    if not area.is_connected("remove_from_array", Callable(self, "remove_from_list")):
                        area.connect("remove_from_array", Callable(self, "remove_from_list"))
            else:
                return
        2: #DisableHitBox
            area.tempdisable()

    new_attack(area)``` and basically how it works is that when a hurtbox node hits a hitbox node this method is called. I can give you the code for the other nodes if you need as well let me know what you need for clarity
#

oh weird it didnt send it all

#

give me a second and ill send it in chunks

#

class_name DamageComponent

@onready var collsion = $CollisionShape2D
@onready var disableTimer = $DisableTimer


var attack_damage: float
var knockback_force: float
var angle: Vector2
var target: Vector2
var attack_size: float
var hp: int
var speed: float
var level: int
var knockback_direction: Vector2
var hit_sound: AudioStreamPlayer

@export_enum("Cooldown", "HitOnce", "DisableHitBox") var HurtBoxType = 0

# attacks added to this array when hitting an enemy so they are not to hit the same enemy twice
var hit_once_array = []

# handles all damage types including bullet and collision based damaged
func _on_area_entered(area):
    if area is HitboxComponent:
        match HurtBoxType:
            0: #Cooldown
                collsion.set_deferred("disabled", false)
                disableTimer.start()
            1: #HitOnce
                if hit_once_array.has(area) == false:
                    hit_once_array.append(area)
                    enemy_hit(area, 1)
                    
                    if area.has_signal("remove_from_array"):
                        if not area.is_connected("remove_from_array", Callable(self, "remove_from_list")):
                            area.connect("remove_from_array", Callable(self, "remove_from_list"))
                else:
                    return
            2: #DisableHitBox
                area.tempdisable()

        new_attack(area)
        
        ```
#
func _on_disable_timer_timeout():
    collsion.call_deferred("set", "disabled", false)
    
    
    
# allows for various attack attributes from the home brewed Attack class
func new_attack(area):
    enemy_knockback()
    var attack = Attack.new()
    var hitbox : HitboxComponent = area
    attack.attack_damage = attack_damage
    attack.knockback_force = knockback_force
    attack.attack_size = attack_size
    attack.hp = hp
    attack.target = target
    attack.angle = angle
    attack.hit_sound = hit_sound
    attack.knockback_direction = knockback_direction
    hitbox.damage(attack)
    hitbox.enemy_knockback(attack)
    
    
    
func enemy_hit(area, charge = 1):
    if not hit_once_array.has(area):
        hit_once_array.append(area)
    hp -= charge
    if hp <= 0:
        if hit_once_array.has(area):
            hit_once_array.erase(area)
        queue_free()
    
        

func enemy_knockback():
    knockback_direction = angle * knockback_force
    
func remove_from_list(object):
    if hit_once_array.has(object):
        hit_once_array.erase(object)
#

this is the chunk of code that calls the new_attack swift```func _on_area_entered(area):
if area is HitboxComponent:
match HurtBoxType:
0: #Cooldown
collsion.set_deferred("disabled", false)
disableTimer.start()
1: #HitOnce
if hit_once_array.has(area) == false:
hit_once_array.append(area)
enemy_hit(area, 1)

                if area.has_signal("remove_from_array"):
                    if not area.is_connected("remove_from_array", Callable(self, "remove_from_list")):
                        area.connect("remove_from_array", Callable(self, "remove_from_list"))
            else:
                return
        2: #DisableHitBox
            area.tempdisable()

    new_attack(area)```
quiet maple
#

is this all on the same script?

gaunt scarab
#

yeah this is all on the DamageComponent

#

this data is set within the arrow component that I sent earlier

#

then it is processed here

quiet maple
#

so this is the arrow

 func _ready():
    angle = global_position.direction_to(target)
    rotation = angle.angle() + deg_to_rad(135)
    
    match level:
        1:
            hp = 1
            speed = 150
            attack_damage = 5
            knockback_force = 100
            attack_size = 1.0
            HurtBoxType = 1
            hit_sound = $HitSound 
gaunt scarab
#

yes correct, also how do you make the code block colorful? I know you type swift but I mess it up some how lol,.

quiet maple
#

and when it enters the damagecomponent _on_area_entered is triggered on the damage component, creating a new attack with the arrow's area

gaunt scarab
#

yes sir

quiet maple
gaunt scarab
#

ahhh I see thanks!

quiet maple
#

can you show me the scene tree of the damage component?

#

also, can you paste the exact error youre getting?

gaunt scarab
#

yeah one moment

#

the script that is attached to this is the damagecomponent script

#

im working on getting the error code now

#

actually I think I know why I was getting the error before

#

I changed the code last night so that the error would stop but I just reimplemented it. the sound doesnt play but maybe it is how I am calling the sound.play

quiet maple
#

so uh, the issue is solved?

gaunt scarab
#

uh not yet I think the issue I was having was that I was calling the hit_sound.play within the new_attack method so it was trying to play the sound as the attack was instansiated rather then when the on area entered method was called

#

I guess where I'm at now

#

is trying to figure out how to properly call the sound to play

#

but I got the error to leave

quiet maple
#

sorry without having the project its just confusing to understand whats happening. so what is happening now and what would you like to happen?

gaunt scarab
#

yeah I aplogize for that I know it's a bit confusing. basically I want the hit sound to play when the arrow impacts an enemy and it is currently not playing anything. If you want I can screen share so you can see the what is happening in real time.

#

I dont think I am calling the hit sound properly. I belive the data is being passed because I am getting unique values from my print statements

#

but I'm also not entirely sure

quiet maple
#

no need to apologise its all good

gaunt scarab
#

I actually just put in some print statements

#

and I think i might of found the issue, just not sure why its happening

#

give me a second and I will paste them

#

I want to get a fresh set of logs

quiet maple
#

i cant see where youre calling the hit sound in any of the code youre sending. also im having difficulty understanding the code flow. i understand that the arrow passes it's area to the damage component. but im not seeing how the arrow is passing its parameters to it. also i dont understand why the damage component is storing all these parameters when you seem to already have an Attack resource that has all of these, so why not just give it an attack resource? sorry im just really confused about the entire setup

gaunt scarab
#

so here are the values printed from the two scripts HitSound:<AudioStreamPlayer#40181433761>inside of new_attack
HitSound:<AudioStreamPlayer#40701527457>inside of arrow
HitSound:<AudioStreamPlayer#40701527457>inside of new_attack
HitSound:<AudioStreamPlayer#41137735073>inside of arrow
HitSound:<AudioStreamPlayer#43301995959>inside of arrow
HitSound:<AudioStreamPlayer#45734692301>inside of arrow
HitSound:<AudioStreamPlayer#48351938029>inside of arrow
HitSound:<AudioStreamPlayer#51271173637>inside of arrow
HitSound:<AudioStreamPlayer#48351938029>inside of new_attack
HitSound:<AudioStreamPlayer#51271173637>inside of new_attack
HitSound:<AudioStreamPlayer#53317993989>inside of arrow
<null>inside of new_attack
HitSound:<AudioStreamPlayer#53317993989>inside of new_attack
HitSound:<AudioStreamPlayer#55415145989>inside of arrow
HitSound:<AudioStreamPlayer#55415145989>inside of new_attack
HitSound:<AudioStreamPlayer#57931728302>inside of arrow
HitSound:<AudioStreamPlayer#57931728302>inside of new_attack
HitSound:<AudioStreamPlayer#61874374147>inside of arrow

#

the arrow is called when its instansiated and the new attack on impact

#

which is why they are out of order however, theres a random null value in there

quiet maple
#

how big is the project? is it possible for you to send it for me to take a look? that would be a lot easier because im just really confused about whats going on so im not sure how helpful i can be

gaunt scarab
#

the project is still pretty early on I think I could send it over. I need to set up git so this would be easier. what is the easiest way to get it to you email?

#

also the null value happened when I collided the player with the enemy which is expected because I dont have that sound implemented yet

#

do you want me to screen share for you real quick before I send it over?

quiet maple
#

no no, simply zip up the project files, you can omit the .godot folder to save a bunch of space. feel free to DM it if you arent comfortable sending it publicly here

quiet maple
gaunt scarab
#

thats perfectly okay 🙂

#

there is one bug with godot so when you get the project you have to open up the level node and click on "run current scene" of f6 is you just hit the regular play button it loads an old version of the game with nodes that I deleted. Don't worry about fixing that I just want to give you a heads up because it is a strange behavior. lol

quiet maple
#

hmm ok never encountered it but i'll take note

gaunt scarab
#

yeah me either with the few other projects Ive done, I noticed this the other day and just havent given it any time to fix it.

quiet maple
gaunt scarab
#

I just sent it over to you

#

I appreciate you taking the time to look into this for me 😄

#

also, let me know what you think about my readability of my code, I have a component approach with how I am building things currently

quiet maple
#

just, what scenes should i be focusing on?

quiet maple
gaunt scarab
#

thats all good, what you should focus on is the damage component script

#

and the arrow script

#

if you want to look at the collision its the hitbox and hurtbox nodes

#

where the hitbox handles taking damage and the hurtbox (using the damage component) handles damage dealing

#

if you open up the "player" and the "enemy_bat_weak" you will see those most of those nodes there and you can click on their scripts and move between them easily