View previous topic :: View next topic |
Author |
Message |
TwinHamster ♫ Furious souls, burn eternally! ♫

Joined: 07 Mar 2004 Posts: 1352
|
Posted: Fri Oct 13, 2006 7:36 am Post subject: Repetitive key-press script? |
|
|
I would like to create a script requiring the player to smash a certain key a bunch of times in a row to trigger an event.
An example of this script can be seen in RMZ's/Worthy's Gargan and Troy mini-game in which you're trying to make the tree laugh.
If it's not possible to have a single key pressed repeatedly, I would settle for the two-key alternation as well.
Thanks. |
|
Back to top |
|
 |
Camdog
Joined: 08 Aug 2003 Posts: 606
|
Posted: Fri Oct 13, 2006 7:45 am Post subject: |
|
|
Is speed an issue? Does the player have to press a key a certain number of times in a set time period, or is only the number of times the key is pressed that matters?
Will pressing a wrong key reset the sequence?
Will the game be paused when the button mashing is required, or will everything be going on as normal when you have an opportunity to button mash? |
|
Back to top |
|
 |
TwinHamster ♫ Furious souls, burn eternally! ♫

Joined: 07 Mar 2004 Posts: 1352
|
Posted: Fri Oct 13, 2006 9:04 am Post subject: |
|
|
Well, I was just asking in general.
So let's say I'm trying to get the player to press a single key a bunch'a times.
Would I have to have something like:
Code: |
script, some script, begin
set variable (key pressed, 1)
first key
end
script, first key, begin
if(Keyispressed(key))
then(
set variable (key pressed, keypressed+1)
first key
)
else(
I dunno, you die?
)
end
|
So that would just loop a lot. I'll probably add a counter into the event, requiring the player to press the key maybe fifty times in the designated time.
I think I can get that, but am I approaching the repetative keypressing part correctly? I've never fancied keypress scripts too much. |
|
Back to top |
|
 |
Camdog
Joined: 08 Aug 2003 Posts: 606
|
Posted: Fri Oct 13, 2006 10:49 am Post subject: |
|
|
You can't access a local variable defined in one script in another script unless you explicity pass that variable as an argument (and even then, that's not really accessing the variable itself, merely reading its value).
Anyway, I'm not sure why you broke this up into two scripts, except maybe you're trying to show that you'd call this from a master script? Regardless, all the logic should be contained in one script in this case.
The else in your logic block would trigger as soon as the player wasn't pressing the designated key, so having them die there would mean they'd die every time they let up the key, making the button mashing impossible.
I'd set up two variables, one for the number of times the key is pressed and one for the time limit in ticks, then loop until the time limit is reach or the button is mashed an appropriate amount of times.
The last thing we need to remember is that we need to distinguish between a key being pressed and a key being held down. Key is pressed returns a 1 if the key is typed, and a 2 if the key is held, so this makes determining this easy.
Code: | script, key mash, begin
variable(time) #how long we've been waiting
variable(reps) #how many times the key has been pressed
time := 0
reps := 0
#check for key mashing for the duration of the time limit
while(time << TIME_LIMIT) do(
if (key is pressed(KEY) == 1) then(
reps := reps +1
)
#if we've key mashed enough, finish the loop early
if (reps == KEYPRESSES_NEEDED) then(
time := TIME_LIMIT
)
#add another tick to time passed
time := time + 1
wait
)
#now we've evaluated the key mashing sequence, act accordingly
if (reps == KEYPRESSES_NEEDED) then(
#do what you want for success
)
else (
#do what you want for failure
)
end |
In the code above, tokens in all caps represent constants:
KEY: the key you want the player to press repeatedly
KEYPRESSES_NEEDED: the number of times you want the player to press it
TIME_LIMIT: the amount of time the player has
[edit] This should not be a "on keypress script". It should be called once when you wish the button mashing sequence to be triggered, such as when the player speaks to an appropriate NPC. |
|
Back to top |
|
 |
TwinHamster ♫ Furious souls, burn eternally! ♫

Joined: 07 Mar 2004 Posts: 1352
|
Posted: Fri Oct 13, 2006 1:12 pm Post subject: |
|
|
This'll work great! Thanks! |
|
Back to top |
|
 |
Moogle1 Scourge of the Seas Halloween 2006 Creativity Winner


Joined: 15 Jul 2004 Posts: 3377 Location: Seattle, WA
|
Posted: Fri Oct 13, 2006 1:47 pm Post subject: |
|
|
No, it won't. You can hold down the key in that script to rack up keypresses. You should add a check to make sure the key wasn't pressed during the last clocktick. _________________
|
|
Back to top |
|
 |
msw188
Joined: 02 Jul 2003 Posts: 1041
|
Posted: Fri Oct 13, 2006 2:30 pm Post subject: |
|
|
Isn't that what the keyispressed == 1 is checking for? (as opposed to keyispressed == 2, for holding a key down) |
|
Back to top |
|
 |
TwinHamster ♫ Furious souls, burn eternally! ♫

