How to enable/disable a script by pressing a key

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Taabta

Hello there, first of all, I’d like to state that I’m still a beginner.
My question is can i assign a key that when is pressed i can disable a script of a character, and another key that enables it again? (Im having two characters that im trying to give them the same movement keys, but my question is can i make a “switch” key that can enable them for one and disable them for the other and vise versa?) Please tell me how if its possible. Thank you

:bust_in_silhouette: Reply From: p7f

No such thing as disable script i thing, but you can set a boolean variable, lets say “active” in each character, that sets to true when a key is pressed, and to false when the other is pressed (or just toogle them with the same key).

So, when you are processing the input, you can also check if “active” is true; if so, you process the intup. If not, you do nothing.

If you share your character’s scripts, i can help you further.

can you please tell me what to write exactly?

here is my code:

func _physics_process(delta):
motion.y += GRAVITY






if Input.is_key_pressed(KEY_D):
	motion.x = min(motion.x + ACCELeRATION, MAX_SPEED)
	$Camera2D.make_current()
elif Input.is_key_pressed(KEY_A):
	motion.x = max(motion.x-ACCELeRATION, -MAX_SPEED)
	$Camera2D.make_current()
else:
	friction = true
if is_on_floor():
	if friction == true:
		motion.x = lerp(motion.x, 0, 0.2)
	if Input.is_key_pressed(KEY_W):
		motion.y = JUMP_HEIGHT
		$Camera2D.make_current()
else:
	if friction == true:
		motion.x = lerp(motion.x, 0, 0.15)
		if Input.is_key_pressed(KEY_1):
			active == false








motion = move_and_slide(motion, UP)

Taabta | 2018-12-13 21:43

var active = false;
func _input(event):
    if event.is_action_pressed("switch_player"):
        active = !active #toogle active state, other character should do the same but start with active = true

func _physics_process(delta): 
    motion.y += GRAVITY
    if Input.is_key_pressed(KEY_D) && active:
        motion.x = min(motion.x + ACCELeRATION, MAX_SPEED)
        $Camera2D.make_current() 
    elif Input.is_key_pressed(KEY_A) && active:
        motion.x = max(motion.x-ACCELeRATION, -MAX_SPEED)
       $Camera2D.make_current()
    else:
        friction = true
    if is_on_floor():
        if friction == true:
            motion.x = lerp(motion.x, 0, 0.2)
        if Input.is_key_pressed(KEY_W) && active:
            motion.y = JUMP_HEIGHT
            $Camera2D.make_current()
    else:
       if friction == true:
            motion.x = lerp(motion.x, 0, 0.15)

    motion = move_and_slide(motion, UP)

Something like that. Could you format your code correctly? because perhaps i missunderstood the code cause the strange indentation. Of course you MUST define the “switch_player” action from the editor before running this!

p7f | 2018-12-13 21:57

Thanks bro, you are a legend!!!

i have two more questions if you don’t mind. the first one is can i assign any key for defining the “switch_player” above like KEY_1 maybe.

The second question is can you tell me how can i write a code for specific collision of the character with an object ex. a bouncer.

Thanks again!!

Taabta | 2018-12-13 22:49

Hi, im glad to help!

  1. of course, you can define any key. Just go to project settings, then to actions tab, and there create a new action with the name you want. In the list, at the right of the recently created action you click and lets you choose any key for that action.

  2. you need to tell me what class of bodies are the ones involved in the collision. Are they kinematic rigid? Just areas? However, i highly recommend you starting a new question for this, so others looking for the same can see it, and also other people with more knowledge than me can help you. Keep just one issue per question!

Also, if you consider my answer to be correct, pls select it as correct answer, so others can see it solved your problem!

p7f | 2018-12-13 23:04