Variable passing by reference

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

Hi all, I think I may just be misunderstanding object oriented principles but if you could help me I would be grateful.

I have a function that I want to pass an empty variable (meant to be an object of some sort), and have the function fill that variable with an object loaded within the function.

I’ve been told here that anything in Godot derived from Object passes by reference. I thought that that would mean I could do this:

onready var test_obj_1 = $test_obj_1

var saved 

func _ready():
	assign_saved(saved, test_obj_1)
	if saved != null:
		print("the name of 'saved' is now: " +
		print("oops, 'saved' is still null.")
func assign_saved(saved_variable : Node, test_object):
	saved_variable = test_object

But it seems to print “‘saved’ is still null”. Can anyone tell me what I’m misunderstanding?

:bust_in_silhouette: Reply From: Inces

No, only arrays, dictionaries and Object inheritants are passed as a reference. Everything else is passed via a value.
Value here was actually a reference to object, but your variable and argument were not referencing each other.
So You passed a value to an argument and assigned a new value to this argument , so it stayed there.

but that would work :

func assign_saved(saved_variable : Node, test_object):
    saved = test_object

Oof, thank you. Just a mistake on my part.

rainswell | 2023-04-24 15:40