Hud: Item counter

Started by magoon, October 19, 2014, 04:02:53 PM

Previous topic - Next topic
Hi all,
i want an item counter to be displayed in the hud. But i dont get it to work :( what am I doing wrong??

Item:
ore.lua:

local item = ...
function item:on_obtaining(variant, savegame_variable)
    self:get_game():get_item("ore_counter"):add_amount(1)
end

ore_counter.lua:

local item = ...
function item:on_created()
self:set_savegame_variable("ore")
self:set_amount_savegame_variable("ore_amount")
self:set_max_amount(99)
end

hud:
ore.lua:

local ore = {}


function ore:new(game)

  local object = {}
  setmetatable(object, self)
  self.__index = self

  object:initialize(game)

  return object
end

function ore:initialize(game)

  self.game = game
  self.surface = sol.surface.create(48, 12)
  self.digits_text = sol.text_surface.create{
    font = "white_digits",
    horizontal_alignment = "left",
  }
  local item = self.game:get_item("ore_counter") 
  self.digits_text:set_text(item:get_amount())
  self.rupee_icons_img = sol.surface.create("hud/rupee_icon.png")
  self.money_displayed = item:get_amount()

  self:rebuild_surface()
end

function ore:rebuild_surface()
  local item = self.game:get_item("ore_counter") 
  self.surface:clear()
  self.money_displayed = item:get_amount()

  self.digits_text:set_text(self.money_displayed)
  self.digits_text:draw(self.surface, 16, 5)
end

function ore:set_dst_position(x, y)
  self.dst_x = x
  self.dst_y = y
end

function ore:on_draw(dst_surface)

  local x, y = self.dst_x, self.dst_y
  local width, height = dst_surface:get_size()
  if x < 0 then
    x = width + x
  end
  if y < 0 then
    y = height + y
  end

  self.surface:draw(dst_surface, x, y)
end

return ore


thanks in advance


I don't know, do you have an error message in error.txt?
Do you correctly call your hud script from somewhere?

no error :/
yes, the hud is adapted from zelda xd. the counter is displayed but it is not updated :(

Your code displays the counter but never changes its value. Use a timer to periodically update it. See what does the small key counter of Zelda Mystery of Solarus DX/XD for example.