Having Trouble Switching Cameras Using The Same Hotkey

Godot Version

v4.2.2.stable.official

Question

I am trying trouble switching cameras using ‘Ctrl+k.’ I added this into the Input Map under the Project Settings.

Here is my code:

extends Node3D

Called when the node enters the scene tree for the first time.

func _ready():
pass # Replace with function body.

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta):
if Input.is_action_just_pressed(“cam_switch”):
$TopCam.set_current(true)

#if Input.is_action_pressed("cam_switch"): 
	#$PlayerCam.set_current(true)

You could insert it into the _input function, but I’m not sure why your current code wouldn’t work at the moment. Does it give you an error? is $TopCam already the current camera?

func _input(event: InputEvent) -> void:
    if event.is_action_pressed("cam_switch"):
        $TopCam.current = true

The problem was not that there was an error, it was that I was not able to switch between the Two Cameras using only One Hotkey.

This is my updated code so far.

extends Node3D


# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _input(event: InputEvent) -> void:
	if event.is_action_pressed("cam_switch"):
		$TopCam.current = true

Ok, is it the swap part that eludes you?

We could use an if statement to detect which camera is active, or some boolean logic

if event.is_action_pressed("cam_switch"):
	var was_top_cam: bool = $TopCam.current
	$TopCam.current = not was_top_cam
	$PlayerCam.current = was_top_cam

Yes. that is exactly what I’m getting at. So far when I updated my code to what you just posted, it just displayed.

Invalid set index 'current' (on base: 'null instance') with value of type 'bool'.

are PlayerCam and TopCam valid children of the script?

I would assume so. Both are parented to the main Node3D node ‘World.’

Spelled the same? any reason either of them would be deleted in-game?

Spelled the same and no, none were deleted from game. PlayerCam is from a separate scene. TopCam is from the main Node3D.

Ah PlayerCam must be direct child, not a grand child. Or update the $cameraParent/PlayerCam path

Have you tried making the camera have a unique name with % ? Then it doesn’t matter where the camera is in the scene

Yes. That did not work. Ignoring the Switch between cams from One Key for a sec. Let alone, I can not seem to make a switch with using two.

func _process(delta):
	if Input.is_action_just_pressed("cam_switch01"):
		$TopCam.set_current(true)
	if Input.is_action_just_pressed("cam_switch02"):
		$PlayerCam.set_current(true)

Attempt to call function ‘set_current’ in base ‘null instance’ on a null instance.

You’ll run into that problem because it needs to know exactly where $PlayerCam is, it needs to know every parent of PlayerCam

2024-07-10-195742_271x276_scrot

in this screenshot I would have to use path $Players/PlayerShooter/Camera3D to get the camera.

Screenshot from 2024-07-10 20-02-08

This is what I have so far
NOTE: The Player is in a separate scene

Awesome, so you will need $Player/Head/PlayerCam, or more if the Player is the child of anything else, in my example I had a Players Marker3D node to contain them

Have you tried setting the one that’s not your current camera’s .set_current() to false? If I could see your entire scene tree that might help me a little bit.

I finally got it figured out. This being my final code, thank you.

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("cam_switch01"):
		var was_top_cam: bool = $TopCam.current
		$TopCam.current = not was_top_cam
		$Player/Head/PlayerCam.current = was_top_cam
2 Likes