Skip to main content
Bumped by Community user
Bumped by Community user
edited tags
Link
Vaillancourt
  • 16.4k
  • 17
  • 55
  • 61
changed title to better suit the problem
Link

Why won't this button opendoes this doorvariable keep returning to its original value?

added 245 characters in body
Source Link

In my 2D platformer game, I'm trying to create a button that, when the player or a box is on top of it, opens a door of the same color. Here is the code for the button:

extends StaticBody2D

onready var is_object_on_button = false

func _on_Green_Button_Area_body_entered(body):
    is_object_on_button = true
    print("button is down")

func _on_Green_Button_Area_body_exited(body):
    is_object_on_button = false
    print("button is up")

Code for the door:

extends StaticBody2D

onready var is_object_on_button = Green_Button.is_object_on_button
onready var door_collision = $CollisionPolygon2D

func _process(delta):
    if is_object_on_button:
        door_collision.set_disabled(true)
        print("unlocked")
    else:
        door_collision.set_disabled(false)
        print("locked")

The button prints the right things at the right time, so I assume the button is working properly, but the door constantly stays (and prints) locked. How do I fix this?

Edit: After a bit of experimentation, I think that the problem is that is_object_on_button keeps returning to whatever value its assigned at the start: If I set it to true at the onready, it stays true. I do not know how to fix this, either.

In my 2D platformer game, I'm trying to create a button that, when the player or a box is on top of it, opens a door of the same color. Here is the code for the button:

extends StaticBody2D

onready var is_object_on_button = false

func _on_Green_Button_Area_body_entered(body):
    is_object_on_button = true
    print("button is down")

func _on_Green_Button_Area_body_exited(body):
    is_object_on_button = false
    print("button is up")

Code for the door:

extends StaticBody2D

onready var is_object_on_button = Green_Button.is_object_on_button
onready var door_collision = $CollisionPolygon2D

func _process(delta):
    if is_object_on_button:
        door_collision.set_disabled(true)
        print("unlocked")
    else:
        door_collision.set_disabled(false)
        print("locked")

The button prints the right things at the right time, so I assume the button is working properly, but the door constantly stays (and prints) locked. How do I fix this?

In my 2D platformer game, I'm trying to create a button that, when the player or a box is on top of it, opens a door of the same color. Here is the code for the button:

extends StaticBody2D

onready var is_object_on_button = false

func _on_Green_Button_Area_body_entered(body):
    is_object_on_button = true
    print("button is down")

func _on_Green_Button_Area_body_exited(body):
    is_object_on_button = false
    print("button is up")

Code for the door:

extends StaticBody2D

onready var is_object_on_button = Green_Button.is_object_on_button
onready var door_collision = $CollisionPolygon2D

func _process(delta):
    if is_object_on_button:
        door_collision.set_disabled(true)
        print("unlocked")
    else:
        door_collision.set_disabled(false)
        print("locked")

The button prints the right things at the right time, so I assume the button is working properly, but the door constantly stays (and prints) locked. How do I fix this?

Edit: After a bit of experimentation, I think that the problem is that is_object_on_button keeps returning to whatever value its assigned at the start: If I set it to true at the onready, it stays true. I do not know how to fix this, either.

Source Link
Loading