Problems with dialog and HUD

Started by Starlock, September 05, 2015, 02:44:21 AM

Previous topic - Next topic
September 05, 2015, 02:44:21 AM Last Edit: September 05, 2015, 02:46:18 AM by Starlock
In game manager I keep getting an error towards the hud: "Error: In on_map_changed: [string "scripts/game_manager.lua"]:75: attempt to index upvalue 'hud' (a nil value)"

and the dialog box creates the error:  Error: In on_started: "[string "scripts/dialog_box.lua"]:59: attempt to call field 'get_dialog_font' (a nil value)"

Clearly the problem is about the nil values but when I try defining either of them the error remains so I'm not quite understanding what to do....

Game_manager.lua
Code ( lua) Select
-- Script that creates a game ready to be played.

-- Usage:
-- local game_manager = require("scripts/game_manager")
-- local game = game_manager:create("savegame_file_name")
-- game:start()

local game_manager = {}

-- Creates a game ready to be played.
function game_manager:create(file)

  -- Create the game (but do not start it).
  local exists = sol.game.exists(file)
  local game = sol.game.load(file)
  if not exists then
    -- This is a new savegame file.
    game:set_starting_location("house")
    game:set_max_money(99)
    game:set_max_life(12)
    game:set_life(game:get_max_life())
    game:get_item("rupee_bag"):set_variant(1)
  end
 
  sol.main.load_file("scripts/dialog_box.lua")(game)
  sol.main.load_file("scripts/game_over.lua")(game)
  local hud_manager = require("scripts/hud/hud")
  local hud
  local pause_manager = require("scripts/menus/pause")
  local pause_menu

  -- Function called when the player runs this game.
  function game:on_started()

    -- Prepare the dialog box menu and the HUD.
    game:initialize_dialog_box()
    hud = hud_manager:create(game)
    pause_menu = pause_manager:create(game)
  end

  -- Function called when the game stops.
  function game:on_finished()

    -- Clean the dialog box and the HUD.
    game:quit_dialog_box()
    hud:quit()
    hud = nil
    pause_menu = nil
  end

  -- Function called when the game is paused.
  function game:on_paused()

    -- Tell the HUD we are paused.
    hud:on_paused()

    -- Start the pause menu.
    sol.menu.start(game, pause_menu)
  end

  -- Function called when the game is paused.
  function game:on_unpaused()

    -- Tell the HUD we are no longer paused.
    hud:on_unpaused()

    -- Stop the pause menu.
    sol.menu.stop(pause_menu)
  end

  -- Function called when the player goes to another map.
  game.on_map_changed = function(game, map)

    -- Notify the HUD (some HUD elements need to know that).
    hud:on_map_changed(map)
  end

  local custom_command_effects = {}
  -- Returns the current customized effect of the action or attack command.
  -- nil means that the built-in effect.
  function game:get_custom_command_effect(command)
    return custom_command_effects[command]
  end

  -- Overrides the effect of the action or attack command.
  -- Set the effect to nil to restore the built-in effect.
  function game:set_custom_command_effect(command, effect)
    custom_command_effects[command] = effect
  end

  -- Returns whether the HUD is currently shown.
  function game:is_hud_enabled()
    return hud:is_enabled()
  end

  -- Enables or disables the HUD.
  function game:set_hud_enabled(enable)
    return hud:set_enabled(enable)
  end

  local rupee_icon = sol.surface.create("hud/rupee_icon.png")
  local rupee_text = sol.text_surface.create()
  rupee_text:set_font("8_bit")

  function game:on_draw(dst_surface)

    rupee_icon:draw_region(0, 0, 12, 12, dst_surface, 10, 220)
    rupee_text:set_text(game:get_money())
    rupee_text:draw(dst_surface, 25, 225)
  end
 

  return game
end

return game_manager



And this is line 59 of the dialogbox script
Code ( lua) Select
local font, font_size = sol.language.get_dialog_font()

