Author Topic: HUD just disappeared by calling "on_started()"  (Read 315 times)

HarleyDavidson

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • IT Werkstatt
HUD just disappeared by calling "on_started()"
« on: January 20, 2017, 02:22:05 pm »
I just copied all required scripts into my projects and the hud is displayed correctly.
But my hero is changed to the default tunic1 hero sprite, so I just wanted to change the sprite id.

Only the call of the function game:on_started() let the HUD disappear and I cannot figure out why.

Here's my game_manager.lua:

Quote
local game_manager = {}

local initial_game = require("scripts/initial_game")

-- Starts the game from the given savegame file,
-- initializing it if necessary.
function game_manager:start_game(file_name)

  local exists = sol.game.exists(file_name)
  local game = sol.game.load(file_name)
  if not exists then
    -- Initialize a new savegame.
    initial_game:initialize_new_savegame(game)
  end

  function game:on_started()
  end

  game:start()
end

return game_manager

Only the call of the function "game:on_started()" is enough to let the hud disappear,
I do not change or call anything in this function...
There's no error message in the console.  :(

Can anyone help me?

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1030
    • View Profile
Re: HUD just disappeared by calling "on_started()"
« Reply #1 on: January 20, 2017, 02:28:01 pm »
By defining the function game:on_started() here, you overwrite the one used by the HUD.
The correct way should be to define it as
Code: Lua
  1. game:register_event("on_started", function()
  2.    ...
  3. end)
  4.  
instead of
Code: Lua
  1. function game:on_started()
  2.    ...
  3. end
  4.  
Using register_event() is equivalent but allows to have several functions associated to the same event (on_started() here).

If a tutorial told you to write function game:on_started(), please tell me and I will fix it.

HarleyDavidson

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • IT Werkstatt
Re: HUD just disappeared by calling "on_started()"
« Reply #2 on: January 20, 2017, 02:35:12 pm »
Thanks for this super fast reply!
Now it works, here's my script now:

Code: [Select]
local game_manager = {}

local initial_game = require("scripts/initial_game")
require("scripts/multi_events")

-- Starts the game from the given savegame file,
-- initializing it if necessary.
function game_manager:start_game(file_name)

  local exists = sol.game.exists(file_name)
  local game = sol.game.load(file_name)
  if not exists then
    -- Initialize a new savegame.
    initial_game:initialize_new_savegame(game)
  end

  game:register_event("on_started", function()
    local hero = game:get_hero()
    hero:set_tunic_sprite_id("main_heroes/dominik")
  end)

  game:start()
end

return game_manager