Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Reafy · Oct 11, 2014 at 12:34 PM · javascriptcheat

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
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scribe · Oct 11, 2014 at 01:29 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Creating realtime shadows without unity pro? 1 Answer

Loop Through Entire Array - Simon Game 1 Answer

Iphone Unity and Best book . 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges