Switching camera's to different playable characters

Godot Version

Version 4.0

Question

I have 2 playable characters in my scene and already have the code to switch between them.

extends Node

@onready var active = 1

func _process(delta):
	if Input.is_action_just_pressed("transfer"):
		if active != 2:
			active += 1
		else:
			active = 1

How do I also switch between the cameras? I found this code but I just get the following error code: attempt to call function ‘is_current’ in base ‘null instance’ on null instance godot 4

extends Node2D

func _input(event):
	if event.is_action_pressed("transfer"):
		if $CameraBuddy.is_current():
			$CameraBuddy.clear_current(true)	
		else:
			$CameraShadowBuddy.clear_current(true)

huh, I’m not sure I’ve seen

base ‘null instance’ on null instance

yet.

Usually the error means that it can’t find the node to call “clear_current()” on. (because $abc is just a short way of get_node("abc") )

Make sure the node “CameraBuddy” is present as child of both of your characters

Thank you for your quick response! How do I make it a child of both characters? I have 1 camera for each character.

Oh, sorry, I did not mean the same node to be a child for both players.

Just make sure that both Players have their own “CameraBuddy” node as their child.

1 Like

Yeah I checked again and they each have their camera so I really don’t get the issue. Also should I incorporate switch camera code in the code I already have in the active manager to switch playable characters? or should I separate those codes?

I would put a breakpoint at the start of that if statement in _input and check your remote tree to make sure the cameras are in the right place. It could be it’s trying to call this before the camera exists or something.

Ok, I found the issue. I had to put: $ActiveManager/Buddy/CameraBuddy and now it’s able to find the node. however, now it’s telling me: "Invalid call. nonexistent function ‘clear_current’ in base camera2D.

Call make_current() on the camera you want to be current~

1 Like

extends Node2D

func _input(event):
if event.is_action_pressed(“transfer”):
if $ActiveManager/Buddy/CameraBuddy.is_current():
$ActiveManager/ShadowBuddy/CameraShadowBuddy.make_current()
else:
$ActiveManager/Buddy/CameraBuddy.make_current

like this?

It worked!!! Thank you soooooo much!!!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.