@Danicron said:
@TwistedTwigleg said:
@Danicron said:
... I do have a global script that autoloads in the beginning perhaps i should place the variable in there and call and update it as a when i need it? i also want to be able to display how much gold the players have in separate screens
I would suggest use a singleton/autoloaded script.
Using a singleton/autoloaded script, you can just get the global (I do not remember the syntax) and then you can just access and use the variable like normal in every script that needs it.
As for displaying how much gold the player have, assuming the gold variable is a float or int, you can display it like this: get_node(“label_or_label_like_node_here”).text = “Coins: “ + str(global_script.coins)
.
Hopefully this helps :smile:
So in the case of multiple characters would i make an autoloaded script just for gold that has seperate money variables for each NPC and they can be stored and called from there and using the command you just gave me but edited for each variable/character?
Yeah, you could do it that way.
Depending on how many NPC characters you have, it may be better to use a dictionary instead. Something like this:
var character_coins = {
"NPC_1": 100,
"NPC_2": 200,
"NPC_5": 4,
}
Then to access and change the coins, you just use code like this: global_script.character_coins["NPC_1"] = 42
Though depending on how many NPC/characters you plan on having, this could get hard to manage. If you wanted to get around this a bit (and don't have a save/load system and/or save and load the dictionary), you could do something like this for in the _ready
function for the NPC characters:
export (int) var NPC_coins = 200;
export (string) var NPC_name = "Fred";
var global_script = null;
func _ready():
global_script = get_node("global_script_path_here");
if (global_script.character_coins.has(NPC_name) == false):
global_script.character_coins[NPC_name] = NPC_coins;
else:
NPC_coins = global_script.character_coins[NPC_name];
Then you can set the coin amount and name of the NPC in the editor for each NPC. Then when each NPC is added to a scene, it checks to see if that NPC has defined how many coins it has in character_coins
. If the NPC's coins are not in character_coins
, then it adds the amount of coins the NPC has to character_coins
, while if the NPC coins are in character_coins
, then it gets sets NPC_coins
to however many coins are in character_coins
for that NPC.
Though you will need to store however many coins the NPC has before changing scenes. There are several ways to do this (like adding all NPC characters to a group, and then iterating over them and storing their coins before changing scenes). It all really depends on how you want to go about it.