Simple doubt about data management and looping

Godot Version

Godot 4.2.1


I’m working on a strategy game where tiles can apply effects to other tiles, each tile information is stored on its own index, at the moment I’m using a dictionary to store the effects on a tile that looks like this:

cellv0 : [effect1, effect2],
cellv1 : [effect3, effect1, effect0],  

This way I can add and remove all the effects of a tile affecting another tile. The problem comes when I want to stack the modifiers of an effect or not apply them at all if there is another effect present on that tile. I could just loop and keep count of everything and maybe that would be fine, I’m inexperienced but that sounds slow to me because there could be a lot of effects to go through. So I’m thinking of making a second dictionary with redundant information but using the effects as keys to find effects faster, but having to duplicate this data for each tile seems like a lot of memory:

effect1: [cellv0, cellv2],
effect2: [cellv0],
effect3: [cellv2],
effect0: [cellv2],

For context here is an example of what I would like to check:
Effect1 can stack but only up to 2
Effect1 does “x” if it has 1 stack but “y” if it has 2 stacks.

How would you solve this problem?


I see a lot of intermediate/newish programmers have this issue because your assessment is kinda technically correct

Looping/iterating over a bunch of stuff takes time and indexing the data in a second dictionary will take up more memory but until you’re running at scale it can be difficult to tell what will impact performance and by how much.

Spend enough time in coding communities and you’ll see Premature Optimisation Is the Root of All Evil a bunch

Until you have thousands and thousands of tiles your ideas would certainly be functional. Personally I would just iterate over the effects and then look at indexing them if speed became an issue.

1 Like

The maps will be pretty small 200x200 or less so I thought more memory for less cpu time would be beneficial in my case because these effects will be checked pretty often on a lot of tiles. I expect the logic of the effects to take a lot of the cpu time with how many there are and how complex they are getting.

I know I won’t know for sure what’s the best option until I finish and test the results but I was asking in case I was totally wrong on my assessment, or if I could make it more efficient with an inner class or something although for what I know they are pretty heavy and they are talking about adding structs to gdscript.

Thanks for the reply, I’ll keep working on it.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.