Create entity on chunk create but AFTER the event.

Place to get help with not working mods / modding interface.
Post Reply
yeganer
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Mon Jul 06, 2015 12:07 pm
Contact:

Create entity on chunk create but AFTER the event.

Post by yeganer »

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.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Create entity on chunk create but AFTER the event.

Post by Adil »

Use ontick event to handle the chunk on the next tick after its generation.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

yeganer
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Mon Jul 06, 2015 12:07 pm
Contact:

Re: Create entity on chunk create but AFTER the event.

Post by yeganer »

Thanks for the idea. I tried it, it still doesn't work.

My approach was to set a variable for each chunk once the on_chunk_generated event was executed and only spawn if this variable is set. i'm still missing chunks. And yes i checked if they are even calculated and they are. they just disappear.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Create entity on chunk create but AFTER the event.

Post by Adil »

I don't get it. Are you saying that on the next tick after the creation the chunk is still not ready to be manipulated?

Hm, it's not unkommon to see empty chunks when you move into unexplored area. The game generator surely works on chunks for more than a single tick. Maybe it is required to wait until its done before manipulating map. (Maybe that wait can be reduced by changing the generator settings to spawn as little stuff as possible.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

GopherAtl
Fast Inserter
Fast Inserter
Posts: 177
Joined: Sat Jan 31, 2015 7:54 pm
Contact:

Re: Create entity on chunk create but AFTER the event.

Post by GopherAtl »

If your noise is generated based on the map seed and tile coordinates, you can do multi-chunk resources without worrying about chunk boundaries.
My Mods:
Nixie Tubes - numeric displays for your circuit networks!
Logistic Combinators - use logistics values in circuit logic! -
Autowire - automate red/green wire connections

yeganer
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Mon Jul 06, 2015 12:07 pm
Contact:

Re: Create entity on chunk create but AFTER the event.

Post by yeganer »

I found the problem and it is a strange one.
At one point in my resource generation algorithm i'm checking surface.can_place_entity

It turns out, this returns false for chunks that are not generated yet.

orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Create entity on chunk create but AFTER the event.

Post by orzelek »

It would need a multiple step setup:
1. Generate patch and store it's location. Check if all chunks exist - if not try to spawn a dummy entity on them to trigger generation (there might be a better way?)

2. In on_tick check if all chunks required for the field exist - create it if they are there or wait some more if not.

yeganer
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Mon Jul 06, 2015 12:07 pm
Contact:

Re: Create entity on chunk create but AFTER the event.

Post by yeganer »

I tried to just ignore the can_place_entity step and it turned out quite well. I think i'll leave it that way for now.

Of course a proper solution would look different.

Post Reply

Return to “Modding help”