Solarus Forum

Community => Your scripts => Topic started by: ffomega on December 03, 2016, 12:59:49 pm

Title: [Script] Slight change to the Rupee counter
Post by: ffomega on December 03, 2016, 12:59:49 pm
I thought I'd try poking around in the rupee script and came up with a neat little alternate counter.  You'll need to do the following:

1. download the gray and yellow digits fonts and place them in your /data/fonts folder (RIGHT-CLICK > SAVE AS):

gray digits ( - yellow digits (

Make sure you add the fonts to your game from WITHIN the quest editor as well otherwise your game may not run!!

2. copy this code and paste it into the rupees script located in the /scripts/hud folder:

Code: (lua) [Select]
-- The money counter shown in the game screen.

local rupees_builder = {}

local rupee_icon_img = sol.surface.create("hud/rupee_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 = "bottom",
  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
    if y < 0 then
      y = height + y

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

  -- 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
        money_displayed = money_displayed - 1

      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."rupee_counter_end")

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

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

      -- Show in gray if the 0 is reached.
      if money_displayed  == 0 then
      -- Show in yellow if 1/4 of the maximum is reached.
      if money_displayed <= max_money / 4 and money_displayed > 0 then
      -- Show in white if the current is grater than 1/4 of the maximum.
      if money_displayed > max_money / 4 then
      -- Show in green if the maximum is reached.
      if money_displayed == max_money then


    return true  -- Repeat the timer.

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

  return rupees

return rupees_builder

Rupees will display in the following format:

0 rupees = dark gray
1 - {max rupees / 4) - yellow
current rupees greater than {max rupees / 4) - white
max rupees - green


Happy mapping!
Title: Re: [Script] Slight change to the Rupee counter
Post by: ffomega on December 05, 2016, 02:37:46 am
For an added bonus, if you want to set your rupee counter up similar to how I had it in my old project, you can add or remove the leading zeros in the rupee script.  Just open it and go down to line 65 in the script.

Code: ( lua) [Select]
      digits_text:set_text(string.format("%04d", money_displayed))
Change ("%04d") to (%01d")