PDA

View Full Version : And still the questions continue: Rand



Muvlo
02-02-02, 10:21 AM
With the Rand an IRand functions, is there a way to set it up so that they are between some number other than zero and another number?

Muvlo
02-02-02, 11:02 AM
And another question:

Can I somehow have the script detect a key being pressed while it's going through a loop?

davey
02-02-02, 12:33 PM
The first question is easy: simply add another number to the random number:

Suppose you want some random number between 50 and 100:

[VarSet,SomeNumber,(IRand(50)+50)]
[Note,SomeNumber]

See? You simply use Rand or IRand to find a number between 0 and 50, then you add 50.

Your second question is a little more difficult. The short answer is no, the long answer is a qualified yes:

You may already know about the slider in the Preferences palette -- Preferences:Utilities:View Keyboard Status. This shows a number corresponding to the most recent key, or key combination, pressed.

However, while a ZScript is running, normal keyboard input is disabled (and the 'Keyboard Status' slider doesn't update). This is because enabling keyboard input might open up a can of worms -- if a user presses a certain keyboard shortcut, for example, should that function happen, should it wait until after the ZScript runs, etc.

However:

You can display a MessageOK (or any of the other Message... commands), which invites the user to click the OKAY button with the mouse. While the Message is visible, the slider will update.

It's important that the user doesn't press Enter to answer OK to the message, because that will put the number for Enter in that slider (13).

Here's an example of a simple button that uses this method to get a keystroke:

[IButton,"Get a Key","Key Input",
[MessageOK,"Press a key and click OKAY"]
[VarSet,TheKey,[IGet,Preferences:Utilities:View Keyboard Status]]
[Note,"You just pressed ",,-1]
[Note,TheKey]
]


Sounds like you've got another fun idea cooking -- can't wait to find out what it is :)

dave

Muvlo
02-02-02, 01:17 PM
Thanks for the answers Davey! :)

There'll probably be more questions coming up. I've actually just run in to this odd odd odd problem, must be a logic error somwhere (over the rainbow.) ;)