Author Topic: Cutscene troubles (as menu)  (Read 262 times)

Eyth

  • Newbie
  • *
  • Posts: 26
    • View Profile
Cutscene troubles (as menu)
« on: October 10, 2018, 01:21:55 pm »
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:
Code: Lua
  1. local cutscene = require("scripts/cutscene/cutscene_1")
  2.  
  3. function mentor:on_interaction() --mentor is the npc--
  4.          game:start_dialog("mentor", function(answer)
  5.                                               if answer == 1 then
  6.                                                  sol.menu.start(game, cutscene)
  7.                                                  hero:freeze()
  8.                                               end
  9.                                      end)
  10. end
  11.  

And then there is the required SCRIPT for the CUTSCENE, build as a menu (not finished):
Code: Lua
  1. local cutscene = {}
  2.  
  3. function cutscene:on_started()
  4.          --fade_in effect and timers start--
  5. end
  6.  
  7. function cutscene:on_finished()
  8.          --all timers stop and hero unfreeze--
  9. end
  10.  
  11. function cutscene:on_draw(dst_surface)
  12.          --menu_background and other images are drawn--
  13. end
  14.  
  15. return cutscene
  16.  

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 ;)

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1146
    • View Profile
Re: Cutscene troubles (as menu)
« Reply #1 on: October 10, 2018, 01:33:10 pm »
You can add an initialization function to your cutscene table, and store the game from there:
Code: Lua
  1. local cutscene = {}
  2.  
  3. function cutscene:initialize(game)
  4.   cutscene.game = game
  5. end
  6.  
  7. function cutscene:on_started()
  8.  
  9. ...
  10.  
and call this function just before starting your menu.

Eyth

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Cutscene troubles (as menu)
« Reply #2 on: October 12, 2018, 11:16:01 am »
Tried it, I'm still doing something wrong...

When I have:
Code: Lua
  1. local cutscene = {}
  2.  
  3. function cutscene:intialize(game)
  4. cutscene.game = game
  5. end
  6.  
  7. function cutscene:on_started()
  8. --sprites, sol.audio.stop_music(), timers--
  9. end
  10.  
  11. function cutscene:on_finished()
  12. --timers:stop, hero:unfreeze()
  13. game:set_paused_allowed(true)
  14. end
  15.  
  16. function cutscene:on_draw(battle_surface)
  17. --sprites and images--
  18. end
  19.  
  20. function cutscene:on_command_pressed(command)
  21.   if command == "action" then
  22.     sol.menu.stop(self)
  23.   end
  24. end
  25.  
  26. return cutscene
  27.  

...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?  ???

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1146
    • View Profile
Re: Cutscene troubles (as menu)
« Reply #3 on: October 12, 2018, 12:40:14 pm »
You stored the game on the cutscene table (cutscene.game), but you are trying to access it just with "game".

Eyth

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Cutscene troubles (as menu)
« Reply #4 on: October 15, 2018, 12:30:49 pm »
I'm very sorry, I didn't manage to do it that way for now...

Instead I did this and it worked:
Code: Lua
  1. local cutscene = {}
  2.  
  3. local function initialize_cutscene(game)
  4.  
  5.     --functions--
  6.  
  7. end
  8.  
  9. local game_meta = sol.main.get_metatable("game")
  10. game_meta:register_event("on_started", initialize_cutscene)
  11.  
  12. return cutscene
  13.  

And also required it in the features.lua

But thanks for help, maybe I'm going over this again later ;)