Create entity on chunk create but AFTER the event.
Posted: Wed Aug 12, 2015 6:23 am
So while rewriting RSO i stumpled accross another problem that i wasn't able to solve completely yet.
The idea is to spawn huge resource patches that are calculated on_chunk_generated. But these patches are bigger than the chunk that creates them so they will extend into other chunks.
My first idea was to store the information for the creation in a table and then in the on_chunk_generated function check if there is a spawn list for the current chunk and if there is, spawn it.
With that approach i had the problem that sometimes some chunks of the patches weren't generated and the table remained in memory. So i assumed these chunks were already generated so i implemented a check to is_chunk_generated and if that is true, just populate the chunk again. Which didn't help. With that approach the table got deleted ( i delete after the spawn is successfull ) but the entities didn't appear on the map.
Yesterday i learned that calling surface.create_entity on a position that isn't generated yet will generate it, so i tried it and i got a new problem. In my on_chunk_generated function i'll have to clear the corresponding chunk before i spawn anything there. It turns out, create_entity will create that entity but my on_chunk_generated will get called after that and remove my spawns, too.
So any idea how i could solve that problem?
Here is the code for reference: https://github.com/yeganer/RSO/blob/mas ... e.lua#L204
The function that is called on_chunk_create is process_chunk() which calls clear_chunk and populate_chunk. The function that calculates where to spawn is calculate_spawns.
The idea is to spawn huge resource patches that are calculated on_chunk_generated. But these patches are bigger than the chunk that creates them so they will extend into other chunks.
My first idea was to store the information for the creation in a table and then in the on_chunk_generated function check if there is a spawn list for the current chunk and if there is, spawn it.
With that approach i had the problem that sometimes some chunks of the patches weren't generated and the table remained in memory. So i assumed these chunks were already generated so i implemented a check to is_chunk_generated and if that is true, just populate the chunk again. Which didn't help. With that approach the table got deleted ( i delete after the spawn is successfull ) but the entities didn't appear on the map.
Yesterday i learned that calling surface.create_entity on a position that isn't generated yet will generate it, so i tried it and i got a new problem. In my on_chunk_generated function i'll have to clear the corresponding chunk before i spawn anything there. It turns out, create_entity will create that entity but my on_chunk_generated will get called after that and remove my spawns, too.
So any idea how i could solve that problem?
Here is the code for reference: https://github.com/yeganer/RSO/blob/mas ... e.lua#L204
The function that is called on_chunk_create is process_chunk() which calls clear_chunk and populate_chunk. The function that calculates where to spawn is calculate_spawns.