Hello girls and guys,
I'm facing an issue here, and due to the fact, that I tried some things on my own and searched for similar topics - I can't quite figure it out myself...
I want to do a cutscene. For me the best option was, to make a script (a menu), in which I have all the funcs on what should happen. A menu, because I want the cutscene to start, when the hero is in a dialog with a npc and gives the right answer on a question.
So long story short, I have this in my MAP-SCRIPT:
local cutscene = require("scripts/cutscene/cutscene_1")
function mentor:on_interaction() --mentor is the npc--
game:start_dialog("mentor", function(answer)
if answer == 1 then
sol.menu.start(game, cutscene)
hero:freeze()
end
end)
end
And then there is the required SCRIPT for the CUTSCENE, build as a menu (not finished):
local cutscene = {}
function cutscene:on_started()
--fade_in effect and timers start--
end
function cutscene:on_finished()
--all timers stop and hero unfreeze--
end
function cutscene:on_draw(dst_surface)
--menu_background and other images are drawn--
end
return cutscene
And her comes the issue: The menu starts like it should, but when it is finished, the hero is still frozen, because if I do hero:unfreeze() in cutscene:on_finished() it doesnt know the value "hero". And I also cant do game:get_hero() at top, because it also doesnt know the value "game".
So is there a way to get the GAME in the script to work with, or am I totally wrong with what I have done with this so far?
Thanks in advance ;)
You can add an initialization function to your cutscene table, and store the game from there:
local cutscene = {}
function cutscene:initialize(game)
cutscene.game = game
end
function cutscene:on_started()
...
and call this function just before starting your menu.
Tried it, I'm still doing something wrong...
When I have:
local cutscene = {}
function cutscene:intialize(game)
cutscene.game = game
end
function cutscene:on_started()
--sprites, sol.audio.stop_music(), timers--
end
function cutscene:on_finished()
--timers:stop, hero:unfreeze()
game:set_paused_allowed(true)
end
function cutscene:on_draw(battle_surface)
--sprites and images--
end
function cutscene:on_command_pressed(command)
if command == "action" then
sol.menu.stop(self)
end
end
return cutscene
...the engine says:"Error: In on_finished: scripts/cutscene.lua:13: attempt to index global 'game' (a nil value)"
I have to add, I made an "on_command_pressed()" event to test the scene, because it doesnt end itself for now.
So I still cant use game-events, what am I doing wrong? ???
You stored the game on the cutscene table (cutscene.game), but you are trying to access it just with "game".
I'm very sorry, I didn't manage to do it that way for now...
Instead I did this and it worked:
local cutscene = {}
local function initialize_cutscene(game)
--functions--
end
local game_meta = sol.main.get_metatable("game")
game_meta:register_event("on_started", initialize_cutscene)
return cutscene
And also required it in the features.lua
But thanks for help, maybe I'm going over this again later ;)