- Edited
xyz Can you make it work with a resource similar to this https://github.com/Hugo4IT/CheatCoder
xyz Can you make it work with a resource similar to this https://github.com/Hugo4IT/CheatCoder
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()```
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()
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 okay the white space I did that because iam on mobile
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
why do i get the feeling this blud is deving on ipad or something? gen alpha...
kuligs2 Yes you correct I am working on a game on mobile
Raptorkillz It still doesn't work
Well try to find out why it doesn't work. It's called debugging and it's an essential part of programming.
Raptorkillz 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 = ""
is this function even correct?
im not fond of lambdas because its convoluted.
i dont like this line:
s += a if not s else " " + a
Seems like if s
- the entered cheat sequence is nothing/empty then you start new sequence with " " a space at the beninging. So it will never match the cheat or i maybe wrong here?
also the last line
for k in cheats.keys(): if s.contains(k): code_entered.emit(cheats[k]); s = ""
does ;
at the end or signal.emit() means if condition fails then do - s = ""??
xyz If you wanted to know I was debugging the outcome by adding a label that said cheat successfully entered when the cheat was successful but the text didn't output anything
Raptorkillz Use a trivial test example.
print()
on your dev platform - use a different platform or set things up in a way so you can see your prints. Developing without printing is not a very wise thing to do. It just makes tracking the flow of your code needlessly more difficult.class_name CheatCodeCapture extends Node
signal cheat_code_entered(name: String)
var s: String
var cheats: Dictionary = {"ui_left ui_right" : "cheat1"}
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
print("current sequence: ", s) # debug
for k in cheats.keys(): if s.contains(k): cheat_code_entered.emit(cheats[k]); s = ""
func _ready():
cheat_code_entered.connect(on_cheat)
func on_cheat(name):
print(name) # debug
kuligs2 Here's an "educational" version for easier understanding:
class_name CheatCodeCapture extends Node
signal cheat_code_entered(name: String)
var action_sequence: String
var cheatcodes: Dictionary = {"ui_left ui_right" : "cheat1"}
func _input(event):
# iterate through actions defined in input map
for action in InputMap.get_actions():
if event.is_action_pressed(action):
# accumulate actions
if not action_sequence.is_empty():
action_sequence += " " # insert space between actions
action_sequence += action
# check if the current action sequence is present in any of cheatcodes
for code in cheatcodes.keys():
if action_sequence.contains(code):
# action sequence found, emit the signal and clear the accumulated sequence
cheat_code_entered.emit(cheatcodes[code])
action_sequence = ""
break
xyz I found the error
Raptorkillz I found the error
Great. Now correct it
xyz The example code you wrote fixed it