#How to Open and Close a Door?

1 messages · Page 1 of 1 (latest)

strong condor
#

currently trying to have my player open a door, and have it stay open until they interact with it again to close it. Whatever I am doing currently isn't working and the door stays closed. I've clued into the fact both bits of code are running one after another and overwriting each other but I don't know how to fix it

        if Input.is_action_just_pressed("Inspect") and stall1CvO == true:
            stall1CvO = !stall1CvO
            $StallCol/Stall1ColC.disabled = true
            $StallCol/Stall1ColO.disabled = false
            $StallAnim1.play("Open")
            print("opening stall 1")
            
        if Input.is_action_just_pressed("Inspect") and stall1CvO == false:
            stall1CvO = !stall1CvO
            $StallCol/Stall1ColC.disabled = false
            $StallCol/Stall1ColO.disabled = true
            $StallAnim1.play("Close")
            print("closing stall 1")```
ancient sky
#

it might help to simplify the code a little (at least for us to read it):

if _in_range_of_door():
  if _just_buttoned() and _get_door_state("1Cv0"):
    _set_door_open("1Cv0", true)
  if _just_buttoned() and not _get_door_state("1Cv0"):
    _set_door_open("1Cv0", false)

(with a few helper functions, that is an accurate translation of your code; for instance,

func _just_buttoned(): return Input.is_action_just_pressed("Inspect")
var _door_states: Dictionary[String, bool]  # replaces your `stall1Cv0` var, but still holds the same data, just in a dictionary for purposes of my example...)
func _get_door_state(key: String):
  return _door_states[key]
#

etc etc.

#

Anyway -- take a look at that; do any simplifications jump out at you? If you simplify the code, it might make more sense to you how to fix it.

formal trout
ancient sky
#

(for instance: you're checking whether you hit the button on both ifs, but that's redundant to what you want -- to @ taymosh's point, normally you'd use an elif here or just an else honestly; but it won't work until you fix the code to not include the logic around button handling in the first place)

formal trout
#

Also disabling and enabling that collider like that is doing nothing

#

No point in that

ancient sky
formal trout
#

Oh you are right

#

Sorry

#

O pretty much looks like a C

#

my bad

ancient sky
#

anyway, structurally I think the right code looks more like:

if _is_in_range() and _just_pressed_button():
  if _door_is_open(): _close_door()
  else: _open_door()
formal trout
#

Honestly I would animate the collider aswell though

ancient sky
#

you don't have to use functions for everything! But if you think about it with functions, you can usually catch & fix state-based complexity bugs like this one 🙂

strong condor
#

I've never tried animating colliders and tbh just having stuff i disable on and off works better in my head

ancient sky
#

you can actually disable (etc) from the animation too!

formal trout
#

Its alright, Its a question of preference I guess.

formal trout
ancient sky
strong condor
#

your suggestions worked btw thank you!

formal trout