Joined: 07 Mar 2004 Posts: 1352
|
Posted: Fri Oct 13, 2006 2:56 pm Post subject: |
|
|
Holding down the key...right...
So I would have to make it a two-key system.
The adjustments shouldn't be too hard, I would just make sure that for each part:
Code: |
If(Keyispressed(Key 1)==true and (Key is pressed(Key 2)==false)
then(
Event)
else(
Minus keypresses
)
|
? |
|
Back to top |
|
 |
TMC On the Verge of Insanity
Joined: 05 Apr 2003 Posts: 3240 Location: Matakana
|
Posted: Fri Oct 13, 2006 5:22 pm Post subject: |
|
|
Camdog wrote: | Key is pressed returns a 1 if the key is typed, and a 2 if the key is held, so this makes determining this easy. |
This is not true. keyispressed returns only either true or false.
You want to use keyval, which returns 2 or 3 for a new keypress. A modified version of Camdog's script:
Code: | script, key mash, begin
variable(time) #how long we've been waiting
variable(reps) #how many times the key has been pressed
time := 0
reps := 0
#loop until finished mashing/ran out of time
while(time << TIME_LIMIT && reps << KEYPRESSES_NEEDED) do(
if (keyval(KEY) >> 1) then(
reps += 1
)
#add another tick to time passed
time += 1
wait
)
#now we've evaluated the key mashing sequence, act accordingly
if (reps == KEYPRESSES_NEEDED) then(
#do what you want for success
)
else (
#do what you want for failure
)
end |
_________________ "It is so great it is insanely great." |
|
Back to top |
|
 |
Camdog
Joined: 08 Aug 2003 Posts: 606
|
Posted: Fri Oct 13, 2006 9:53 pm Post subject: |
|
|
The Mad Cacti wrote: | This is not true. keyispressed returns only either true or false. |
Uh, yes it is. I had an issue dealing with this not too long ago (since in the plotscripting dictionary it says key is pressed merely returns true or false), and I asked about it here. Mike said in another thread:
Mike Caron (in another thread) wrote: | But, anyway. key is pressed() returns 1 if it's a "new" key press, and 2 if it's a repeat (or, possibly the other way around). I think that's valid behaviour. In most cases (i.e. those where you don't care what type of key press) you should always be testing "if(key is pressed())" or "if(key is pressed()==false)". |
So, the documentation is wrong, but what I said is right (unless someone changed the behavior of key is pressed, which would kind of suck since it'd mean I'd have to update some plotscripts...)
Anyway, yes, the checking key is pressed for 1 as opposed to 2 or true should prevent holding the key down from happening.
Also, Moogle, the logical "and" (&&) you're using is not in the latest version. Twinhamster would need to be using a nightly for your code to work, and you should really only be doing that for bugtesting, not developing a game.
[edit] Twinhamster: I'm not sure what the second bit of code you posted is supposed to do. How does a different key factor into what you're trying to do? |
|
Back to top |
|
 |
Moogle1 Scourge of the Seas Halloween 2006 Creativity Winner


Joined: 15 Jul 2004 Posts: 3377 Location: Seattle, WA
|
Posted: Fri Oct 13, 2006 10:32 pm Post subject: |
|
|
Camdog wrote: | Also, Moogle, the logical "and" (&&) you're using is not in the latest version. Twinhamster would need to be using a nightly for your code to work, and you should really only be doing that for bugtesting, not developing a game. |
I'm terribly sorry. Next time I write code in my head, I'll make sure it's compatible with the last release.
Or maybe you're referring to the code TMC actually posted on the board.  _________________
|
|
Back to top |
|
 |
Camdog
Joined: 08 Aug 2003 Posts: 606
|
Posted: Sat Oct 14, 2006 8:17 am Post subject: |
|
|
Hah! Yes, I was. Sorry about that. |
|
Back to top |
|
 |
TMC On the Verge of Insanity
Joined: 05 Apr 2003 Posts: 3240 Location: Matakana
|
Posted: Sun Oct 15, 2006 6:21 pm Post subject: |
|
|
Camdog wrote: | The Mad Cacti wrote: | This is not true. keyispressed returns only either true or false. |
Uh, yes it is. |
Uh no it isn't. Please refer to whatsnew, the closest thing to official documentation:
whatsnew.txt hasta-la-qb wrote: | * Bug 179: "Use of two keyispressed conditions in an if statement causes
massive weirdness" - "keyispressed" command has been changed so that it
now only returns a bool. It may help to recompile your scripts. If you
want the old behaviour, use the new "keyval" command [Ralph] |
As you point out, the plotdict documentation was incorrect, so it was corrected. The command, not the documentation. If you used the old behaviour in pre hasta-la-qb scripts, don't worry, your compiled scripts will work as they always did. However, if you recompile using recent plotscr.hsd's, you need to replace your key is pressed's with keyval's. You and James were the only people to use the old behaviour, as far as anyone knows.
If you want to compile that code with an old plotscr.hsd, replace the "&&" with ",and,". _________________ "It is so great it is insanely great." |
|
Back to top |
|
 |
TwinHamster ♫ Furious souls, burn eternally! ♫

Joined: 07 Mar 2004 Posts: 1352
|
Posted: Tue Nov 14, 2006 9:41 am Post subject: |
|
|
I've finally gotten a chance to actually test the script, and I found that the player can beat the game simply by holding down the button.
As Moogle pointed out earlier, I suppose there should be some sort of check to see if the key isn't held down.
(Unless you guys enjoy a mini-game in which you hold down a single button), where should this little "check" be inserted? And what command would I use to force the player to re-press the button?
Thanks again. |
|
Back to top |
|
 |
Camdog
Joined: 08 Aug 2003 Posts: 606
|
Posted: Tue Nov 14, 2006 10:40 am Post subject: |
|
|
This was described in the code I posted. Just use "keyval" instead of "key is pressed" |
|
Back to top |
|
 |
|