Author Topic: Hud: Item counter  (Read 1553 times)

magoon

  • Newbie
  • *
  • Posts: 2
    • View Profile
Hud: Item counter
« on: October 19, 2014, 04:02:53 pm »
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:
Code: [Select]
local item = ...
function item:on_obtaining(variant, savegame_variable)
    self:get_game():get_item("ore_counter"):add_amount(1)
end
ore_counter.lua:
Code: [Select]
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:
Code: [Select]
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


Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1172
    • View Profile
Re: Hud: Item counter
« Reply #1 on: October 19, 2014, 08:49:28 pm »
I don't know, do you have an error message in error.txt?
Do you correctly call your hud script from somewhere?

magoon

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Hud: Item counter
« Reply #2 on: October 19, 2014, 09:27:54 pm »
no error :/
yes, the hud is adapted from zelda xd. the counter is displayed but it is not updated :(

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1172
    • View Profile
Re: Hud: Item counter
« Reply #3 on: October 20, 2014, 08:51:23 am »
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.