Charting chunks seems to be disruptive

Place to get help with not working mods / modding interface.
Pi-C
Smart Inserter
Smart Inserter
Posts: 1759
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Charting chunks seems to be disruptive

Post by Pi-C »

The following is in my event handler for defines.events.on_player_changed_force:

Code: Select all

    for r, radar_id in pairs(players[index] or {}) do
      radar = radars[radar_id]
      if radar then
	-- snip
	
        if player.force.is_chunk_charted(radar.surface, radar.chunk_position) then
          radar.chart_tag = player.force.add_chart_tag(radar.surface, new_chart_data())

        -- Chart tags can only be placed on generated chunks. If the new force
        -- hasn't charted this chunk yet, schedule charting (this will raise
        -- on_chunk_charted) and store the data for the new force's chart tag!
        else
	  local x, y = radar.chart_tag.position.x, radar.chart_tag.position.y
	  local area = {{x - 16, y - 16}, {x + 16, y + 16}}

          radar.ordered_chart = new_chart_data()
MAPS.show("radar.ordered_chart", radar.ordered_chart)

          player.force.chart(radar.surface, area)
          MAPS.show("Ordered charting of this area on \""..radar.surface.name.."\"", area)
        end

	-- Anything here won't be run if the area is charted.

      end
    end
When it is run, the last thing that will be logged is the output of "radar.ordered_chart" while "Ordered charting of this area …" is never reached. The next line in the log file is from the handler for defines.events.on_chunk_charted, which is run one tick later in response to player.force.chart(radar.surface, area). So far, my radars table contained only one entry (setting up a testing environment is a bit complicated), but I assume the result would be the same if there were more.

For some reason, player.force.chart() never seems to return in the tick it was called. Is that intended? I realize that charting is an expensive operation which may take far longer than just one tick (that's why we can listen to on_chunk_charted). But I supposed that function would just put the area on top of the list of chunks that must be charted, then it would return to finish with the remaining code, and the actual charting would start at the end of the tick.

By the way, when exactly is a chunk regarded as charted? I'm working on a mod that's placing hidden radars (each scanning just one chunk, i.e. max_distance_of_sector_revealed and max_distance_of_nearby_sector_revealed are set to 0) that are associated with specific players. When a player changes to another force, radar.force will be set to the player's new force, so radar coverage of the player's chunks is transferred from the old to the new force. Perhaps I'd better use on_sector_scanned instead of on_chunk_charted?
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Pi-C
Smart Inserter
Smart Inserter
Posts: 1759
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Charting chunks seems to be disruptive

Post by Pi-C »

Pi-C wrote: Tue Dec 07, 2021 10:47 am For some reason, player.force.chart() never seems to return in the tick it was called. Is that intended? I realize that charting is an expensive operation which may take far longer than just one tick (that's why we can listen to on_chunk_charted). But I supposed that function would just put the area on top of the list of chunks that must be charted, then it would return to finish with the remaining code, and the actual charting would start at the end of the tick.
Sorry, false alarm. The charting seems to work as expected, after all. However, there was an error (indexing nil) in the command after the charting, so my function just quit there. But I didn't notice that because the game ignored the error. Heck, even adding

Code: Select all

error("Break!")
right before the erroneous command didn't crash the game! I only noticed the error message rushing past on the console, but there was nothing about in in the log file.

It seems that I've activated a kind of protective mode somehow, but I don't know what could have done that. Right now, I've turned off all mods except the one I'm working on, and my mod doesn't use pcall. Any ideas?
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5409
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Charting chunks seems to be disruptive

Post by Klonan »

Pi-C wrote: Tue Dec 07, 2021 2:41 pm It seems that I've activated a kind of protective mode somehow, but I don't know what could have done that. Right now, I've turned off all mods except the one I'm working on, and my mod doesn't use pcall. Any ideas?
You're using some event handling lib?
Pi-C
Smart Inserter
Smart Inserter
Posts: 1759
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Charting chunks seems to be disruptive

Post by Pi-C »

Klonan wrote: Tue Dec 07, 2021 7:53 pm You're using some event handling lib?
No, I don't. I just started a clean new game with just my mod, and the same thing happened again. If you want to see for yourself, load the saved game, sync mods (this will deactivate all mods but Maps), and change the player force:

Code: Select all

/c game.players[1].force = "neutral"
An error message should appear in the chat window, but not in the log file.
Attachments
test.zip
Saved game
(1.71 MiB) Downloaded 129 times
Maps_1.1.3.zip
Mod (WIP)
(92.62 KiB) Downloaded 125 times
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Post Reply

Return to “Modding help”