Store Variable in Dictionary??

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

For my game, I have functionality that whenever I want to instance something through the dialogue I write curly brackets at the beginning of the dialogue in the format of:

var dialogue = ["{scene_to_instance,argruments(including the variable_to_assign)}"]

The variable to assign to after the instance has been deleted is passed as a string and I was trying to map the string to a variable using a dictionary:

var variable_dic = {"global.dogName" : global.dogName}

But the problem is that global.dogName is passed as the value of global.dogName rather than the actual variable, so instead when I try to assign a value to that variable, it assigns the value to “global.dogName” in the dictionary.

This is a little confusing because it’s kind of hard to explain but basically I want to be able to assign a value to a variable stored in a dictionary but I don’t know how I’d go about doing that.

I’m having a hard tim figureing out what your poblem is;
In your example

var variable_dic = {“global.dogName” : global.dogName}

varible_dic[“global.dogName”] should contain the value of the varible global.dogName, does it not?

The other example:

var dialogue = ["{scene_to_instance,argruments(including the variable_to_assign)}"]

Would be a string (because it’s in quotes)

guppy42 | 2018-11-05 07:29

:bust_in_silhouette: Reply From: woopdeedoo

Not sure what you’re trying to achieve, but it sounds like you want to store a reference to a variable (like a C++ pointer) in that dictionary. I don’t think you can do that in GDScript. And if the variable itself is gone with the instance where it came from, then I think all you have left is the value you passed around.

As it says in the docs:

"Built-in types are stack-allocated. They are passed as values. This means a copy is created on each assignment or when passing them as arguments to functions. The only exceptions are Arrays and Dictionaries, which are passed by reference so they are shared."

You could potentially store that variable in an array of temporary variables, and delete it when you don’t need it anymore, or pass the variable in an array, though I’m not sure if that works. I don’t recall having tried it myself. Something like function( [variable] ). My assumption is that a copy of the variable is stored in that array until the array is deleted with the function from where it came from.