Adding scripts to tileset

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By carbajosa

Hi,

If I add a scripts in the sprites when creating tilesets, will they be read by the engine as is? I only know about adding collision boxes but I have not tried adding scripts in it… Like for example, I implemented a tile that crumbles when a player has stepped on it, or do I have to do that separately outside the tileset creation?

I hope that made sense.

Thanks!

:bust_in_silhouette: Reply From: GunPoint

I have tried to add a script to a water tile, changing it’s sprite.
It didn’t worked. I checked with a print if the script was actually read by the engine. And it didn’t.
So how should you fix your problem (based on my fix): [It may not work, I can’t test it but theoretically it should work; also you should know how to use the AnimationPlayer node]

  1. Add an Area2D node to the player. Make sure that the Area2D node has a CollisionShape node as a child.
  2. Add an AnimationPlayer node in your main scene (where you have the Tilemap node)
  3. Open the animation tab, make a new animation file. Now click on the Tilemap node, in the inspector click on the name of your tileset next to the Tileset property
  4. Find the tile you want to animate in that dropdown list
  5. Key the first texture. Change the texture. Key the current texture. Do that until you get the animation you want.
  6. In your player script: (in your process function)
for body in $Area2D.get_overlapping_bodies():
    if body.get_name() == "Your tile's body name"
        get_parent().get_node("AnimationPlayer").play("animationName")

Again, I have not tested this but that’s basically how I fixed my animated water tile.

Hi, thanks for answering the question and for the guide, this is really helpful! It worked! I didn’t know I can detect which tile I am stepping on.

carbajosa | 2019-01-21 08:24