0 votes

Hi,
I'm using Godot 3.1.1 and I need to switch between two cameras when a button is pressed. I was trying to use set_current to switch between them but it doesn't seem to work. I have the following code:

extends Spatial

func _ready():
    pass

func _input(event):
    if event.is_action_pressed("switch"):
        $Camera1.set_current(not $Camera1.current)
        $Camera2.set_current(not $Camera2.current)

When I print the state before and after the set_current() function, it doesn't change so the cameras are not switching. If I instead do it like the code below, it works.

func _input(event):
if event.is_action_pressed("switch"):
    if $Camera1.is_current():
        $Camera1.clear_current(true)
    else:
        $Camera2.clear_current(true)

Did I miss something about how set_current works()? Is it not supposed to be used like this?

asked Aug 20 in Engine by danoli (15 points)

1 Answer

0 votes
Best answer

Making one camera current automatically clears the current one. The problem with your first code block is that you can't have no current camera. The first line is trying to set Camera1.current to false, without another camera to be selected.

Calling make_current() or clear_current() is the way to go.

Also, note that set_current() is not the preferred method. Set/get methods are not necessary (and don't show up in autocomplete) because direct property access is preferred:

$Camera1.current = true
answered Aug 20 by kidscancode (11,756 points)
selected Aug 20 by danoli

I get it now why it doesn't work. I'll keep the second code snippet then. Thanks.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.