Small Key HUD ?

Started by dpro_games, April 04, 2017, 09:05:39 PM

Previous topic - Next topic
April 04, 2017, 09:05:39 PM Last Edit: April 04, 2017, 09:10:36 PM by dpro_games
Hello, I followed Christopho's tuto and I don't understand how to display a small key counter...

The small_key item:

local item = ...
local game = item:get_game()

--small_key option
function item:on_created()
  self:set_shadow("small")
  self:set_can_disappear(false)
  self:set_brandish_when_picked(true)
  self:set_sound_when_picked("picked_small_key")
  item:set_amount_savegame_variable("small_keys_save")
end

--small_key save
function item:on_obtaining()
  item:add_amount(1)
end


Thank you for helping me !

There are several ways to do that. The easiest is to make a small key item that increments a saved value.

And then you need a HUD element that draws this saved value on the screen.

You need to create a menu, and attach it to the HUD script (if you use any).
You can use the savegame variable to display the correct number of small keys
You can look how it is done in MoSDX

Mystery of Solarus DX is quite old, I have more modular examples now.

For example: https://github.com/solarus-games/zelda-xd2-mercuris-chess/blob/dev/data/items/small_key.lua
Which calls code of this script: https://github.com/solarus-games/zelda-xd2-mercuris-chess/blob/dev/data/scripts/equipment.lua
With detection of the current map to know which small key counter to update (basically, which dungeon).

Another way is to make several small key items, one for each dungeon. Same thing for the map, the compass and the big key. A bit easier but very repetitive.

Thank for your answers but I'm really begining in the world of programmation... For now, I'm trying to modifiying the christopho's script for the HUD called rupee but I don't know how it work... I have already check those scripts but it's to complicate (I'm talking about mercuris chess)...

The ruppee code in the tutorial:

-- The money counter shown in the game screen.

local rupees_builder = {}

local rupee_icon_img = sol.surface.create("hud/small_key_icon.png")

function rupees_builder:new(game, config)

  local rupees = {}

  local digits_text = sol.text_surface.create({
    font = "white_digits",
    horizontal_alignment = "left",
    vertical_alignment = "top",
  })
  local money_displayed = game:get_money()

  local dst_x, dst_y = config.x, config.y

  function rupees:on_draw(dst_surface)

    local x, y = dst_x, 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

    rupee_icon_img:draw(dst_surface, x + 8, y)
    digits_text:draw(dst_surface, x, y + 10)
  end

  -- Checks whether the view displays correct information
  -- and updates it if necessary.
  local function check()

    local need_rebuild = false
    local money = game:get_money()
    local max_money = game:get_max_money()

    -- Current money.
    if money ~= money_displayed then

      need_rebuild = true
      if money_displayed < money then
        money_displayed = money_displayed + 1
      else
        money_displayed = money_displayed - 1
      end

      if money_displayed == money  -- The final value was just reached.
          or money_displayed % 3 == 0 then  -- Otherwise, play sound "rupee_counter_end" every 3 values.
        sol.audio.play_sound("rupee_counter_end")
      end
    end

    if digits_text:get_text() == "" then
      need_rebuild = true
    end

    -- Update the text if something has changed.
    if need_rebuild then
      digits_text:set_text(string.format("%03d", money_displayed))

      -- Show in green if the maximum is reached.
      if money_displayed == max_money then
        digits_text:set_font("green_digits")
      else
        digits_text:set_font("white_digits")
      end
    end

    return true  -- Repeat the timer.
  end

  -- Periodically check.
  check()
  sol.timer.start(game, 40, check)

  return rupees
end

return rupees_builder

Ok then yes, start with rupees, it is simpler than small keys. What are you trying to modify exactly and what went wrong?

Hmmmm, I do wonder if there is any difference between MoS and MC alteration of these scripts ? because MOS looks simpler ...

I don't really know what I modify... I'm trying with chance. For now I got two counter, one for rupee with rupee's icon and an other rupee's counter with key's icon but I would like it display the number of keys... I have a big problem with variable and the save in solarus... (Where is the variable "money"..., how to use a variable from an item...)

I have used the code you gived to me ("small_key" and "equipment") but now, when I take a key I got an error:

Error: In on_obtaining: [string "items/small_key.lua"]:12: attempt to call method 'add_small_key' (a nil value)

Why ?... What sort of things I need to do to make it work well ?!  :-\

It is explained in the comments of equipment.lua. You need to require("scripts/equipment.lua") at least once. I usually do it from scripts/features.lua

Quote from: dpro_games on April 05, 2017, 06:51:25 AM
(Where is the variable "money"..., how to use a variable from an item...)
game:get_money()

Quote from: dpro_games on April 05, 2017, 06:51:25 AM
(how to use a variable from an item...)
item:get_amount()
But small keys are more compilcated since we want different small key counters in each dungeon. Start with something easier like rupees or the bow and arrows.

It's not possible to have a simple script for the keys ? I don't want to have different key counters in each dungeon... I can't just have a really simple key counter like rupee but for the key ?

Well that's exacty the purpose of my equipment.lua script. It provides a function game:get_num_small_keys() so that it is very easy for the HUD.

Je peux parler fr ce sera plus simple ?