godot sound won't play in script - godot

So in my coin script, I game it plays a file called coin.wav but I cannot hear it play
Code:
extends Area2D
signal coin_collected
func _on_coin_body_entered(_body):
emit_signal("coin_collected")
set_collision_mask_bit(0, false)
print("collected")
$coin_sound.play()
print("stared sound")
queue_free()
Scene:
Note: if t turn on autoplay it will play the sound so I know its not the file

alright it was because I was doing queue_free() before the track could play

We had exactly the same problem, however, we answered this question
by adding a slight time delay just after the play audio line
Actual Example
func _on_Gift_body_entered(body):
if (body.get_name() == "Player"):
GameManager.Score = GameManager.Score +10
$scoresnd.play()
yield(get_tree().create_timer(0.2), "timeout")
queue_free()

Related

Collision in Godot Engine, 2D game

I want the inscription "hello" to be displayed in case of a collision with an object! How to do it? And what should be used?
I tried to do this using the Area2D node, but nothing worked :(
I tried to use the following code:
func print_msg():
print("Hello")
func _on_area_entered(): (I attached it)
print_msg()
Before that, I made a player using KinematicBody2D and made him move. And made blocks for walking
Since you've left things very ambiguous I'll try to explain it to the best of what I understood
Here I have the basic setup with a RigidBody2D and Area2D:
And here's the main script:
extends Node2D
func message_area(thing):
print("Hello! (Area Entered)")
func message_body(thing): # you were probably missing this
print("Hello! (Body Entered)")
func _ready():
$Area2D.connect("area_entered",self,"message_area")
$Area2D.connect("body_entered",self,"message_body")
Now when you press play the rigid body will fall into Area2D and when it does it will output this:
Hello! (Body Entered)
My best guess is you only connected the area_entered signal but not the body_entered which is why nothing was being printed when the object entered the Area2D
You can further follow on how to connect signals & How Area2D works
And I'd recommend you post code or explain what you've tried (preferably with images) so people can understand your problem better and help better :)
Edit:
If you want to change the visibility of the thing entered/collided you have 2 options:
...
func message_body(thing):
print("Hello! (Body Entered)")
thing.visible=true #option 1
thing.modulate.a=0.0 #option 2 (you can also use self_modulate)
Option 1 is the equivalent of pressing the eye icon
Option 2 changes the alpha value i.e. decides how much transparent your object should be and lies between 0 & 1 where 0=completely invisible, 1=completely visible, 0.5=50% visible and so on...
Equivalent of changing :
However, since you said "with a certain object" you can add a condition before toggling the visibility:
func message_body(thing):
print("Hello! (Body Entered)")
if(thing is KinematicBody2D): # or you can create a custom class and use it's `class_name`
thing.visible=true
thing.modulate.a=0.0
# or
if(thing.name =="Main Player"):
thing.visible=true
thing.modulate.a=0.0
# etc etc
I recommend you read this if you already haven't

Tried making collectibles in Godot but the fade out animation of the item doesn't play and the playable character bumps onto them like they're enemies

So, the problem i'm struggling with is making collectibles disappear properly and making it so the playable character doesn't collide with them at all. My guess (i'm still very new to the engine) is that either the stomp mechanic of the playable character is somehow colliding with the collectibles' hitbox or i messed up the collectibles' hitbox settings.
My guess for the animation is that i missed a step while coding the animation switch, i still now pretty much nothing of the AnimatedSprite node since i've used the AnimationPlayer so far (someone has suggested me to use the AnimatedSprite node to animate 2D sprites instead of the AnimationPlayer so i'm trying it out)
Here follows a portion of the playable character's code.
#Stomp mechanic
func calculate_stomp_velocity(linear_velocity: Vector2, impulse: float) -> Vector2: #Schiacciare i nemici
var output = linear_velocity
output.y = -impulse
return output
func _on_EnemyDetector_area_entered(area):
_velocity = calculate_stomp_velocity(_velocity, stomp_impulse)
#Death
func _on_EnemyDetector_body_entered(body):
_velocity.y = 0.0
queue_free()
Here is instead the collectibles' code (the collectible's name is "Leaf")
extends Area2D
onready var _animated_sprite = $AnimatedSprite
func _process(delta):
_animated_sprite.play("Floating")
func _on_Leaf_body_entered(KinematicBody2D) -> void:
_animated_sprite.stop()
_animated_sprite.play("Fade")
queue_free()
The function queue_free will delete the node between frames. Thus, in this code:
_animated_sprite.play("Fade")
queue_free()
The animation does not get a chance to be seen.
I suggest you yield to the "animation_finished" signal:
_animated_sprite.play("Fade")
yield(_animated_sprite, "animation_finished")
queue_free()
When Godot is executing the code and encounters yield, it will stop executing the function. And when the signal ("animation_finished") is emitted, the Godot will resume the exeution. That way the animation gets to play before the node is removed.
You may also want to disable the Area2D. For example: set_deferred("monitoring", false). So it does no longer trigger "body_entered". I will also mention that it is a common practice to use node groups to identify nodes on collision.

