Author Topic: Small Key HUD ?  (Read 923 times)

dpro_games

  • Newbie
  • *
  • Posts: 33
    • View Profile
Small Key HUD ?
« on: April 04, 2017, 09:05:39 pm »
Hello, I followed Christopho's tuto and I don't understand how to display a small key counter...

The small_key item:

Code: [Select]
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 !
« Last Edit: April 04, 2017, 09:10:36 pm by dpro_games »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1053
    • View Profile
Re: Small Key HUD ?
« Reply #1 on: April 04, 2017, 09:22:18 pm »
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.

MetalZelda

  • Hero Member
  • *****
  • Posts: 511
    • View Profile
Re: Small Key HUD ?
« Reply #2 on: April 04, 2017, 09:23:50 pm »
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

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1053
    • View Profile
Re: Small Key HUD ?
« Reply #3 on: April 04, 2017, 09:28:08 pm »
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.

dpro_games

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Small Key HUD ?
« Reply #4 on: April 04, 2017, 09:31:48 pm »
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:

Code: [Select]
-- 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

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1053
    • View Profile
Re: Small Key HUD ?
« Reply #5 on: April 04, 2017, 10:19:55 pm »
Ok then yes, start with rupees, it is simpler than small keys. What are you trying to modify exactly and what went wrong?

MetalZelda

  • Hero Member
  • *****
  • Posts: 511
    • View Profile
Re: Small Key HUD ?
« Reply #6 on: April 05, 2017, 01:18:58 am »
Hmmmm, I do wonder if there is any difference between MoS and MC alteration of these scripts ? because MOS looks simpler ...

dpro_games

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Small Key HUD ?
« Reply #7 on: April 05, 2017, 06:51:25 am »
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...)

dpro_games

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Small Key HUD ?
« Reply #8 on: April 05, 2017, 02:13:38 pm »
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 ?!  :-\

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1053
    • View Profile
Re: Small Key HUD ?
« Reply #9 on: April 05, 2017, 02:23:53 pm »
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

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1053
    • View Profile
Re: Small Key HUD ?
« Reply #10 on: April 05, 2017, 02:25:31 pm »
(Where is the variable "money"..., how to use a variable from an item...)
game:get_money()

(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.

dpro_games

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Small Key HUD ?
« Reply #11 on: April 05, 2017, 02:36:04 pm »
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 ?

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1053
    • View Profile
Re: Small Key HUD ?
« Reply #12 on: April 05, 2017, 02:39:20 pm »
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.

dpro_games

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Small Key HUD ?
« Reply #13 on: April 05, 2017, 02:40:32 pm »
Je peux parler fr ce sera plus simple ?

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1053
    • View Profile
Re: Small Key HUD ?
« Reply #14 on: April 05, 2017, 02:45:08 pm »
Oui pas de souci :)