Solarus-Games English Forum

Solarus => Development => Topic started by: Diarandor on December 14, 2014, 03:26:47 AM

Title: I need some help to customize the command "action".
Post by: Diarandor on December 14, 2014, 03:26:47 AM
Hi! I was trying to execute a function (for example, to open a menu in the game) when the "action" button is pressed, but only in case the hero is not performing another action (talking, lifting,...). To do that, I tried to use the function:

function game:on_command_pressed(command)
  if command == "action" and game:get_command_effect("action") == nil then
     ---- ...my function...
  end
end

The problem is that this does not work when interacting with custom entities because the function game:get_command_effect("action") returns nil, and I think it is not possible to assign a command effect when the hero is close to a custom entity before the "action" command is pressed (maybe I'm wrong).

Could you help me to solve this problem?
Thanks in advance!
Title: Re: I need some help to customize the command "action".
Post by: Christopho on December 14, 2014, 09:28:15 AM
What you should do is add a second function that remembers if the action command would do something custom in one of your scripts, like with a custom entity.
So you would have two functions:
- game:get_command_effect("action"): returns the built-in action or nil. This function is in the engine. There is is game:set_command_effect() because it is the engine who decides.
- game:get_custom_command_effect("action") and game:set_custom_command_effect("action", "something"): when the built-in action is nil, returns if your script would do something. These functions is not in the engine, they will be added by your quest. Call game:set_custom_command_effect() from your custom entity script.
And it is only when both of them return nil that nothing happens.

Actually, these additional functions game:get/set_custom_command_effect() already exist in ZSDX, I use them for the HUD.Indeed, the action icon of the HUD displays more state than just the built-in effect of the action command.

Last remark: if the player has the ability to run, the action command can be used to run. Unfortunately, the engine still returns nil in this case when you call game:get_command_effect("action"). I condiser this as a bug.
Title: Re: I need some help to customize the command "action".
Post by: Diarandor on December 14, 2014, 05:11:52 PM
Thanks again!
I will do as you say, mimicking some functions of the HUD to manage my custom actions.
Title: Re: I need some help to customize the command "action".
Post by: Cadwyn on December 28, 2014, 01:38:49 AM
Hello. I am doing something similar, trying to customize the action command so that the hero can jump when not performing another action (as if they had the feather item). Unfortunately I am very new to lua coding and am not sure how to implement this. Here is the code I have so far:

-- Event called when action button is pressed while not near any interactable entities

local command = game:is_command_pressed("action")
if command and game:get_command_pressed("action") == nil then
sol.audio.play_sound("jump")
local hero = self:get_map():get_entity("hero")
local direction4 = hero:get_direction()
hero:start_jumping(direction4 * 2, 32, false)
self:set_finished()
end


However, I'm not sure even where to put this code where it might work. I tried putting it in the main.lua script, but nothing happens. Is there somewhere else I should put it? Would the code not even work at all as written?
Title: Re: I need some help to customize the command "action".
Post by: Christopho on December 28, 2014, 11:07:58 AM
You should try something like

function game:on_command_pressed(command)
  if command == "action" and
      not game:is_suspended() and
      game:get_hero():get_state() == "free" and
      game:get_command_effect("action") == nil then
    .......... (your code to jump)
  end
end

Put this code where your game object is created. In my tutorials, this would go to game_manager.lua.
I have not tested but this should be the idea.
Title: Re: I need some help to customize the command "action".
Post by: Cadwyn on December 28, 2014, 03:55:04 PM
Thank you!