The second error is due to that the function get_dialog_font is not defined by the engine (you can check it does not appear in the Lua API). In some scripts I have seen, it appears defined in the main script (the game manager would be another option to define that). Try to put in the main.lua the following code (but change the font names to the ones you are using):

-- Returns the font and font size to be used for dialogs
-- depending on the specified language (the current one by default).
function sol.language.get_dialog_font(language)
  -- For now the same font is used by all languages.
  return "fixed8", 11
end

-- Returns the font and font size to be used to display text in menus
-- depending on the specified language (the current one by default).
function sol.language.get_menu_font(language)
  -- For now the same font is used by all languages.
  return "Viner Hand Itc", 8
end
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

The first error is strange. As you already know, it means that the hud variable is nil.
Maybe something is not working in the line "hud = hud_manager:create(game)"
(this could be because your hud_manager:create(game) is not returning a hud for some reason, or something like that). So, maybe the problem is in the hud script, could you post it?
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

Code ( lua) Select
-- Script that creates a head-up display for a game.

-- Usage:
-- local hud_manager = require("scripts/hud/hud")
-- local hud = hud_manager:create(game)

local hud_manager = {}

-- Creates and runs a HUD for the specified game.
function hud_manager:create(game)

  -- Set up the HUD.
  local hud = {
    enabled = false,
    elements = {},
  }

  -- Create each element of the HUD.
  local hearts_builder = require("scripts/hud/hearts")
  local rupees_builder = require("scripts/hud/rupees")
  local bombs_builder = require("scripts/hud/bombs")
  local arrows_builder = require("scripts/hud/arrows")
  local item_builder = require("scripts/hud/item_icon")
  local magic_bar_builder = require("scripts/hud/magic_bar")
  local small_keys_builder = require("scripts/hud/small_keys")
  local boss_life_builder = require("scripts/hud/boss_life")
  local attack_icon_builder = require("scripts/hud/attack_icon")
  local action_icon_builder = require("scripts/hud/action_icon")

  local hearts = hearts_builder:new(game)
  hearts:set_dst_position(-88, 0)
  hud.elements[#hud.elements + 1] = hearts

  local rupees = rupees_builder:new(game)
  rupees:set_dst_position(121, 10)
  hud.elements[#hud.elements + 1] = rupees

  local bombs = bombs_builder:new(game)
  bombs:set_dst_position(153, 10)
  hud.elements[#hud.elements + 1] = bombs

  local arrows = arrows_builder:new(game)
  arrows:set_dst_position(178, 10)
  hud.elements[#hud.elements + 1] = arrows

  local item = item_icon_builder:new(game, 1)
  item:set_dst_position(27, 15)
  hud.elements[#hud.elements + 1] = item

  local magic_bar = magic_bar_builder:new(game, 1)
  magic_bar:set_dst_position(10, 8)
  hud.elements[#hud.elements + 1] = magic_bar

  local small_keys = small_keys_builder:new(game)
  small_keys:set_dst_position(88, 10)
  hud.elements[#hud.elements + 1] = small_keys

  local boss_life = boss_life_builder:new(game)
  boss_life:set_dst_position(110, 220)
  hud.elements[#hud.elements + 1] = boss_life

  menu = attack_icon_builder:new(self)
  menu:set_dst_position(13, 29)
  self.hud[#self.hud + 1] = menu
  self.hud.attack_icon = menu

  menu = action_icon_builder:new(self)
  menu:set_dst_position(26, 51)
  self.hud[#self.hud + 1] = menu
  self.hud.action_icon = menu

  -- Destroys the HUD.
  function hud:quit()

    if hud:is_enabled() then
      -- Stop all HUD elements.
      hud:set_enabled(false)
    end
  end

  -- Call this function to notify the HUD that the current map has changed.
  function hud:on_map_changed(map)

    if hud:is_enabled() then
      for _, menu in ipairs(hud.elements) do
        if menu.on_map_changed ~= nil then
          menu:on_map_changed(map)
        end
      end
    end
  end

  -- Call this function to notify the HUD that the game was just paused.
  function hud:on_paused()

    if hud:is_enabled() then
      for _, menu in ipairs(hud.elements) do
        if menu.on_paused ~= nil then
          menu:on_paused()
        end
      end
    end
  end

  -- Call this function to notify the HUD that the game was just unpaused.
  function hud:on_unpaused()

    if hud:is_enabled() then
      for _, menu in ipairs(hud.elements) do
        if menu.on_unpaused ~= nil then
          menu:on_unpaused()
        end
      end
    end
  end

  -- Returns whether the HUD is currently enabled.
  function hud:is_enabled()
    return hud.enabled
  end

  -- Enables or disables the HUD.
  function hud:set_enabled(enabled)

    if enabled ~= hud.enabled then
      hud.enabled = enabled

      for _, menu in ipairs(hud.elements) do
        if enabled then
          -- Start each HUD element.
          sol.menu.start(game, menu)
        else
          -- Stop each HUD element.
          sol.menu.stop(menu)
        end
      end
    end
  end

  -- Start the HUD.
  hud:set_enabled(true)

  -- Return the HUD.
  return hud
end

return hud_manager

I could not find the problem, sorry.
Maybe Christopho or other programmers can help you to find what is wrong.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

Alright, no problem. Thanks for the help with the dialog box problem  :)

Are you sure that "Error: In on_map_changed: [string "scripts/game_manager.lua"]:75: attempt to index upvalue 'hud' (a nil value)" is the first error you get?
Maybe it is the consequence of a previous error .

Error: In on_started: [string "scripts/hud/rupees.lua"]:12: attempt to call method 'set_transparency_color' (a nil value)

This is the only other error but I don't think its related since the other error has been appearing for a longer time

In the hud script, there are global menu variables that you could define as local. Also, you wrote self.hud instead of hud.elements, could that be related to the error?
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

Doesn't seem to be related since the errors are still occurring even after editing the code.

These two errors appear once the game loads   Error: In on_started: [string "scripts/hud/rupees.lua"]:12: attempt to call method 'set_transparency_color' (a nil value)
Error: In on_map_changed: [string "scripts/game_manager.lua"]:75: attempt to index upvalue 'hud' (a nil value)

and this error appears when the game ends   Error: In on_finished: [string "scripts/game_manager.lua"]:46: attempt to index upvalue 'hud' (a nil value)


I figured I would keep this in this thread so I tried to add the save game menu using the code used in ZSDX so that I could easily just change graphics but have the same feel later on, however now I am getting several errors that prevent me from starting the game at all. Its strange because I basically just used the exact code that zsdx uses...

Error: In timer callback: [string "scripts/menus/title.lua"]:27: attempt to index local 'zs_presents_img' (a nil value)
Fatal: Cannot open file 'settings.dat' for writing: The system cannot find the path specified.
Error: In on_finished: [string "main.lua"]:55: Internal error: Cannot open file 'settings.dat' for writing: The system cannot find the path specified.
Error: In on_started: [string "scripts/menus/savegames.lua"]:262: attempt to concatenate a nil value


As the error says, you are trying to load the settings.dat file, but this file (or the path you give to load it) do not exist.

On the other hand, I think you do not have the image that you store on the variable zs_presents_img in the correct path for it to be opened, or you are not opening it correctly, which would explain why your zs_presents_img variable is nil.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

I'm just not sure where to put settings :/   and now the savegame is still getting a nil value for a string even though I triple checked that it existed in strings.dat   

Code ( lua) Select
-- New file.
      local name = "- " .. sol.language.get_string("selection_menu.empty") .. " -"
      slot.player_name_text:set_text(name)
    end


Error: In on_started: [string "scripts/menus/savegames.lua"]:262: attempt to concatenate a nil value

Did you fix previous errors first? Try to fix the first one first, because others are often a consequence.