Error message with hud (solved)

Started by The Pink Bunny, June 21, 2016, 11:15:17 PM

Previous topic - Next topic
June 21, 2016, 11:15:17 PM Last Edit: June 23, 2016, 08:19:29 PM by The Pink Bunny
I have a simple hud that I made to display health as hearts and magic as a number. It was working just fine until I made some change to a file that was NOT the hud script, and now it displays this error message whenever I start the game:

QuoteError: Failed to load script 'menus/hud': [string "menus/hud.lua"]:1: '=' expected

Which sounds like the script is missing an equals sign in line 1, except it is definitely there and I don't know what the problem is. Below is menus/hud.lua . I thought it was a simple syntax error, so I didn't want to bring it to the forum, but I have tried everything I can think of and I've got nothing. My game runs just fine if I comment out all references to hud.lua in my game manager (also down below).

Thanks for the help, and if it IS a stupid syntax error, sorry for wasting your time.

Code ( lua) Select
local hudmenu = {}

function hudmenu:create(game)  -- the game is passed as an input to the function so it can be used

local hud = {}
local hudsurface = sol.surface.create(256,224)
local hearts = 1
local heart = {}
local health = 0
local fullhearts = 0
local halfheart = 0
local magic = 0
local maxmag = 0
local magbar
local magbarbg
local magsize = 0
local jaricon
local jars = 0
local jartext
local decicon
local decs = 0
local dectext

sol.menu.start(game, hudmenu)

sol.timer.start(100, function()  -- runs health calculationsn 10x/sec instead of every frame
hearts = game:get_max_life()/4

for h = 1,hearts do
heart[h] = sol.sprite.create("hud/lifebar")
heart[h]:set_frame_delay(nil)
end

health = game:get_life()
fullhearts = math.floor(health/4)

for h = 1,fullhearts do
heart[h]:set_frame(0)
end

halfheart = 0   -- it's actually just binary "is there a half heart or not", but I need it to be able to do math

if health-fullhearts*4 == 1 or health-fullhearts*4 == 2 then
heart[fullhearts+1]:set_frame(1)
halfheart = 1
end

for h = fullhearts+halfheart+1,hearts do
heart[h]:set_frame(2)
end

magic = game:get_magic()
maxmag = game:get_max_magic()
magbarbg = sol.sprite.create("hud/magic_meter_bg")
magsize = math.floor(magic/max*34)

if magsize == 0 and magic > 0 then
magsize = 1
end

if magic == 0 then
magbar = sol.surface.create(10,34)
magbar:fill_color({0,0,0,0})
else
magbar = sol.surface.create(10,magsize)
magbar:fill_color({32,192,40})
end

jars = game:get_item("magic_jar_storage"):get_amount()
jartext = sol.text_surface.create({font = "8_bit", text = jars,})
decs = game:get_item("magic_dec_storage"):get_amount()
dectext = sol.text_surface.create({font = "8_bit", text = decs,})

return true
end)

jaricon = sol.sprite.create("entities/items")
jaricon:set_animation("drops/magic_jar")
decicon = sol.sprite.create("entities/items")
decicon:set_animation("drops/magic_jar")
decicon:set_direction(1)

function hudmenu:on_draw(hudsurface)

for h = 1,hearts do
heart[h]:draw(hudsurface, 21+(h-1)*8, 5)
end

magbarbg:draw(hudsurface, 5, 5)
magbar:draw(hudsurface, 8, 9+34-magsize)

jaricon:draw(hudsurface, 60, 10)
jartext:draw(hudsurface, 56, 18)

decicon:draw(hudsurface, 72, 10)
dectext:draw(hudsurface, 68, 18)

end

function hud:quit()
if sol.menu.is_started(hud) then
  sol.menu.stop(hud)
end
end

return hud

end

return hudmenu


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 dialog_box_manager = require("scripts/dialog_box")
local hudmenu = require("menus/hud")
local inventory = require("menus/inventory")


local game_manager = {}

-- Sets initial values for a new savegame of this quest.
local function initialize_new_savegame(game)
  game:set_starting_location("Hyrule Castle/dungeon")
  game:set_max_money(100)  -- default is 0
  game:set_max_life(12)
  game:set_life(game:get_max_life())
  game:set_max_magic(100)
  game:set_magic(100)
 
  item_cooldown = false
 
