How do I fetch data from a variable in a script from a different script

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

I’m making a card game setup using Godot. I’m learning the engine and programming as I go along. Not really making a game, rather, converting a research workshop of ours that uses cards into a digital variant. There are 17 cards in a dictionary with paths to texture, name, etc which I have shuffled, with 2 scripts one on root node and one for drag/drop.

The program is simple, you pick cards form a deck and place them in shuffled placeholders like solitaire. The way I approached it was to make TextureRect nodes as placeholders on the screen then a basic drag and drop script attached to them that changes the resource path. Problem I’m facing is once you drag and drop, the texture is duplicated (used the basic drag/drop script from Godot assets). What should happen is after dropping its removed from the previous placeholder and if it took another cards place that card is sent to the ‘deck’. This is the drag script so far:

extends TextureRect

var GLOBAL = preload("res://script/").new()    
var default = "res://gfx/Empty.png"

func get_drag_data(_pos):     # make a preview of card while dragging
	var drag =
	drag.texture = texture
	drag.rect_scale = Vector2(0.5, 0.5)
	if drag.texture.resource_path != default:
		return texture

func can_drop_data(_pos, data):
	return true
func drop_data(_pos, data):
	texture = data

I imagine it as a check_table() function in the main script, which compares the textures in the placeholders to an array item_cards[] dynamically populated for the current shuffled cards. Function checks for duplicates after dragging, if any removes them, then if a card was replaced while dragging that card is sent back to deck to be dragged out again later. I can’t really proceed into making check_table() because it won’t let me reference item_cards[]. Did this for a test:

func check_table():

The function is being called fine, if I put in a print("something") in the function that works perfectly. But I can’t reference any variables outside of the function this way. I know item_cards[] is not empty but I get this error every time:

Invalid get index '0' (on base: 'Array').

Definitely doing something wrong here. Maybe my whole using textures is the wrong approach? I’ve gotten this far though lol

Any help is much appreciated, thank you.

Hello, i’m really new so I can’t help you in depth. However, if you would put your data in a singleton (or autoload or global… whatever you prefer to call it). You can access it from anywhere. Put the data you want to access anywhere, in a script. Save it. Then go to project->projectsettings->autoloads. Then add that script. Make sure its enables. Then you can access it.

After that, to access it all you have to do is
(NameofScriptHere) . (NameofDataorFunction).
for example: If I save a variable “Player_position” in the script “Player”. Autoload it. I can access it anywhere by just typing Player.Player_position in my script.

Link to the API:
Singletons (AutoLoad) — Godot Engine (3.2) documentation in English

Aireek | 2020-04-14 08:39