|
|
|
 |
Reply From: |
Gil-Koren |
You should use the Spring’s signal of collision.
The same signal that fires in the player when you detect if he should be launched upwards is fired in the Spring scene. Attach a script to your basic spring scene if it doesn’t have one yet, and connect the collision signal to it.
Then add the player node to a group (next to the signals in the inspector) called “Player”
so you can know if it’s the player colliding and add this bit of code in the new function created by the signal:
func _area_entered_spring(area):
if area.is_in_group("Player"):
$AnimatedSprite.play("SpringBounce")
If you have anymore questions feel free to ask!
I read the comments to your original question and it seems the concept you are getting stuck with is Signals.
If you want, there should be some great tutorials on youtube on this topic.
I have all this already.
But there isn’t any tutorials online that covers playing an animation from another script in the example I provided.
All the online tutorials only use one instanced scene, where as I have multiple springs.
The other tutorials only focus on collision, which I have done already as I said.
If you mean playing the spring animation from the player script when it bounces, you can add to the same funciton where you play the player animation for bouncing and change it’s velocity this bit of code after adding the spring scene to a group called Spring:
if area.is_in_group("Spring"):
area.$AnimatedSprite.play("SpringBounce")
The signal of collision in the player passes the body it interacts with as a paramter and you can call function of that scene by accessing the parammter (I belive the default for the collision is area, change the area in the code block above to whatever is the passed paramter. If you want this to scale better i suggest adding a function in the spring script and every other type of interactable called something along the lines of PlayerInteracted, so you can just add the scenes to a group called “Interactables” and call that function each time
Gil-Koren | 2022-07-29 15:52
Hi, thanks for your reply. Yes, I wish to play the spring animation from the Player script.
Just so we’re on the same page, here’s what I have in the Spring script:
extends Area2D
signal PlayerOnSpring
signal PlayerNotOnSpring
func _on_Spring_body_entered(body: Node) -> void:
if body.is_in_group ("Player"): #Check if the Player body entered (Player Group)
emit_signal("PlayerOnSpring") #Emit Signal
func _on_Spring_body_exited(body: Node) -> void:
if body.is_in_group ("Player"): #Check if the Player body entered (Player Group)
emit_signal("PlayerNotOnSpring") #Emit Signal
In part of the Player’s script I have which is the received Signals:
#CHECK IF PLAYER ON THE SPRING
func _on_Spring_PlayerOnSpring() -> void:
playerSpringJump = true
#CHECK IF PLAYER OFF THE SPRING
func _on_Spring_PlayerNotOnSpring() -> void:
playerSpringJump = false
And the part in the Player’s script where I’d like to trigger the animation:
#SET IF ON THE GROUND OR NOT
if is_on_floor():
if not onGround:
$FootDust.restart() # Reset the Particle effect so that it's not half way though
$FootDust.emitting = true
onGround = true
else:
onGround = false
if myVelocity.y < 0:
$AnimatedSprite.play("Jump")
if myVelocity.y > 0 :
$AnimatedSprite.play("Fall")
print(myVelocity.y)
#BOUNCE PLAYER ON SPRING/MUSHROOM
if playerSpringJump:
myVelocity.y = -springJumpPower
#$AnimatedSprite.play("SpringBounce") # Where the animation should play
myVelocity = move_and_slide(myVelocity, FLOOR) #MOVE PLAYER WALKING
The Player can only bounce on the Spring when Overlapping the Spring AND when he is falling. This allows him walk past the spring the rest of the time 
I’m not sure what parameter I require to swap with the ‘area’ you mentioned.
Ok, i misunderstood the scripts and scene structure. Try this:
In the spring script, pass self
as a paramter when emitting the signal, and save this param in the player object as a variable named interacting_object
.
Then, you can use interacting_object.PlayerInteracted()
as mentioned before. This way you can play the animation on the relevant object everytime without re-connecting every interactable, just by passing self
as a param.
Let me know if this is what you ment and if it worked!
Gil-Koren | 2022-07-29 17:37
Thank you for your time.
I’ve added self to the Signal emitter:
func _on_Spring_body_entered(body: Node) -> void:
if body.is_in_group ("Player"): #Check if the Player body entered (Player Group)
emit_signal("PlayerOnSpring", self) #Emit Signal with Self
#print("on Spring!")
#$AnimatedSprite.play("SpringBounce")
The variable you said to add to the Player’s script:
var interacting_object
Does this need a type?
I’m a little unsure on what you mean exactly when you said:
“Then, you can use interacting_object.PlayerInteracted()
as mentioned before.”
If you can, could you clarify a little more please 
The variable does not need a type.
What i meant is that you can add a function called PlayerInteracted()
to every interactable object so you can easily call it each time you want it do something. In this case you should add to the Spring script:
func PlayerInteracted():
$AnimatedSprite.play("SpringBounce")
And in the Player script, inside the if playerSpringJump
you should add:
interacting_object.PlayerInteracted()
Hopefully this was more clear. Update me how it went! 
Gil-Koren | 2022-07-29 19:13
That’s great, so basically calling a function from the Spring script with the Signal.
I have a small issue though, adding self to the Signal from the Spring script has stopped my Player’s received signal from working for the collison of the Spring. The collision works at the Spring end, just not received at the Player’s.
Do I need something to pass in the () brackets of the function?
#CHECK IF PLAYER ON THE SPRING
func _on_Spring_PlayerOnSpring() -> void:
playerSpringJump = true
#print("on spring")
I think the problem here is that you are passing an argument to a function that doesn’t accept any arguments. Also, it does not seem you change the value of interacting_object inside the function. Try this:
func _on_Spring_PlayerOnSpring(spring) -> void:
playerSpringJump = true
interacting_object = spring
Gil-Koren | 2022-07-30 08:09
Hi,
I was hoping to get back here sooner, but the servers were down.
The animation now plays now, however it only plays the once.
Any ideas?