• Godot Help
  • How would I add a cheat code to a title screen

xyz Okay but in the code there's this line

var cheatCodes = {
"gm_up gm_up gm_down gm_down gm_up gm_up gm_up gm_up": "konami"
}

    Raptorkillz Okay but in the code there's this line

    Yeah, but there are other lines as well 🙂

    Here's an automatic class that listens to action sequences, and when it finds something, it notifies about that via a signal:

    class_name CheatCodeListener extends Node
    signal code_entered(name)
    var s: String
    var cheats  = {"ui_up ui_down ui_up" : "some cheat", "ui_left ui_right" : "some other cheat"}
    
    func _input(e):
    	for a in InputMap.get_actions().filter(func(aa): return e.is_action_pressed(aa)).slice(0,1):
    		s += a if not s else " " + a
    		for k in cheats.keys(): if s.contains(k): code_entered.emit(cheats[k]); s = ""

    Now the only thing left to do is to make a game to cheat at. Easy peasy 😃

      Raptorkillz Okay but how would I make it play a sound

      print(String.chr(0x07)). But you'll need to start a console version of Godot for this to work.

        Raptorkillz Well then you play it the same way you play the music. You already have that in the code you posted in one of your questions.

          Konami cheat in a SEGA game? Is this allowed? 😄

          • xyz replied to this.

            trizZzle Konami cheat in a SEGA game? Is this allowed? 😄

            I don't think so. But this thread already entered the psychoactive zone, and while passing the threshold it may have created an alternate reality in which this is not only allowed but is in fact - mandatory.

            xyz Okay but now when I press a button is pressed it plays the sound but I want it to play the sound when the cheat is successfully entered

            Raptorkillz That addon works with keys, not with actions.
            I wrote you the complete thing. Just connect the signal and do whatever you want (play sound etc...) in the signal handling function. If you don't understand what I'm talking about, you really need to go through the introductory tutorial I mentioned earlier.

              konami code is not patented, thankfully..

              xyz Now its not playing a sound

              
              class_name CheatCodeListener extends Node
              signal code_entered(name)
              var s: String
              var cheats  = {"gm_up gm_up gm_down gm_down gm_up gm_up gm_up gm_up" : "sonic 3 level select"}
              
              func _input(e):
              	for a in InputMap.get_actions().filter(func(aa): return e.is_action_pressed(aa)).slice(0,1):
              		s += a if not s else " " + a
              		for k in cheats.keys(): if s.contains(k): code_entered.emit(cheats[k]); s = ""
              		code_entered.connect(Play_sound)
              		
              		
              		
              		
              func Play_sound():
              		Global.debug =1
              		$"../Node2D/AudioStreamPlayer2".play()```
              • xyz replied to this.

                Raptorkillz Of course it isn't because you're connecting the signal after it's been emitted. You need to do it beforehand. Put the connect call in _ready() or connect it manually in the inspector.

                  xyz Yea I connected the signal but its not working

                  class_name CheatCodeListener extends Node
                  signal code_entered(name)
                  var s: String
                  var cheats  = {"gm_up gm_up gm_down gm_down gm_up gm_up gm_up gm_up" : "sonic 3 level select"}
                  
                  
                  
                  	
                  func _input(e):
                  	for a in InputMap.get_actions().filter(func(aa): return e.is_action_pressed(aa)).slice(0,1):
                  		s += a if not s else " " + a
                  		for k in cheats.keys(): if s.contains(k): code_entered.emit(cheats[k]); s = ""
                  		
                  		
                  		
                  		
                  		
                  
                  
                  
                  func _on_code_entered(name):
                  	match name:
                  		"sonic 3 level select":
                  			Global.debug =1
                  			$"../Node2D/AudioStreamPlayer2".play()
                  • xyz replied to this.

                    Raptorkillz Use a simpler cheat code for testing and put a print statement in the signal handler to see it it's getting called. Also make sure you don't have multiple actions mapped to the same input because the script will use only the first mapped action it finds in the InputMap.
                    Btw you have too much whitespace in your code.

                      xyz It still doesn't work

                      class_name CheatCodeListener extends Node
                      signal code_entered(name)
                      var s: String
                      var cheats  = {"gm_left gm_right" : "sonic 3 level select"}
                      
                      
                      
                      	
                      func _input(e):
                      	for a in InputMap.get_actions().filter(func(aa): return e.is_action_pressed(aa)).slice(0,1):
                      		s += a if not s else " " + a
                      		for k in cheats.keys(): if s.contains(k): code_entered.emit(cheats[k]); s = ""
                      		
                      		
                      		
                      		
                      		
                      
                      
                      
                      func _on_code_entered(name):
                      	match name:
                      		"sonic 3 level select":
                      			$"../Node2D/AudioStreamPlayer2".play()
                      			$"../title emblem/Label".text = "Cheat Successfully entered"
                      			Global.debug =1