<SOLVED> Switch between two sprites by pressing a key?

Started by Eyth, May 17, 2018, 02:35:10 PM

Previous topic - Next topic
Hello there  ;)
New here and just started with Solarus and the programming-thing  :P
First, thank you for such an amazing programm! Great job.

My question:
I already have my own character-sprite implimented as the "hero". The thing is, that my idea is, to be able to switch the sprite of my hero between two sprites everytime I press a certain key. (F.e. I press "w" -> sprite of hero changes to "tunic1", I press "w" (again) -> sprite of hero changes to "tunic2", and back, and back...)
I already tried some things myself, but can't really figure it out...
(Still got problems with the whole "game:get things, game:set things" thing ^^)

One thing you could try is going into your main.lua script. There's some lines in there that handle key presses, I think the function is sol.main:on_key_pressed? Something like that. Anyway, that handles special keys, you can see there the format is stuff like, if key==f5 do this, if key ==escape, do this. You'll want some code like:

If key == "w" then
If game:get_ability("tunic") == 1 then game:set_ability("tunic", 2)

And vice versa. If you're still not familiar enough with Lua to notice, what I typed is not correct code (there are no "end"s for the conditional branches), so don't try copy/pasting or it'll just give you errors, haha, but that's the general idea which I think should work. You can look those methods up in the Solarus documentation and ask more questions of you don't understand them : )

Thanks for the tip ;)
I understand the lua-concept a little bit. I know what you mean with the "ends" ;)
I'll try your advice, since that is something I've not tried yet ^^

May 22, 2018, 12:46:45 PM #3 Last Edit: May 23, 2018, 07:10:22 PM by Neovyse
So I tried Max's advice and had to play a little bit around. But IT WORKED!!
I post my result, if anyone has a similar task ;)
I tried so many things and as often with new programs, overcomplicated it  :P

I now didn't go with the "get_ability()" method in the main lua, but with "set_tunic_sprite_id()" directly in the hero.lua (see below)
I set up two sprites (playersprite1 & playersprite2) and now let the engine change between those two whenever "w" is pressed.
Exactly what I wanted, thanks again Max for the great tip, which made it work: ;)

Code (lua) Select

--Initialize the behaviour of the hero in the quest--

require("scripts/multi_events")

local hero_meta = sol.main.get_metatable("hero")

local function initialize_player(game)
                local player = game:get_hero() --Took the name "player", because the sprite has the same name--
                player:set_tunic_sprite_id("hero/playersprite1") --Sprite to begin with--

      function sol.main:on_key_pressed(key, modifiers) --key-press function--

        local handled = false
        if key == "w" then --change on key "w"--
              if player:get_tunic_sprite_id() == "hero/playersprite1" then --if current sprite is sprite1, then--
                 player:set_tunic_sprite_id("hero/playersprite2") --change to sprite2--
              elseif player:get_tunic_sprite_id() == "hero/playersprite2" then --and back--
                     player:set_tunic_sprite_id("hero/playersprite1") --to sprite1--
              end
          handled = true
        end
        return handled
      end
end

--Set up the hero features on any game that starts--
local game_meta = sol.main.get_metatable("game")
game_meta:register_event("on_started", initialize_player)
return true

Tip: you can write lua with automatic syntax coloring. Click on the "Insert code" button and write code=lua in the first token.


Code (lua) Select

local function my_function()
  -- content
end

Quote from: Eyth on May 22, 2018, 12:46:45 PM
      function sol.main:on_key_pressed(key, modifiers) --key-press function--
You don't want to use sol.main:on_key_pressed() here. You should only use this function for responding to key presses that are applicable at all times while your quest is running, including when there is not an active game (such as the character select or title screen). The way you have it now you'll probably get an error if the player exits to the title screen and presses the w key because game and hero won't exist any more. Use game:on_key_pressed() instead.

You could also run into problems if you have multiple scrips calling game:on_key_pressed() as the scripts will end up overriding each other. You might be able to get away with using multi_events for game:on_key_pressed() depending on what you are trying to do. A better solution might be to create a menu to handle processing the key presses and call menu:on_key_pressed() instead.

A very old video (my first video) may motivate you and shows things you can do.
https://www.youtube.com/watch?v=iY-yckilRrg&t=0s&list=PLysNP7i5PKBCgT898-xreWyPcaByEB6Rg&index=72
My switch hero system is different from yours (you want one like in Secret of Mana, which is simpler) and mine is like the one in Lost Vikings games.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

@neovyse: Thanks for the tip, I already thought, there was something like this ^^

@Ilamazing: Thanks for the reply. That makes sense. It wasn't a problem till now, because I haven't made a titlescreen for now. But I will consider it  ;)

@Dirandor: Thanks for the link. I will watch it. Maybe I can use something from it  :)

The code/script is constantly improving and evolving. For now its working perfectly. Thanks again