1

I am working on a strategy game where you can conquer territory. Here is a screenshot of what it looks like (I own the purple land at the middle): enter image description here A map stores a plot object per coordinates (a coordinate is stored as a bytes32):

    mapping(bytes32 => Plot) public map;

When I draw the map in browser I need to check a big quantity of coordinates and this takes a lot of time. In fact, even if I try to do it in the contract and write a solidity function which iterates over the coordinates for me, this is still pretty slow. A solution I came up with is to create another map which stores informations about a 8*8 square space. A chunk is just a boolean. false = empty, true = at least one chunk. This theoretically allows me to be much faster on empty zones, but this is still pretty slow. What should I do?

2
  • What information are you storing in the map? The EVM is pretty bad at storing large amount of data. I'd store that type of data off-chain perhaps using something like zk snark for synchronization.
    – Ismael
    Jan 2, 2022 at 16:27
  • I am storing a Plot as value (a really simple struct) and a coordinates (serialized as a bytes32) as a key. This represents territories owned by players on a world map.
    – Th0rgal
    Jan 2, 2022 at 16:39

1 Answer 1

1

I guess you should emit an event once a plot is filled just like emit PlotClaimed(bytes32 coordinates) and then query those events on your script or even do a subgraph.

2
  • How using events would make my queries faster? I will look into TheGraph. Do you know if it is compatible with any evm chains (eg Cronos)?
    – Th0rgal
    Jan 2, 2022 at 16:13
  • Events are what index calls off chain so it's the only way for you to retrieve information without looping and I think The Graph doesn't support Cronos by now, but you could take a look at their website.
    – Omar
    Jan 2, 2022 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.