Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

View Poll Results: What did you think of this tutorial?
It was very useful. 12 85.71%
A few bits and pieces came in handy. 2 14.29%
Nah, it didn't help me learn at all. 0 0%
Voters: 14. You may not vote on this poll

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-30-2002, 05:17 PM
GrowlZ1010 GrowlZ1010 is offline
defunct
Join Date: May 2002
Posts: 187
GrowlZ1010 is on a distinguished road
Post GrowlZ's Tutorial #1: The Keyboard

Yes, Graal now has full keyboard detection. Neat, huh? For those of you who want to use this handy new feature, but don't know quite how yet, here's your opportunity to learn.

To understand this properly, you need to understand that the keyboard has two basic types of key: character keys and operation keys. Character keys are buttons that produce a symbol or letter when pressed, for example the "S" or "5" keys. Operation keys are keys that cause the computer to do a special function, for example: "Caps Lock", "Tab" (which sometimes prints a space, but which Graal views as an operation key), "Return" and all the cursor keys (Up, Down, Left and Right). Graal's "keypressed" keyboard detection can only handle character keys. With that cleared up, let's begin.

There's one new event, keypressed. This is for detecting when a character key is pressed. So:

NPC Code:

if (created){
setimg book15.png;
hide;
}
if (keypressed){
show;
}


... will cause a NPC to show itself when a character key is pressed. Simple enough.

But how, you may ask, do you find out which key has actually been pressed? Here's when we find out about the two new messagecodes, #p(0) and #p(1).

NOTE: Yes, I know #p messagecodes were formerly used for triggeraction parameters. In fact, they still are. #p(2) will still give you a triggeraction param, rather than a key name. But #p(0) and #p(1), for all practical purposes, are now keyboard messagecodes. I haven't been able to test what happens if a NPC that has an action triggered on it checks #p(0) (will it return a triggeraction parameter, or a keycode?) because triggeraction always sucks and refuses to work for me. If anyone can find out the result, I'd appreciate it. And now, back to the tutorial.

#p(0) returns the KEYCODE of the character. A keycode is a number assigned to a character to distinguish it from the others: for example, the character "p" has the keycode 80, whereas the character "s" has the keycode of 83.

IMPORTANT! Keycodes differ between case. A small letter "p" won't return the same keycode as the capital "P".

#p(1) returns the actual NAME of the character. For example, the name of the character "p" is, surprisingly enough, "p". "q" has the name "q", "f" is "f", and "@" comes out as "@". The character's name is basically just the character itself.

Small thing to consider here: Graal doesn't seem to be able to recognize the name of the "," character. So try and leave that out of your #p(1) scripts.

So to test if the key "p" has been pressed using #p(0), it would be:

NPC Code:

