I am in the prototyping phase of making a platformer in which the player can transform between 2 separate forms.
In this I want the camera to follow the player, since rooms will be bigger then the screen resolution.
The two forms have different collisions and sizes, so my scene tree for the player sub-scene looks like this:
The code I currently have to handle transformations and camera swapping is this:
(This code runs on the Player Node2D)
func _switch_form():
# Enable the next form
other_form.get_node("collision").disabled = false
other_form.get_node("sprites").visible = true
# Get the position and velocity of the next form the player
other_form.position = current_form.position
other_form.velocity =current_form.velocity
# Disable the current form
current_form.get_node("collision").disabled = true
current_form.get_node("sprites").visible = false
# Set the camera to focus on the new form
other_form.get_node("camera").current = true
# Swap the forms in memory soswitching between them goes correctly
var store_form = current_form
current_form = other_form
other_form = store_form
But swapping camera on the line
other_form.get_node("camera").current = true
results in a crash with the error:
Invalid set index ‘current’ (on base: ‘Camera2D’) with value of type ‘bool’.
On previous posts I saw that using “current = true” was the way you’re supposed to swap camera’s, although these were a few years old, so the way it’s done might have changed.
How do I resolve this issue?
Alternatively, can I instead make the camera a child of the player subscene in a level scene?
That way there is no need to swap cameras when a transformation happens.
I tried putting a camera as a child of player in my debug level, but the camera did not follow the subscene when it moved.
the debug scene tree looks like this right now.
I rather have it work this way, since that way I can more easily give bounds to the camera for a more dynamic game feel when I get to that later, but a solution to problem 1 is also appreciated.