Page 1 of 1

on_cutscene_start and on_cutscene_end events

Posted: Fri Apr 21, 2023 1:51 am
by Wiwiweb
What?
I would like 2 new events: `on_cutscene_start` and `on_cutscene_end`, which trigger when anything starts/ends a cutscene.
Why?
When leaving or returning to Space Exploration's navigation satellite view, we need to save/restore the player's inventory.
When a cutscene happens while someone is in satellite view, we have no way to know that they have just left that view, and we cannot save their inventory.
In addition, `on_cutscene_end` would be convenient to restore that inventory. (At the moment this is possible in a hacky way by checking for the player's controller 1 tick after`on_cutscene_cancelled` and `on_cutscene_waypoint_reached`, but a proper way to detect the end of a cutscene would be best.)

Re: on_cutscene_start and on_cutscene_end events

Posted: Sat Apr 22, 2023 2:47 pm
by Pi-C
Wiwiweb wrote:
Fri Apr 21, 2023 1:51 am
… a proper way to detect the end of a cutscene would be best.
on_cutscene_cancelled will also trigger when a cutscene ends naturally, without player intervention. Add this to your mod:

Code: Select all

script.on_event(defines.events.on_cutscene_cancelled, function(event)
	log("Cutscene ended: "..serpent.block(event))
end)
Start a new freeplay game, wait until the cutscene has finished of its own. Grep your log file for "Cutscene ended:", and you'll find the event data.

Re: on_cutscene_start and on_cutscene_end events

Posted: Sun Apr 23, 2023 12:53 am
by Wiwiweb
That doesn't seem to be true, or maybe not in all cases? This is the result from clicking the "Activate" button on Krastorio 2's intergalactic transmitter (Using this mod: https://mods.factorio.com/mod/0-event-trace - muted the on_player_position_changed and on_entity_damaged events for clarity)
Screenshot 2023-04-22 174821.png
Screenshot 2023-04-22 174821.png (101.26 KiB) Viewed 1088 times
Save file included if you want to try and reproduce. Regardless, I'm mostly interested in on_cutscene_starts, since I already have the hacky way of detecting the end on a cutscene :)

Re: on_cutscene_start and on_cutscene_end events

Posted: Sun Apr 23, 2023 9:58 am
by Pi-C
Wiwiweb wrote:
Sun Apr 23, 2023 12:53 am
That doesn't seem to be true, or maybe not in all cases?
Sorry, it seems you're right. According to the API description, on_cutscene_cancelled is raised when a cutscene is cancelled by the player or by script. In miniMAXIme, I must delay initializing players until the cutscene is cancelled or ends because the player won't have a character before that. However, looking in data/base/scenarios/freeplay.lua, I've found that the event is raised once the first waypoint of the cutscene has been reached by player.exit_cutscene():

Code: Select all

local on_cutscene_waypoint_reached = function(event)
  if not global.crash_site_cutscene_active then return end
  if not crash_site.is_crash_site_cutscene(event) then return end

  local player = game.get_player(event.player_index)

  player.exit_cutscene()

  if not global.skip_intro then
    if game.is_multiplayer() then
      player.print(global.custom_intro_message or {"msg-intro"})
    else
      game.show_message_dialog{text = global.custom_intro_message or {"msg-intro"}}
    end
  end
end
Thus, my assumption that on_cutscene_cancelled will also be triggered when the cutscene ends naturally seemed to be right for the vanilla cutscene (it really isn't as there is another waypoint after player.exit_cutscreen() has been called), but isn't guaranteed to work with cutscenes added by other mods.

I agree now that it would be nice to have on_cutscene_end. :-)

Re: on_cutscene_start and on_cutscene_end events

Posted: Mon May 22, 2023 2:21 pm
by raiguard
This was added in 1.1.82, but I forgot to put it in the changelog.

Code: Select all

Scripting:
  - Added on_cutscene_started, on_cutscene_finished
on_cutscene_finished will only be raised if a cutscene ends naturally. If you need to detect whenever a cutscene ends at all, you need to hook both on_cutscene_finished and on_cutscene_cancelled.