Modify the "tutorial" player?

Place to get help with not working mods / modding interface.
Post Reply
sarge945
Burner Inserter
Burner Inserter
Posts: 16
Joined: Wed Apr 26, 2023 9:45 am
Contact:

Modify the "tutorial" player?

Post by sarge945 »

So, I have made a mod which reduces the players reach to 1 tile.

However, this also breaks all the tutorial demonstrations, for instance the "place power poles by dragging" tutorial, as the click instead says "not enough range" rather than actually showing the action.

I decided to fix this, and tried to do so by setting the players reach to a huge number when the tutorial GUI was open

Code: Select all

-- Because we are modifying the players reach, this will break the tutorial screens
-- Here, we can fix it

--Subscribe to gui events so we can fix the reach in the tutorials screen
script.on_event(defines.events.on_gui_opened, TutorialOpen)
script.on_event(defines.events.on_gui_closed, TutorialClose)

local function TutorialOpen(event)
	if event["gui_type"] == defines.gui_type.tutorials then
		for i, player in pairs(game.players) do
			FixTutorialReach(player)
		end
	end
end
local function TutorialClose(event)
	if event["gui_type"] == defines.gui_type.tutorials then
		for i, player in pairs(game.players) do
			ResetReach(player)
		end
	end
end

function FixTutorialReach(player)
	player.character_build_distance_bonus = 100
	player.character_item_drop_distance_bonus = 100
	player.character_item_pickup_distance_bonus = 100
	player.character_loot_pickup_distance_bonus = 100
	player.character_reach_distance_bonus = 100
	player.character_resource_reach_distance_bonus = 100
end
However, it still refuses to work and still has the same super short range in the tutorial pane.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1655
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Modify the "tutorial" player?

Post by Pi-C »

Code: Select all

-- Because we are modifying the players reach, this will break the tutorial screens
-- Here, we can fix it

--Subscribe to gui events so we can fix the reach in the tutorials screen
script.on_event(defines.events.on_gui_opened, TutorialOpen)
script.on_event(defines.events.on_gui_closed, TutorialClose)
Both TutorialOpen and TutorialClose are undefined, so the handler you assign to these events is nil. Put these lines at the end of your file and the functions will be called just fine. Also, function TutorialOpen calls function FixTutorialReach (which isn't defined yet), and function TutorialClose calls ResetReach (which is never defined in the code you've posted). Move TutorialOpen and TutorialClose after the definitions of FixTutorialReach and ResetReach!

The other thing is that running on_gui_opened/on_gui_closed won't affect the tutorials. I've made a small mod with just this in control.lua:

Code: Select all

script.on_event(defines.events.on_player_created, function(event)
  log(string.format("TEST MOD: Entered handler for on_player_created(%s)", serpent.line(event)))
end)
Then I added the logging-line to data/base/tutorials/stack-transfers/control.lua (yep, that's the "data" directory in the game's main folder):

Code: Select all

function on_player_created(event)
log(string.format("TUTORIAL: Entered handler for on_player_created(%s)", serpent.line(event)))
My log file after starting the new game:

Code: Select all

2480.667 Verbose BlueprintLibrary.cpp:67: Loaded external blueprint storage: playerIndex=0, nextRecordID=4, timestamp=1659526345, records={}
2481.516 Loading level.dat: 1261177 bytes.
2481.516 Info Scenario.cpp:199: Map version 1.1.81-0
2481.543 Verbose Scenario.cpp:255: Loading level.dat finished: 0.026579 seconds.
2481.544 Verbose BlueprintLibrary.cpp:67: Loaded external blueprint storage: playerIndex=0, nextRecordID=4, timestamp=1659526345, records={}
2481.544 Verbose Scenario.cpp:339: Entities setup finished: 0.000958 seconds.
2481.546 Checksum for script /home/pc/GOG_Games/Factorio/test_1.1/temp/currently-playing/control.lua: 2881393120
2481.547 Checksum for script __test_player_range__/control.lua: 2286630489
2481.552 Verbose Scenario.cpp:389: Map setup finished: 0.036339 seconds.
2481.553 Verbose AppManagerStates.cpp:2488: Time to create game: 0.933316 seconds.
2481.569 Script @__test_player_range__/control.lua:109: TEST MOD: Entered handler for on_player_created({name = 26, player_index = 1, tick = 0})
2482.333 Script @__test_player_range__/control.lua:116: TEST MOD: Entered handler for on_cutscene_cancelled({name = 169, player_index = 1, tick = 44})
Now I opened the GUI for the tutorials, selected "Stack transfers", and clicked on "Replay tutorial":

Code: Select all

2518.276 Supending game state
2518.276 Entering tutorial: [base]stack-transfers
2518.290 Loading level.dat: 1185316 bytes.
2518.290 Info Scenario.cpp:199: Map version 0.18.1-0
2518.293 Info PrototypeMigrationList.cpp:194: Activating migration base/1.1.0.json
2518.355 Verbose Scenario.cpp:255: Loading blueprint.dat finished: 0.065099 seconds.
2518.356 Verbose BlueprintLibrary.cpp:67: Loaded external blueprint storage: playerIndex=0, nextRecordID=4, timestamp=1659526345, records={}
2518.357 Verbose Map.cpp:1168: Time to re-chart all: 0.000047 seconds.
2518.357 Verbose Scenario.cpp:339: Entities setup finished: 0.001528 seconds.
2518.360 Checksum for script /home/pc/GOG_Games/Factorio/test_1.1/temp/currently-playing-tutorial/control.lua: 1346350558
2518.366 Info LuaGameScript.cpp:797: level state size: 270 bytes.
2518.368 Checksum for script /home/pc/GOG_Games/Factorio/test_1.1/temp/currently-playing-tutorial/control.lua: 1346350558
2518.369 Verbose Scenario.cpp:389: Map setup finished: 0.079301 seconds.
2518.372 Info LuaGameScript.cpp:797: level state size: 270 bytes.
2518.402 Script @/home/pc/GOG_Games/Factorio/test_1.1/temp/currently-playing-tutorial/control.lua:10: TUTORIAL: Entered handler for on_player_created({name = 26, player_index = 1, tick = 0})
As you can see, your original game is suspended, a new game is started inside the game, and a new player gets created. Naturally, the tutorial player will also have another character! Your mod will change just the reach of the character existing in the main game, but these changes aren't propagated to the new character.

Unfortunately, I didn't find a way to get any events from my testing mod to run when the tutorial was active, so I couldn't reapply the changes to the tutorial character. Not sure if I did something wrong, but usually all events are passed on to all mods, so my handler for on_player_created should have been triggered as well unless the tutorial somehow prevents other mods from running.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: Modify the "tutorial" player?

Post by PFQNiet »

You should be able to loop through tutorials in the prototype tree, and prefix their code with a line to set the character's reach there.

Post Reply

Return to “Modding help”