end

function item_cooldown_timer()
sol.timer.start(500, function()
item_cooldown = false
end)
end

-- 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.
    initialize_new_savegame(game)
  end

  local dialog_box
  local hud

  -- Function called when the player runs this game.
  function game:on_started()
hud = hudmenu:create(game)
    dialog_box = dialog_box_manager:create(game)

--game:set_command_joypad_binding("item_1", "button 1")
--game:set_command_joypad_binding("attack", nil)
--game:set_command_joypad_binding("pause", "button 7")
  end

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

    dialog_box:quit()
    dialog_box = nil
hud:quit()
  end

  function game:on_paused()
inventory:open(game)
  end
 
  function game:on_unpaused()
sol.menu.stop(inventory)
  end

  local rupee_icon = sol.surface.create("entities/miscellaneous.png")
  local rupee_text = sol.text_surface.create()
  rupee_text:set_font("8_bit")                   -- these three things are all set outside of the function so that they aren't redefined every frame (bad)

local magic_num = sol.text_surface.create()
magic_num:set_font("8_bit")
local life_num = sol.text_surface.create()
life_num:set_font("8_bit")

  function game:on_draw(dst_surface) --function is called every time game is redrawn (same as framerate)

--rupee_icon:draw_region(0, 16, 8, 8, dst_surface, 5, 5) -- reference Solarus documentation
--rupee_text:set_text(game:get_money())
--rupee_text:draw(dst_surface, 14, 8)
--[[
magic_num:set_text(game:get_magic())
magic_num:draw(dst_surface, 16, 16)

life_num:set_text(game:get_life())
life_num:draw(dst_surface, 16, 8)
--]]
  end
 
 
  function game:on_joypad_button_pressed(butt)
--[[
iBuffalo Classic USB Gamepad button guide:
SNES button - index (default command mapping)
A - 0 ("action")
B - 1 ("attack")
X - 2 ("item_1")
Y - 3 ("item_2")
L - 4 ("pause")
R - 5
select - 6
start - 7

For mysterious and currently unknown reasons, the controller only works the first time
you start the quest from the editor. After that, it doesn't even register anything
happening. Also, the editor keeps crashing, but I don't know if that's related.
Actually, maybe it is, because it crashes every third time I close the game window.
--]]

if butt == 5 then
game:get_item("magic_jar_storage"):on_using()
end
if butt == 4 then
game:get_item("magic_dec_storage"):on_using()
end
if butt == 6 then
print("save menu to be implemented later")
end
  end

  function game:on_key_pressed(key)

if key == "f" then
game:get_item("magic_jar_storage"):on_using()
end

if key == "g" then
game:get_item("magic_dec_storage"):on_using()
end

  end
 
  return game
 
end

return game_manager

I would put "local hudmenu = require("menus/hud")" above "local hud" (and not at the beginniing of your script).
      -- Create the game (but do not start it).
-- blablabla

      local dialog_box
      local hudmenu = require("menus/hud")
      local hud


If it does not solve your problem, then ... :'(, are you sure the paths of your scripts?

I moved it there, and it didn't make a difference. Everything is where it is supposed to be for the paths.

The syntax of your hud.lua is correct, I don't reproduce the error. Maybe you should send your whole project so that we can reproduce the problem.

Sure. Here's a link to the data folder as a .zip: https://drive.google.com/open?id=0B-z6SYGP3fRJbXBGaWNYMlgza2c

I went ahead and attached hud.lua, game_manager.lua, and main.lua to the post right here because I figure the problem is probably in there somewhere.

The game starts just fine, I don't have the error message you are reporting. Maybe it is specific to your savegame file? Can you attach your savegame?

June 23, 2016, 02:47:53 AM #6 Last Edit: June 23, 2016, 02:51:20 AM by The Pink Bunny
There aren't any savegame files. I just let it initialize a new savegame every time.

I checked and made many tests. Rename hud.lua to hud_old.lua. Create a new hud.lua and copy lines inside. I think your file is corrupted.

Yep, that solved it! I wonder how that happened. Thanks for you guys' help!