#Camera reference from player scene to main scene

1 messages · Page 1 of 1 (latest)

noble drum
#

Hi all, working on trying to get a camera reference working from. my player scene

I have ths in my player script:

@export_node_path("Camera2D") var camera: NodePath

var remote_camera: RemoteTransform2D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
    assert(camera != null, "No Camera Assigned.")
    print(camera)
    remote_camera.remote_path = camera

i keep getting an error on the last line,

Invalid assignment of property or key 'remote_path' with value of type 'NodePath' on base object of type 'Nil'

I want to have the player in the main scene be able to reference the Game Camera to allow for camera movement to follow the player. I had a RemoteTransform2D when the player was just set as a node in the main scene but since separating it into its own scene, i cant figure out how to do this reference.

I tried doing a web search but I can't get it specific enough for my use case.

Anyy and all help is much appreciated!

noble drum
#

Thanks for the reply, but that’s not quite my issue I think. 🤔 it’s late and the I’m not at my computer right now, I’ll try and sleep on it and provide more info tomorrow (read later today as it’s just after midnight for me) 🙂 Thanks again.

noble drum
#

So what im trying to do is make it so that in the editor i can assign any Camera2D to the Players' Camera property which in the player script assigns it to the players' internal remote_camera var (it may not be private but isn't directly exposed to the properties in the node inspector).

#

In my Game scene, the player has the GameCamera assigned via the editor like so:

#

Perhaps it's a timing issue when the node is instantiated?

#

vs when the GameCamera is available in the scene tree?

#

This is my node hierarchy currently:

vapid fiber
noble drum
vapid fiber
noble drum
#

remote transform 2d is via the code.

#

oh

#

-_-

vapid fiber
noble drum
#

im stupid lol

#

i need to actually just put a RemoteTransferm2D in the player scene

#

then i can reference/use that

vapid fiber
#

Correct

noble drum
#

wow, haha

#

i did have one at ome point

#

im so sorry! such a noob issue!

vapid fiber
#

I've not actually used these remote nodes before, seem like they would be useful

vapid fiber
noble drum
#

yeah they let you assign a camera to a scene

#

and when the scene that references said camera gets freed it doesn't jump the scene back to the origin

#

so say the player dies at x 1000 and y 1000

#

since the remote transform has modified the "location" of the parent scene

#

it stays in place

vapid fiber
#

gotcha!

noble drum
#

at least in theory

vapid fiber
#

I'll assume so! I just saw the error and quickly checked out what a RemoteTransform2D was 😂

noble drum
#

hmmm the camera isnt' moving the scene around though

vapid fiber
#

So according to the docs, the remote node with push its own transform to the other

#

So you are pushing the transform to the camera

#

which means your player is moving the camera around

noble drum
#

yes, which is the expected behavior but i'ts not actually working currently

vapid fiber
#

Hmmm the property remote_path expects a NodePath rather than a Node, could that be the issue?

noble drum
#

ok i think i got what's wrong

#

im assigning the node path of the camera to the remote camera node

#

but that's not what is needed

#

i neeed to assign the camera 2d directly?

vapid fiber
#

no hang on, your code is using a node path ...

noble drum
#

right. but if i set editable childeren in the instantiated scene (player within Game) i can assign the GameCamera directly

#

but assining it via code doesn't seem to the same as what the GUI does

vapid fiber
#

I don't see why that would matter

noble drum
#

me neither 🤔

vapid fiber
#

The player is just getting a node path of the external camera

#

What value does that resolve to?

noble drum
#

right then the ready function assigns that to the RmeoteCamera2D node

vapid fiber
#

(I don't use NodePaths personally)

noble drum
#

in the raw scene file (looking it as text) it just directly assigns

vapid fiber
#

Suppose that value your player has stored is a local node path, then it wouldn't work

noble drum
#

camera = NodePath("../GameCamera")

#

but that's with editable children

#

let me undo those changes then look at the raw file

vapid fiber
#

Yeah so if your player script has "GameCamera", it will fail

#

I think it needs to be "../../GameCamera" for it to work

noble drum
#

odd it does the same thing

#

as with editable children

#

so here's the current code

#
@export_node_path("Camera2D") var camera: NodePath
@onready var remote_camera = $RemoteCamera

func _ready() -> void:
    assert(!camera.is_empty(), "No Camera Assigned.")
    remote_camera.remote_path = camera
    print(remote_camera)
    var cam = get_node(remote_camera.remote_path)
    print(cam)
#

the 2 print messages: ```
RemoteCamera:<RemoteTransform2D#37262198629>
GameCamera:<Camera2D#37278975846>

vapid fiber
#

This is what the tree looks like

#

It needs to go up two levels

noble drum
vapid fiber
#

So the path should be "../../GameCamera"

noble drum
#

the player is its own scene

vapid fiber
#

that's right

noble drum
#

game scene

vapid fiber
#

The remote transform is a child of that player node

noble drum
#

the player has an export

#

so the GameCamera can be assigned

#

you know, maybe im making this too complicated?

vapid fiber
#

Not at all

#

It is quite straight forward

noble drum
#

maybe just have it reference a Camera2D directly

vapid fiber
#

You could, sure

#

But either way the node path you give it needs to still work

noble drum
#

then assign the remote path that way?

vapid fiber
#

At the very least passing the full path would work too

#

Which would resolve to "/root/Game/GameCamera"

#
remote_transform.remote_path = camera.get_path()
noble drum
#

yeah i think that was it

#

yeah that's wha ti was doing in the code right?

#

camera export was originally a NodePath

#

wait you may be right... let me check

vapid fiber
#

I am not sure how you were setting the NodePath, just saying what it would need to be

noble drum
#

i think i missed somethign essential, that maybe you even pointed out originally?

#

it was being set via the GUI in the export

#

drag and drop

#

Camera is the NodePath export

#

cam is the Camera2D export

#

cam works

vapid fiber
noble drum
#

../GameCamera for "Camera"

vapid fiber
#

Yep so that is relative to the player node

noble drum
#

same for "Cam"

vapid fiber
#

Which won't work, since you are passing that path to a child node

#

It seems that node path exports are not very useful for anything outside of their own scene

noble drum
#

ah, that's probably it

#

yeah i think the solution is to just have the GUI assign a reference to a Camera2D directly

vapid fiber
#

I would concur

noble drum
#

then in code assign that node path to the RemoteTransform2D.remote_path

vapid fiber
#

Yep. That would work and cross your fingers that particular node doesn't change heirarchy

#

Otherwise, you'd need to set it again I'd imagine

noble drum
#

so the solution is:

#
@export var camera: Camera2D
@onready var remote_camera = $RemoteCamera

func _ready() -> void:
    assert(camera != null, "No Camera Assigned.")
    remote_camera.remote_path = camera.get_path()
#

thanks for helping out @vapid fiber 🙂

vapid fiber
#

idk honestly, if you find out could you le me know?