- Edited
Hi!
This is half question and half example of how I implemented multiple cameras/SubViewports since I had a hard time finding an example I could wrap my head around when I first started looking for solutions. Most likely though I failed miserably with my search keywords, heh.
So to my question:
Is this a good way of implementing multiple cameras to render onto the main viewport?
I feel like making a Node3D (player character) a decendant of a canvasItem (in this case the subViewportContainer) isn't the best approach. Will it create problems for me down the road? Is there a way of keeping the camera a child of the player character while still having it render to the subViewport without the player being a decendant of that subViewportContainer? I recently started out with Godot so my knowledge of these things is very limited.
Player CreatePlayer(string name, out SubViewportContainer container)
{
// This helper method creates a SubViewportContainer and makes it a child of this playerManager(of type: Container)
container = Helpers.CreateContainer<SubViewportContainer>(this, "container_" + name);
container.Stretch = true;
SubViewport viewport = new SubViewport();
viewport.Name = "SubViewport";
viewport.HandleInputLocally = false;
container.AddChild(viewport);
Player player = new Player(engine, name, viewport);
player.PlayerContainer = container;
return player;
}
// This is the code inside the Player constructor that creates the camera
/*
world = engine.GetWorld3D();
this.viewport = viewport;
viewport.World3D = world;
// Make this player a child of the subViewport
viewport.AddChild(this);
camera = new Camera3D();
camera.Name = "Camera3D";
// Make the camera a child of this player
AddChild(camera);
*/
I wanted to create an easy way of instancing multiple players from code for split-screen/PiP and rn I'm instancing two players and using player2 as a picture-in-picture subViewport so I can get a birdseye view of the scene.