if (keypressed && strequals(#p(0),80)){
setplayerprop #c,You pressed "p"!;
}


To do the same with #p(1):

NPC Code:

if (keypressed && strequals(#p(1),p)){
setplayerprop #c,You pressed "p"!;
}


Keyboard detection has also added two new functions, keycode(character) and keydown2(keycode,ignorecase<false/true>).

Keycode(character) lets you check the keycode of any character you want. You can use this to bypass #p(0) altogether, using keycode(#p(1)), but why would you want to do that? :P

Keydown2(keycode,ignorecase<false/true>) is a little more complicated. This function actually uses the other function, keycode(character), to find out the button you want to press. Now, as I've told you earlier, case matters when we're talkin' about keycodes... or at least, it does without the help of keydown2. The ignorecase part has two states: "true" or "false". If ignorecase is "true", then no matter whether the key is a or A, p or P, f or F, it will always be treated as the same letter. Here's an example of how you would use keydown2.

NPC Code:

if (created){
timeout = 0.05;
}
if (timeout){
if (keydown2(keycode(A),true)){
setplayerprop #c,You pressed "a" or "A"!;
}
timeout = 0.05;
}


In case you're wondering, it's in a timeout loop because functions have to be checked by something else. They can't do themselves when something happens.

This ends my keyboard tutorial: anything you need clarified, comments, suggestions, complaints, etc., just post 'em here. Or say what you thought with the poll. I'm listening!

The attatchment below is a Graalscript to help you check keycodes for characters. It requires Graal v.2.14 or later to work properly.
Attached Files
File Type: graal keypress.graal (1.3 KB, 223 views)
Reply With Quote
  #2  
Old 06-30-2002, 09:05 PM
Poogle Poogle is offline
Registered User
Poogle's Avatar
Join Date: Jun 2001
Posts: 2,471
Poogle is on a distinguished road
ooo good job
Reply With Quote
  #3  
Old 06-30-2002, 09:11 PM
Dark-Dragoon Dark-Dragoon is offline
Registered User
Dark-Dragoon's Avatar
Join Date: May 2002
Posts: 886
Dark-Dragoon is on a distinguished road
Thumbs up Nice job

Excellent work.
__________________
R.I.P. 1999-2004 Graalian Year
The World Renown Nobody, Guy.
Reply With Quote
  #4  
Old 06-30-2002, 09:40 PM
Artificial_Sweetener Artificial_Sweetener is offline
Registered User
Artificial_Sweetener's Avatar
Join Date: Mar 2002
Posts: 758
Artificial_Sweetener is on a distinguished road
They should sticky this tutorial.. in fact they need to sticky all tutorials :o
__________________
OOC: Scarlett (Head Auctioneer)
IC: Queen Arianna Durime of (Inra)
Reply With Quote
  #5  
Old 06-30-2002, 09:42 PM
user13-xo user13-xo is offline
Registered User
Join Date: Nov 2001
Location: California
Posts: 297
user13-xo is on a distinguished road
Send a message via AIM to user13-xo
Great Job
Reply With Quote
  #6  
Old 07-01-2002, 08:36 AM
Falcor Falcor is offline
Darth Cucumber
Falcor's Avatar
Join Date: Mar 2001
Location: At School
Posts: 2,874
Falcor is on a distinguished road
Send a message via ICQ to Falcor Send a message via AIM to Falcor Send a message via MSN to Falcor Send a message via Yahoo to Falcor
Keycodes are a dirivitive of the ascii indexes of the characters. So if you have an ascii conversion chart (or memorized it) it would help to some extent (knowing that space is 32 and so on)

It works something like

0x<special><index>

so if you press shift... a, you would have

0x141
or
16^2 *1 + 16^1 *4 + 16^0 *1
which is 321

I use 41 as the index becasue the hexidecimal value for 65 (keycode for a) is 41 (this is why an ascii chart is sometimes needed, its hard to explain how hexidecimal values work, its base 16 so figure it out)

the special case would be if shift or ctrl were true. (im guessing, since this is what it seems to do)

special = 1 would be shift
speical = 2 would be ctrl

so control + a would be
0x241
or
16^2 *2 + 16^1 *4 + 16^0 *1
which is 577
__________________

subliminal message: 1+1=3

Last edited by Falcor; 07-01-2002 at 09:21 AM..
Reply With Quote
  #7  
Old 07-01-2002, 03:19 PM
Xbob42 Xbob42 is offline
Registered User
Join Date: May 2002
Posts: 429
Xbob42 is on a distinguished road
Thumbs up

Good job.

I'm a little lazy, but I know if you open up visual basic you'll find the numbers for all the buttons in there.
__________________
Criminal uses this account to post.
Reply With Quote
  #8  
Old 07-04-2002, 05:43 AM
Aestus Aestus is offline
Registered User
Join Date: Apr 2002
Posts: 665
Aestus is on a distinguished road
I love you for this.

Marry me?
__________________
AIM: CapraConnubialis

"Be excellent to each other. And party on, dudes." - Abraham Lincoln
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 09:11 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.