Teleporting node

I am trying to teleport my player node(kinematicbody2d) when it hits my finish node (area2d) from the side of the Finish node
BTW I'm using godot 3
What I tried:Change the location using get_node("player").set_pos and get_node("player").location
code:
extends Area2D
func _on_Finish12_body_entered(body):
if body.get_name() == "player":
print("%s touched the finish on level %s" % [body.get_name(), get_tree().get_current_scene().get_name()])
get_node("player").position = Vector2(1504, 1896)
pass
So what I need:
The playing being teleported to 1504, 1896
There's a lot of unknowns here that can be the problem
Is the player's location updated in other parts of the code? and if so, could it be possible you did move to 1504, 1896 but then immediately get clobbered by said code?
What is the current behavior when you apply the new position? Does your player move at all? Does it go somewhere unintended?
Does your print statement execute?
Have you tried using move_and_slide/move_and_collide for the kinematicBody to check for collision?
Just a few ideas on how to go about figuring it out.
This is what works with Area and KinematicBody (i.e. 3D):
extends Area
func _on_Area_body_entered(body):
body.look_at_from_position(spawn, Vector3(0,0,0), Vector3(0,0,0))
with spawn being an empty spatial to define the point in space to teleport to.

Corona: Audio does not begin playing again after scene is re-created

I am trying to play background sound, only during one scene. When this scene is destroyed, the sound should fade out and then stop. Then when the scene is re-created, the sound should start playing again.
Here is what I am trying:
local backgroundMusic
local backgroundMusicChannel
local function stopSound(event)
audio.stop(event.channel)
audio.dispose(event.handle)
backgroundMusic = nil
end
function scene:create(event)
backgroundMusic = audio.loadSound("music/intro.mp3")
backgroundMusicChannel = audio.play(backgroundMusic, {loops = -1, fadein = 3000, onComplete = stopSound})
end
function scene:destroy(event)
audio.fadeOut({channel = backgroundMusicChannel, time=2000})
end
The sound plays when the scene is created initially, and fades out correctly, but does not start again when the scene is created again.
When I change the destroy function to:
function scene:destroy(event)
audio.fadeOut({channel = backgroundMusicChannel, time=2000})
audio.stop(backgroundMusicChannel)
end
the sound does not fade out, because it stops immediately, but it DOES start again correctly the next time the scene is created.
I have also tried using audio.stopWithDelay, which fades out correctly, but similarly, does not begin playing again when the scene is created once more.
If anyone has any insights on what's going on here please let me know!
Could the problem be you cannot (re)play the sound if it isn't stopped yet?
You also never dispose of the audio when the scene ends.
Inside create, stop the previous audio if it already exists.
You're better off creating the sound once and reusing it when needed.
If you use loadStream, you'll have to use audio.rewind() after playing it.
The documentation on this also states that you should probably use loadStream:
https://docs.coronalabs.com/guide/media/audioSystem/index.html

Random Audio Playing - Unity3d

I don't know what is wrong with my script, at the moment I have a main menu music playing and this script is meant to overlap and evil laugh sound over the main menu music at a random time different every time.
var soundToPlay : AudioSource;
function Start(){
Invoke("PlaySound", Random.Range(5.0, 20.0));
}
function PlaySound(){
if (!soundToPlay.isPlaying)
soundToPlay.Play();
Invoke("PlaySound", Random.Range(5.0, 20.0));
}
I did set the audio source but I get this error (I think when the audio is going to play) not at build:
MissingMethodException Unity.AudioClip.Play
Any help would be greatly appreciated.
Thank You!
You can play an AudioClip but not an AudioSource, try var soundToPlay : AudioClip;

Resources