Author Topic: <SOLVED> Freezing/locking keyboard during specific event?  (Read 75 times)

Eyth

  • Newbie
  • *
  • Posts: 8
    • View Profile
<SOLVED> Freezing/locking keyboard during specific event?
« on: June 14, 2018, 04:26:57 pm »
Hi there again  ;)

Today I have a quick question. Hope I can describe it the right way:
I made a custom item with animations and all of that, the hero can use with the "item_1" command key (x). Works - so far so good ^^
But when I press the key, while the animation of the item used is running - the animation stops and starts new. In further consequence errors occur obviously...

Is it possible to lock the keyboard input, til item:on_using is finished, or you may have another idea?
Thanks for your advises in advance  :)
« Last Edit: Today at 11:15:56 am by Eyth »

Max

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Freezing/locking keyboard during specific event?
« Reply #1 on: June 14, 2018, 04:39:11 pm »
I've tackled similar problems this way:

I create a variable, "using_item", for example. This should probably be a variable local to the item's script, set it to false when the item is created. For the item:on_using() event, or the on_key_pressed, start it with the condition of if using_item ~= true. Then set the variable using_item to true first thing, and once you're done using the item (after a timer, or on_item_finished or whatever) set using_item back to false.

I think I might have some of the events referred to improperly here, but does the idea make sense?

Eyth

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Freezing/locking keyboard during specific event?
« Reply #2 on: June 14, 2018, 05:09:12 pm »
Yes, makes sense. Didn't think of that ^^
I'll have to try, to see how it fits best.

Thanks for the fast reply  ;)

Diarandor

  • Hero Member
  • *****
  • Posts: 957
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Freezing/locking keyboard during specific event?
« Reply #3 on: June 14, 2018, 07:37:04 pm »
To do it in a clean way you'll need to modify the event on_command_pressed and do nothing if the item is being used. That will avoid annoying built-in stuff like the hero state and animations being restarted. As Max says, it is convenient to define item:is_being_used() (or item:can_be_used()) for all items.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Eyth

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Freezing/locking keyboard during specific event?
« Reply #4 on: Today at 09:42:25 am »
So, back in the topic.
The tips above work perfectly for "normal" items:
local variable ("using_item") -> set false -> in on_using() set true -> and tackle on_using() with: if using_item ~= true then -> and on_item_finished set false again.

But... I made an item, that creates a duplicate (entity having the same sprite) of the hero - like in Minish Cap :)
This duplicate cannot do anything, it is just standing where it is created and vanishes after a timer.
While the duplicate is on screen, I wanted the hero to be able to walk around normaly. Therefore I unfreeze him in the middle of the item (crazy me!  :P) when the duplicate is created. Works, but then I can use the item again because the hero is back in his normal state and there comes the problem of the item beeing able to activate again, especially in the same spot, where it causes an error, because at the end of the item it removes the entity sprite. And when there are two sprites it cannot remove the first one created.

Hope you have an idea or something, this item seems to be a special candidate ^^
Thanks in advance again ;)

Eyth

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Freezing/locking keyboard during specific event?
« Reply #5 on: Today at 11:15:41 am »
Solved it on my own ^^

The Problem with this is, that the hero gets out of the item state and is able to use another "instance" of the same item, I feel.
So whatever you do with variables, its working but the engine says there is no first entity sprite to remove at the end, if you have created two of them.

For others with similar tasks:
So what I did now, is to set the keyboard binding of the item slot nil, when the animation of the item starts...
game:set_command_keyboard_binding("item_1", nil)
... and give it back to the key (x) after the sprite of the entity is removed
game:set_command_keyboard_binding("item_1", "x")

For me that works perfectly fine for now, because I dont intend to use another item, while the duplicate is activ (and its way simpler) :)
« Last Edit: Today at 11:23:39 am by Eyth »

Diarandor

  • Hero Member
  • *****
  • Posts: 957
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: <SOLVED> Freezing/locking keyboard during specific event?
« Reply #6 on: Today at 03:46:04 pm »
You should not set the state to false when on_item_finished() is called by the engine, but call it yourself when the item action has TRULY finished.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”