#Connect buttons to platforms

1 messages · Page 1 of 1 (latest)

hot cobalt
#

Hey folks!

I've created a Platform Scene & a Button Scene and I connected different buttons to different platforms so that, once the button is pressed, the animation on the platform starts or pauses accordingly.

The thing is I can't help but think there is a better way than the one I did.

Right now, each instance of the button sends a signal and then in GDScript I decide what animation plays:

class_name grassPlatform
extends AnimatableBody2D

func _on_button_lamp_toggled(lamp_on: bool) -> void:
    if (lamp_on == true):
        $AnimationPlayer.play("left_and_right")
    else:
        $AnimationPlayer.pause()


func _on_button_2_lamp_toggled(lamp_on: bool) -> void:
    if (lamp_on == true):
        $AnimationPlayer.play("up_and_down")
    else:
        $AnimationPlayer.pause()

Isn't there a more modular and scalable way to achieve this?

Thanks in advance!

robust panther
hot cobalt
#

And thanks for taking the time, btw!

robust panther
#

This is a finish line

#

It has an @export var that takes as an input a scene (the next level in our case)

#

in ur case u can make only 1 button that takes as input the animation it should play

#

Also, as a small tip

#

is the AnimationPlayer, on the player?

#

if yeah

#

make a new group

#

add the player into that group

#

on ur script add

@onready var animator: AnimationPlayer = get_tree().get_first_node_in_group("GROUP").animator
#

go in ur player node

#

make the animated body an unique name, it has the "%" symbol, so u can spot it easier

#

then add in ur player.gd (or the player script)

@onready var animator: AnimationPlayer = %AnimationPlayer
#

why should u do that?

#

"$AnimationPlayer" is the short-hand for "get_node("AnimationPlayer")"

#

what this does is going trough the root node and brute force searching for the target node

#

This function doesnt caches the output, so every time u acces this node, the calculations are made again and again

#

what unique names do on the other hand is storing the name in a precompiled cache (aka this makes the game heavier on the disk) which reduces the calculations and memory used