How can I apply a differing z_index to a Sprite node based on which Viewport it is displayed in?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tla

I have the following setup and am trying to work out a way to have each Viewport display a different z_index for each sprite. Is there an easy way to do this?

I have tried using YSort but it applies the sorting to both Viewports.

To clarify what I am trying to do is to get the left Viewport to display the blue Sprite drawn on top of the red Sprite and the right Viewport to display the red Sprite on top of the blue Sprite.

The use case for this is where each player would see their own character sprite in the foreground in a split-screen side-scrolling game where the players are collision exceptions with each other.

:bust_in_silhouette: Reply From: benana

A brute-force solution I can think of is:

  1. In Viewport 2, add another Sprite that’s FakePlayer2
  2. Viewport 2’s FakePlayer2 tracks the position of the real player 2 in World.
  3. Set FakePlayer2’s z-index to a high value.

This essentially achieves your goal.