Godot Version
4.2.2
Question
I have a 2d side scroller with split screen local multiplayer. I added a forest level, and setup ParallaxBackground and two ParallaxLayers to add depth. All is working as expected, but only in the first of four camera panels. The other three panels show no backgrounds, only the “main level” graphics.
Below is my split screen node tree and attached script. Happy to share anything else to help arrive at an answer.
extends Node
@export var viewports : Array[SubViewport]
@export var cameras : Array[Camera2D]
@export var nametags : Array[RichTextLabel]
@export var current_level_scene_path : String
var level_parent : SubViewport
var level : GameLevel
func _ready():
level_parent = get_node("VBoxContainer/HBoxContainer/SubViewportContainer/SubViewport")
load_level(current_level_scene_path)
func remove_current_level():
# Remove the current level
level_parent.remove_child(level)
level.call_deferred("free")
func load_level(scene_path):
var level_resource = load(scene_path)
level = level_resource.instantiate()
level_parent.add_child(level)
# load the signals that change levels
level.load_forest.connect(load_forest)
level.load_house.connect(load_house)
for i in range(0,4):
viewports[i].world_2d = viewports[0].world_2d
viewports[i].size.x = get_window().size.x/2.0
viewports[i].size.y = get_window().size.y/2.0
nametags[i].text = level.players[i].PlayerName
var remote_transform = RemoteTransform2D.new()
remote_transform.remote_path = cameras[i].get_path()
level.players[i].add_child(remote_transform)
func load_forest():
remove_current_level()
load_level("res://Scenes/forest.tscn")
func load_house():
remove_current_level()
load_level("res://Scenes/level1_house.tscn")
There is a note in the docs regarding viewports for ParallaxBackground
: ParallaxBackground — Godot Engine (stable) documentation in English
Also, Godot currently doesn’t officially support having things show in one viewport but not another. There is a proposal to make this easier though.
opened 12:38AM - 23 Jun 24 UTC
topic:rendering
topic:2d
### Describe the project you are working on
I'm making an FAQ for the communi… ty of commonly asked questions and problems users have with working with parallax in Godot.
### Describe the problem or limitation you are having in your project
When I started making a demo to show how to use parallax within a split screen game. I realized there's no reasonable way to have a canvas item show in one viewport but not another.
The current steps to achieve this is:
1. share the same `World2D` between the two viewports.
2. set the first Viewport's canvas cull mask to layer 1.
3. set the second Viewport's canvas cull mask to layer 2.
4. set the visibility layer of every canvas item and every descendant's that you want to appear only in the first viewport to layer 1.
5. set the visibility layer of every canvas item and every descendant's that you want to appear only in the second viewport to layer 2.
6. set the visibility layer of every canvas item and every descendant's that you want to appear only in both viewports to 1 and 2.
### Describe the feature / enhancement and how it helps to overcome the problem or limitation
Add an `inherit_visibility_layer` property to canvas items. `false` will use the current visibility layer and `true` will use its parent's visibility layer. This means instead of having to set each canvas item and all its descendants, you just set a common parent to `false` and set the visibility_layer there.
### Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Creating a draft PR we can discuss. Feel free to give input. It seems like the ideal behavior would be to have `true` as the default, but that would be a compatibility breakage. We could start by having it `false` by default and add a project setting that could be removed when we're okay introducing the compatibility breakage.
### If this enhancement will not be used often, can it be worked around with a few lines of script?
It's absolutely doable right now, but requires explicitly setting the visibility_layer of almost every canvas item in a game, which is a rough ask.
### Is there a reason why this should be core and not an add-on in the asset library?
CanvasItem is part of core.
EDIT: Updated to match PR
“Note: Each ParallaxBackground is drawn on one specific Viewport and cannot be shared between multiple Viewports, see CanvasLayer.custom_viewport. When using multiple Viewports, for example in a split-screen game, you need create an individual ParallaxBackground for each Viewport you want it to be drawn on.”
GOLD MINE. Thank you for sharing this from the docs.
1 Like
system
Closed
August 15, 2024, 12:10am
4
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.