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
7
Question by AngryAnt · Dec 21, 2009 at 05:12 PM · input

How could I implement cheat codes in my game?

I would like to implement Doom-style cheat codes. That is when the user types in a given succession of letters, something should happen in my game. How would I go about implementing that?

Comment
Add comment
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

2 Replies

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

Answer by AngryAnt · Dec 21, 2009 at 05:12 PM

Its actually pretty straight forward. What you need to consider is the delay before the currently typed in string is cleared, which codes you'll accept and possibly a target GameObject - if you plan on handling the accepted codes somewhere else.

The code below shows a simple implementation. Try saving it out as CheatCodeListener.cs, adding it to a GameObject and populating the codes list with "Test".

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class CheatCodeListener : MonoBehaviour { private string m_Keys = ""; private float m_LastKeyTime;

 public List <string> m_Codes = new List <string> ();
 public float m_ClearDelay = 2.0f;
 public GameObject m_Receiver = null;

 void Start ()
 {
     m_LastKeyTime = Time.time
     for (int i = 0; i < m_Codes.Count; i++)
     {
         m_Codes [i] = m_Codes [i].ToLower ();
     }
 }

 void Update ()
 {
     if (Input.anyKey)
     {
         m_LastKeyTime = Time.time;
     }
     else if (Time.time - m_LastKeyTime > m_ClearDelay)
     {
         m_Keys = "";
     }

     m_Keys += Input.inputString.ToLower ();

     if (m_Codes.Contains (m_Keys))
     {
         string message = "On" + char.ToUpper (m_Keys [0]) + m_Keys.Substring (1) + "Code";

         if (m_Receiver == null)
         {
             SendMessage (message);
         }
         else
         {
             m_Receiver.SendMessage (message);
         }
         m_Keys = "";
     }
 }

 void OnTestCode ()
 // Handle codes in scripts on the target GameObject (or the current one if
 // no target is set) by implementing functions named On{Name}Code - note
 // that the codes are set to be all-lowercase except for the first letter.
 {
     Debug.Log ("We caught a code!");
 }

}

Comment
Add comment · Show 5 · 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 davebuchhofer · Dec 21, 2009 at 07:33 PM 0
Share

Interesting, useful to centralize a set of hotkeys also!

avatar image fireDude67 · Dec 24, 2010 at 06:30 AM 0
Share

Nice script, possible applications in validation fields

avatar image Al-Anselmo · Jan 03, 2011 at 02:16 AM 0
Share

Does it work in Unity 3.X?

I did what you said, but I get this error:

ArgumentException: get_time can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function. CheatCodeListener..ctor ()

avatar image AngryAnt ♦♦ · Jan 04, 2011 at 02:59 PM 1
Share

It wasn't. Now it is.

avatar image theorbitgames · Jul 21, 2014 at 04:20 AM 0
Share

Do you have a Javascript version?

avatar image
0

Answer by $$anonymous$$ · May 01, 2012 at 04:58 PM

Here : unitynoobs you can download a small project with this answer!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Update - Delaying until next frame? 0 Answers

Mecanim character wobbles when changing camera relative direction. 0 Answers

how to integrate steering wheel controller for ps3 in unity?? 0 Answers

how to use gui buttons as input get axis for mobile? 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