Solarus-Games English Forum

Solarus => Development => Topic started by: PeterB on January 04, 2020, 09:20:50 PM

Title: Help with name display code
Post by: PeterB on January 04, 2020, 09:20:50 PM
Hey guys,

I am after some help with my savegames.lua. I am trying to get the players name from the save1.dat file (or 2 or 3) and print it on the game select screen.

I can't see what I am doing wrong with drawing it on the screen, so am after some help please.

Here is the relevant section of code, and I have attached the savegames.lua file.

Thanks in advance.

Peter

local function read_savegames()

  for i = 1, 3 do
    local file_name = get_savegame_file_name(i)
    local surface = sol.surface.create(272, 16)
    surface:set_xy(24, 8 + i * 48)
    savegames_surfaces[i] = surface

player_name_text = sol.text_surface.create{
      font = "alttp",
      font_size = 11,
    }

    if not sol.game.exists(file_name) then
      games[i] = nil
  local name = "- " .. sol.language.get_string("savegames_menu.empty") .. " -"
      player_name_text:set_text(name)
    else
      -- Existing file.
    local game = game_manager:create(file_name)
    games[i] = game

-- Player name get and display
player_name_text:set_text(game:get_value("player_name"))
player_name_text:draw(surface, 90, 16 + i * 48)

    -- Hearts.
    draw_hearts(game, surface)
    end
  end
end
Title: Re: Help with name display code
Post by: llamazing on January 05, 2020, 06:00:45 AM
looks like your problem is this line:
Code (lua) Select
player_name_text:draw(surface, 90, 16 + i * 48)

You are drawing the text surface to surface, where surface is only 16 pixels tall. So it makes no sense to be drawing to a y coordinate of 16 + 48 = 64 pixels when i==1, which is beyond the edge of the surface and thus will not be visible. You'd want a y coordinate of 0 if your text is vertically aligned by "top", but I think you are using "middle" in which case it should be half the height of the text.

btw, you want to do that draw() line for both cases whether the savegame file exists or not, so it should be moved outside of the if..else block (not the cause of your immediate problem, but will your next problem to deal with).
Title: Re: Help with name display code
Post by: PeterB on January 05, 2020, 10:47:19 AM
Brilliant thanks Llamazing, i hadn't even thought about that. I thought it was probably that line, but as there weren't any errors i got sidetracked on font colours.

It all works now, i used

player_name_text:draw(surface, 64, 8)

which works a treat. Now on to the - Empty - file name problem!!!
Title: Re: Help with name display code
Post by: PeterB on January 05, 2020, 10:47:49 PM
Hey Llamazing,

Struggling with the - EMPTY - name if no save game present section. Any help gladly received as I am pulling my hair out with this one today. It doesn't seem to want to run the two lines                 local name = "- " .. sol.language.get_string("savegames_menu.empty") .. " -"
player_name_text:set_text(name)


For some reason!!! Here is the full section.

-- Reads the existing savegames and creates the savegames surfaces.
local function read_savegames()

  for i = 1, 3 do
    local file_name = get_savegame_file_name(i)
    local surface = sol.surface.create(272, 16)
    surface:set_xy(24, 8 + i * 48)
    savegames_surfaces[i] = surface

player_name_text = sol.text_surface.create{
      font = "alttp",
      font_size = 11,
    }

    if not sol.game.exists(file_name) then
      games[i] = nil
    else
      -- Existing file.
local game = game_manager:create(file_name)
games[i] = game

-- Player name get or use EMPTY
if sol.game.exists(file_name) then
player_name_text:set_text(game:get_value("player_name"))
else
                local name = "- " .. sol.language.get_string("savegames_menu.empty") .. " -"
player_name_text:set_text(name)
end
    -- Draw hearts and player name.
draw_hearts(game, surface)
player_name_text:draw(surface, 64, 8)
    end
  end
end 


full lua file and screen capture attached, shame you can't see the fairy's wings moving though. Thanks for any help.

Title: Re: Help with name display code
Post by: llamazing on January 07, 2020, 05:20:47 AM
Oops, I tried to hint at how to fix that problem in my last post. I must have not been clear enough.

Change this:
Code (lua) Select
    if not sol.game.exists(file_name) then
      games[i] = nil
    else
      -- Existing file.
local game = game_manager:create(file_name)
games[i] = game

-- Player name get or use EMPTY
if sol.game.exists(file_name) then
player_name_text:set_text(game:get_value("player_name"))
else
local name = "- " .. sol.language.get_string("savegames_menu.empty") .. " -"
player_name_text:set_text(name)
end
    -- Draw hearts and player name.
draw_hearts(game, surface)
player_name_text:draw(surface, 64, 8)
    end


To this:
Code (lua) Select
    if not sol.game.exists(file_name) then
      games[i] = nil
    else
      -- Existing file.
local game = game_manager:create(file_name)
games[i] = game

-- Player name get or use EMPTY
if sol.game.exists(file_name) then
player_name_text:set_text(game:get_value("player_name"))
else
local name = "- " .. sol.language.get_string("savegames_menu.empty") .. " -"
player_name_text:set_text(name)
end
    -- Draw hearts and player name.
    end
draw_hearts(game, surface)
player_name_text:draw(surface, 64, 8)


i.e. move the draw call to outside of the end block (so it happens on the empty condition as well).

FYI-- you'd probably get a faster response if you asked questions like these on discord instead of the forums.
Title: Re: Help with name display code
Post by: PeterB on January 08, 2020, 05:02:22 PM
Ok, brilliant thanks, thanks for the discord tip too.