- Home /
How to implement cheat codes (Javascript)
So I have already looked it up but the simplest one I found was C# and i only know JavaScript. So could someone either convert this into Js for me, or show me different Js code for cheats
private string[] cheatCode;
private int index;
void Start() {
// Code is "idkfa", user needs to input this in the right order
cheatCode = new string[] { "i", "d", "k", "f", "a" };
index = 0;
}
void Update() {
// Check if any key is pressed
if (Input.anyKeyDown) {
// Check if the next key in the code is pressed
if (Input.GetKeyDown(cheatCode[index])) {
// Add 1 to index to check the next key in the code
index++
}
// Wrong key entered, we reset code typing
else {
index = 0;
}
}
// If index reaches the length of the cheatCode string,
// the entire code was correctly entered
if (index == cheatCode.Length) {
// Cheat code successfully inputted!
// Unlock crazy cheat code stuff
}
}
Just to point out something that might cause a problem for you, an example of a situation that might occur: Say your cheat code is 'idkfa' and then say your user entered the letters 'ididkfa' Although what they entered actually contains the correct cheat code, the code you posted will not recognise it as correct.
To ~somewhat~ fix you should have another check in your else where you set the index to 0 if the letter entered is completely wrong, or set the index to 1 if the letter entered was wrong in the current pattern but is the first letter of your desired sequence. This will still be broken by repeats in your cheat codes for example if your code is 'ididkfa' and your user enters 'idididkfa'!
I think a better technique would be if you know the maximum length of cheat code, just store the last x key presses and see if those key presses contain your cheatcode.
Answer by Redeemer86 · Oct 11, 2014 at 01:04 PM
Remove... Using unity engine at the begining Remove ... Class xample : mono behavior {}
Change the declaration to : private var string[] cheat code; private var index : int;
Instead of all " void" before functions replace with " function". Rest of the code you posted doesn't seem to require change.
Red.
Answer by MyUnitydream · Aug 29, 2016 at 04:57 AM
Hi To All
you can use this plugin: https://www.assetstore.unity3d.com/en/#!/content/59794
by thanks myunitydream
Your answer
