Disabling key presses in a room

Started by shaungaryevans, Wed 10/06/2009 21:30:29

Previous topic - Next topic

shaungaryevans

hi,

how to disable key presses in a room please?

Thanks in advance

GuyAwesome

I haven't tried it, but I'd imagine creating an on_key_press in the room that just contains ClaimEvent() would do it. That is, it'll stop the global on_key_press running, and do nothing in its place.

shaungaryevans

I only want one key disabled in a room, not all of them

GuyAwesome

#3
OK, so just add an 'if (keycode == eKeyWhatever)' condition before ClaimEvent().

Or, you could edit the global on_key_press with 'if (keycode == eKeyWhatever && player.Room != X)', but the room based on_key_press keeps things neater.

shaungaryevans

Thank you for helping but I done it another way.

thanks

Trent R

Haven't you asked this twice before?

~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune


Trent R

I beg to differ.

Quote from: shaungaryevans on Sat 23/05/2009 22:47:08
Hi,

Can I deactivate a key then activate it later on in the game?

Quote from: shaungaryevans on Sat 23/05/2009 21:06:12
Can I deactivate a key then activate it later on in the game?



~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

GuyAwesome

#8
Well, that's really a different question to this one, and he didn't actually get an answer to it (either time), just didn't follow it up. (Quick answer: Yes. Use a bool and check if (keycode == eKeyWhatever && KeyBool == true). Make it true when you want the key to work, false to turn it off.)

But, this question is exactly the same as the one answered in the topic by Atelier you (Trent) linked in the first of those topics, complete with the exact same answer from you as I gave here. So well done on that, I guess - if we agree, it MUST be right ;)

Snake

#9
Why in the world haven't you just put in the room's REP_EXEC:

Code: ags

if (keycode==whateverKEY){
  //don't do anything
}

//NOTE: actually, in rooms it's IsKeyPressed, not KEYCODE...


Or in the on key press function in the global script:

Code: ags

//previously set a variable
//let's say it's this: 
int nokey;

//NOTE: 
//Remember to set this variable to 1 in the room you want this key to be disabled (before fade in)

//ON KEY PRESS CODE
if (nokey==1){}
else if (nokey==0){
  //key being pressed code here
}


These two things are what I have done in the past and they have worked perfectly fine. No need to think into it too much.

Sometimes coding something like this doesn't have to be as complicated as people try to code them around here. ClaimEvent()? Bah. Never heard of it ;)

//--EDIT--//
I forgot to mention that you'll have to export and import the integer so you can access it from within the room you want.
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Trent R

I'm agreed with you Snake, ClaimEvent wasn't part of my scripting vocab for a long time. My favorite use of it is actually with the on_event rather than key_press or mouse_click.

Question to the scripting masters: What functions will it actually claim? The manual mentions only key_press and mouse_click, but I'm guessing that it works for all functions listed on that page, right?



~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

GuyAwesome

#11
Snake:
Would the first method actually stop whatever's in the global on_key_press from running, though, or would it just not do anything else? (Which would be ... a little pointless, actually ;))

The second way actually might be easier if you need to do it for quite a number of rooms, I admit. If it's just for one or two, though, using ClaimEvent (and for what it was intended, no less) is probably easier than importing/exporting an int just to handle this.

EDIT2:
You could even simplify the script a little. Since nothing should happen if nokey == 1, you don't need to include that condition. It might be a little more complicated if you want to block a couple of different keys, but I suppose you could use an array for nokey, like so

Code: ags

int nokey[383]; // the highest ASCII code listed in the manual (that I could see)


//ON KEY PRESS CODE
if (keycode == eKeyWhatever && nokey[eKeyWhatever] == 0) {
  //key being pressed code here
}


Then do nokey[eKeyWhatever] = 1; to disable it and nokey[ekeyWhatever] = 0; to enable it. So yeah, OK, much simpler than ClaimEvent() in the long run :)

Trent:
I think you're right, any global script function that you can also have in the room script, can be claimed. It definitely works for unhandled_event, don't know about the others.
EDIT:
unhandled_event, not mouse_click
EDIT 3:
Actually, ARE there any others? interface_click and I guess dialog_request are obsolete, game_start probably can't be in room script (what would be the point as it runs before the first room), and apparently you can't claim repExec or repExec_always ('no event to claim' error).

Atelier

I think you could do it this way too...

Code: ags

//In global script, or wherever your on_key_press function is located...

function on_key_press()
{
   if (keycode == eKeySpace) gIconbar.Visible = true;
   if (player.Room == (whatever room number) return;
   if (keycode == eKeyReturn) gSettings.Visible = true;
}


This is just an example. Say you wanted to disable the return key in a certain room, put the second line of code just above it. If this disables other codes that you want enabled, make sure you put what you do want disabled at the very bottom of the function, like shown. Hope this helps! :)

Snake

Actually, GA, come to think of it, I think you're right. The first one I listed might work under certain circumstances, but not with this... I don't think. I might have actually been thinking of the second one when I was writing it. But yeah, the second one is definately a GO.

Trent: Heh. Yeah, half the stuff that's coded here for some of these questions is rediculous. Certainly great scripting minds (no fucking doubt, don't take this the wrong way), but, they can certainly be scripted quite simpler. If I can do the same thing these people are scripting and be confused by what the hell they are trying to explain... heh.
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

GuyAwesome

I just realised that my answer to 'can you disable and re-enable key presses' is essentially the same as Snake's second method. That's the difference between thinking globally and room-specific, I guess.

I think my problem is that I'm using other people's problems for coding practice but not actually working on a game of my own, so anything I code I tend to make as generic as possible. It has applications in far more situations, I can module-ise it with minimal fuss if I ever actually need it somewhere, and you don't have to (in this example) add a new int for every key you're likely to want to block. Downside is, it can take 20 lines to achieve what a single if (...) would do in practice ::)
I also tend (but not here) to over-function, just because I'd find using KeyOff(eKeyWhatever); a hell of a lot clearer than nokey[eKeyWhatever] = 1;, and that obviously leads to KeyOn, and a checking function IsKeyOn to use in on_key_press... All totally unnecessary if this is a one or two time thing but, to me, pretty useful for generic, easy to use code.

But I'm still not convinced that using a built-in function, in the way it's supposed to be used, counts as 'complicated' :P

Snake

QuoteBut I'm still not convinced that using a built-in function, in the way it's supposed to be used, counts as 'complicated' :P
Oh be quiet :)
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

SMF spam blocked by CleanTalk