How to change tiles once per move

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

Okay… I didn’t know how else to phrase that question but hopefully, my explanation will clear things up. So I have an entity in my world that gathers those green blocks called energy and transfers them to other blocks. Each colored block has an energy value higher than the other. The image below shows all 6 green blocks. Starting from the top, its energy values are as follows: 6, 5, 4, 3, 2, 1. The entity has text showing the state that it is in and the amount of energy it has. “t” stands for taking and “g” stands for giving. The system that I have in place right now doesn’t actually place the right tile corresponding to the energy level but instead, it just removes the block and replaces it with dirt since the entity took the energy. I need help with implementing a system where the entity takes from the block and changes the tile to the appropriate one as well as when it gives. If any clairifications are needed please ask!

enter image description here

:bust_in_silhouette: Reply From: Inces

You mean entity is supposed to take 1 energy at a time and change the mined tile to the one corresponding with less energy ?

Hey Inces, essentially yes, but when I implemented this I ran into a problem where the code that handled the tile changing instantly changed tiles all the way down to the dirt tile. I tried adding a system where the entity remembers which tile it interacted with but failed. Thank you for helping me again! <3

zephy | 2022-04-22 14:03

Ah ok, so You must have designed an iteration, that worked too fast, as it was based on time isntead of on action, like on move ?

I might have mentioned setget funbctionality in your previous questions. It will allow You to mark a movement action when new tile is approached. Next, You will be able to use this mark to trigger actions and checks.

var tile : Vector2 setget newtile

func process():
       self.tile = Tilemap.world_to_map(global_position)

func newtile(value)
     if value != tile :
           tile = value
           checkcollisions()

This last function will trigger once per new move action on grid. Checkcollisions is just an example, You can also insert some signal there. Basically You want to check for collisions in this place of code and if there are mineral spots - take 1 energy and set tile to corresponding one. It will happen once.

Inces | 2022-04-22 14:19

Thank you so much Inces!!! It worked… again! I wrote a function called check_tile_type(arr : Array) which takes a get_neighbors() array and checks which ones are of the type NATURAL_BLOCK (the green blocks) and changes them only once. Can I ask how you decided to use this approach? I just want to know your line of reasoning. Thanks again!! <3

zephy | 2022-04-22 19:00

This is a common problem in programming to trigger actions by other actions. Setget is known sollution for all these situations, that are connected with change of variable. You will find yourself to use this approach all the time :). Advanced programmers are able to almost not use process() function at all, only signals, setgets and custom triggers.

Inces | 2022-04-22 19:41