Page 1 of 1

Changing Build Distance from on_init

Posted: Thu Apr 27, 2017 1:11 am
by utoxin
I'm trying to help with some updates to the Long Reach mod, to make it use the new mod settings interface in the options screen. I've got the settings displaying, but I can't figure out how to affect the actual build distance or reach distance once outside the data-final-fixes stage.

Is there a way to affect these values during game create/load?

For clarity, the values being changed in data-final-fixes.lua are:

Code: Select all

data.raw.player.player.build_distance = 125
data.raw.player.player.reach_distance = 125
And my settings are set up as such:

Code: Select all

data:extend({
   {
      type = "int-setting",
      name = "long-reach-build-distance",
      setting_type = "runtime-global",
      default_value = 125,
      maximum_value = 10000,
      minimum_value = 1,
      per_user = false,
   },
   {
      type = "int-setting",
      name = "long-reach-reach-distance",
      setting_type = "runtime-global",
      default_value = 125,
      maximum_value = 10000,
      minimum_value = 1,
      per_user = false,
   },
})

Re: Changing Build Distance from on_init

Posted: Thu Apr 27, 2017 2:09 am
by utoxin
Okay, I kept plugging away at it, and checked the code from some other mods, and found a working solution. The only thing I'd like to improve is the 'magic number' in my final method here. Is there a way to access the actual build/reach distance values on the player object from both on_init and on_event? I bet I could do it in on_event, but in on_init, the only places I can think to look for it aren't set yet.

Code: Select all

-- Initialize the settings on world load
script.on_init(
	function ()
		sync_long_reach_settings()
	end
)

-- When mod settings are changed, go ahead and reload ours, just in case
-- Could optimize by checking if it's our setting, but it's probably not worth it
script.on_event({defines.events.on_runtime_mod_setting_changed},
	function (player_index, setting) 
		sync_long_reach_settings()
	end
)

function sync_long_reach_settings()
		local settings = settings.global
		
		-- Default build and reach distances are 6, so subtract that from the requested distance
		game.forces["player"].character_build_distance_bonus = settings["long-reach-build-distance"]["value"] - 6
		game.forces["player"].character_reach_distance_bonus = settings["long-reach-reach-distance"]["value"] - 6
end