Thread: Keys in Order
View Single Post
  #4  
Old 01-14-2014, 04:07 AM
100Zero100 100Zero100 is offline
Registered User
Join Date: Jul 2006
Posts: 31
100Zero100 is on a distinguished road
Quote:
Originally Posted by cbk1994 View Post
What do you have so far? Post some code.

Probably the simplest approach is keep track of the player's current position in a combo. When they hit the right key, increase the position. When they hit a wrong key, reset their position. If they hit the end of the combo, they've hit all the keys in the right order, so do whatever action.

But there are lots of potential approaches here, so post some code so we know where you're getting stuck.
I disagree about simplest. In your method, the script would have to simultaneously track player's progress through multiple combinations. If one combination was QWERTY and another was ERGAF and another was ROCKSTAR, then a player who typed QWER needs to be recognized as being 4/6 done with QWERTY, 2/5 with ERGAF, and 1/8 with ROCKSTAR. That's more complex.

Without testing/off the top of my head, I believe the simplest, most efficient, and most editable method would be something like as follows:

NPC Code:

//#CLIENTSIDE
const resetTime = 2;
function onCreated() {
this.codes = {
"qwerty",
"asdf",
"zxcv",
"etc"
};
this.text = "";
}
function onKeyPressed(keycode, key) {
this.text @= key;
for (temp.ending: this.codes) {
if (this.text.ends(ending))
// Code triggers
}
cancelEvents("reset");
scheduleEvent(resetTime, "reset");
}
function onReset() {
this.text = "";
}



The example reset time there is 2 seconds. That is an individual 2 seconds, however. That would be best in the scenario where you give the player two seconds between every keystroke. That means, for "asdf", they could hit A, wait 1.9s, hit S, wait 1.9s, hit D, wait 1.9s, and hit F, and it would register as the ASDF ending.

If you instead wanted to give a player 2 seconds from first-letter to last-letter to type the full combo, it would be:

NPC Code:

//#CLIENTSIDE
const resetTime = 2;
function onCreated() {
this.codes = {
"qwerty",
"asdf",
"zxcv",
"etc"
};
this.text = "";
}
function onKeyPressed(keycode, key) {
this.text @= key;
for (temp.ending: this.codes) {
if (this.text.ends(ending))
// Code triggers
}

scheduleEvent(resetTime, "remove");
}
function onRemove() {
this.text = this.text.substring(1);
}



In this scenario, typing a letter adds it to the string. In 2 seconds, the scheduleEvent will finish and the first letter (that letter) will be removed from the string. Thus, doing A - wait 1.9s - S - etc method won't work, because 0.1s after typing S, the A would be removed from the combo.

Of course, scheduleEvents() like this tend to mess up sometimes if a player over-spams (notably when they hit keys with both hands at the same time rapidly). I believe it's due to the scheduleEvent() limit being exceeded. However, in this scenario that wouldn't actually hurt our effect. What would occur is some letters would be added to the this.text string that would never get removed. While unideal, that's pointless: the script only cares about how it ends. While in theory you could possibly get it to "glitch" on several letters that actually matter, due to the chaotic nature of "random button mashing" it's extremely unlikely.
Reply With Quote