Mod not applied when other mods are added/removed?

Place to get help with not working mods / modding interface.
Post Reply
Fexx
Burner Inserter
Burner Inserter
Posts: 11
Joined: Sat Jan 13, 2018 4:07 pm
Contact:

Mod not applied when other mods are added/removed?

Post by Fexx »

Hi,

As similar mods did not seem to be updated at that point, so when 0.16 was released I created the mod Far Reach which is a pretty simple mod that extends the reach of the character.
The distance is configurable.

For me, everyhing works fine, but I got several reports that the mod do not get applied when people add or remove mods from their games.

Basically I just have 3 settings: far-reach-build-distance-bonus, far-reach-reach-distance-bonus and far-reach-resource-reach-distance-bonus
They are applied by a function with the following body:

Code: Select all

function far_reach_apply_settings()
    local settings = settings.global
    local playerForce = game.forces["player"]
        
    playerForce.character_build_distance_bonus = settings["far-reach-build-distance-bonus"].value
    playerForce.character_reach_distance_bonus = settings["far-reach-reach-distance-bonus"].value
    playerForce.character_resource_reach_distance_bonus = settings["far-reach-resource-reach-distance-bonus"].value
end
This function is called by on_init, and to make everything reconfigurable I also added calling the function to an event listener for defines.events.on_runtime_mod_setting_changed.

When the bug was reported, I thought I could fix that by also calling the function in script.on_configuration_changed, but this did not have the desired effect. As I'm running out of solutions, I hope someone here can help me out what I'm missing?


As people report, when they go to the mod settings, everything works again, so on_runtime_mod_setting_changed seems to work fine, but after the game is loaded, the settings are not applied.

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

Re: Mod not applied when other mods are added/removed?

Post by orzelek »

There is a chance that more mods modify those so what you applied in on_init simply gets overwritten.
When settings are changed you are reapplying it so it works then.
From what I recall events are processed in mod loading order (affected by dependencies) so any mod that loads after yours could be the culprit.

I do think that on_init is called on any added mod but I'm not sure.
You would need to ask someone for mods they have and debug the events to see if all is working as intended inside on_configuration_changed.

chrisgbk
Long Handed Inserter
Long Handed Inserter
Posts: 92
Joined: Mon Jan 02, 2017 4:31 am
Contact:

Re: Mod not applied when other mods are added/removed?

Post by chrisgbk »

Factorio Data Lifecycle wrote: Factorio sorts mods first by dependencies then by natural sort order accounting for case (https://en.wikipedia.org/wiki/Natural_sort_order).

...

Using the mod order each mod is setup:
  • When creating a new game, script.on_init() will be called on each mod that has a control.lua file.
  • When loading a save game and the mod existed in that save game script.on_load() is called.
  • When loading a save game and the mod did not exist in that save game script.on_init() is called.
on_init is only called the first time the mod is added into a new game or save. If it's already been added, on_load is called instead.

Just don't forget this important note:
During the script.on_load() event handler access to the game table is not available. This handler is meant for only 3 things:
  1. Re-setup meta-tables. Meta-tables are not persisted through save-load.
  2. Re-setup conditional event handlers (subscribing to an event only when some condition is true to save processing time).
  3. Create local references to data stored in the global table
Attempting to change the contents of the global table during the script.on_load() event handler is not allowed. Doing so can lead to desyncs if the mod is used in multiplayer and will generate an error if the game detects it has been changed in any way.

Fexx
Burner Inserter
Burner Inserter
Posts: 11
Joined: Sat Jan 13, 2018 4:07 pm
Contact:

Re: Mod not applied when other mods are added/removed?

Post by Fexx »

Thanks for your replies.

Yes, on_load is irrelevant for me, as I am modifying game.forces

So from your perspective, the only possibility is other mods which override this settings? Because people report that it happens with "any mod" that is added or removed, not just one particular.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Mod not applied when other mods are added/removed?

Post by eradicator »

Another possible point of failure:
You've hardcoded the force name to "player", so anybody on a different force won't get the bonus. So ask the people who report those issues if they were playing multiplayer/pvp. Also the bonus gui does show the bonus, so tell those people to check that... because maybe they just think it's not there or something while it actually is?

A suitable solution for the MP situation would be to loop through all players and fetch their force. This avoids applying the bonus to non-player forces. I also added an event hook to player_changed_force which should take care of new forces being created too.

Code: Select all

function far_reach_apply_settings()
	local settings = settings.global
  for _,player in pairs(game.players) do
    player.force.character_build_distance_bonus = settings["far-reach-build-distance-bonus"].value
    player.force.character_reach_distance_bonus = settings["far-reach-reach-distance-bonus"].value
    player.force.character_resource_reach_distance_bonus = settings["far-reach-resource-reach-distance-bonus"].value
    end
end

script.on_init(far_reach_apply_settings)
script.on_configuration_changed(far_reach_apply_settings)
script.on_event(
  {defines.events.on_runtime_mod_setting_changed,
   defines.events.on_player_changed_force}
  ,far_reach_apply_settings
  )
Disclaimer: Untested. May contain typos.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”