3 common Ways to transfer variable between script

preview_player
Показать описание
if you have questions like
1. how to share variable between scripts ?
2. how to transfer variable between one script to another ?
3. how to transfer variable over another scene ?
then you are at right place. what ever game you create there is always a need to share or transfer variables between scripts. there are actually three very easy way to share variable between scripts.
1. by creating a global script
2. by sending variables with signals
3. by sending values using functions
there may be other ways also to share data but these are more common and simple way to share variable.
I hope this helps you in making your game.
Рекомендации по теме
Комментарии
Автор

love this type of tutorials, straight to the point, fast, and helps you understand a topic on a larger scale, keep up the awesome work!

ahmedsan
Автор

I have been looking for the awnser to this problem for 5 hours straight, thank you so much. You deserve my subscription:)

moneycoding
Автор

Incredible tutorial! I've been struggling to understand how to share information between scripts and understanding. This is a perfect, succinct, and clear breakdown. Thank you so much1

wickedlittletongue
Автор

Thank you so much for the tutorial, everyone else made things more complicated than it actually was.

zv
Автор

very appreciated! straight to the point, simple, and clear. subscribed!

potatoboundfries
Автор

This guy helped me so much with setting up a loading screen for my games. And I can do even more with it than that.

vas-poenitentiae-
Автор

A 4th way is to use find_node, for example to find the camera (the scene should have only one node with this name otherwise it will take the highest on the tree): onready var camera = get_tree().get_root().find_node("Camera", true, false).

danylbekhoucha
Автор

Thank you, this was a great explanation.

milkmadetea
Автор

omg this is what i found for hours, the signal

nonameiam
Автор

the best tutorial ever, thank you, you help me so much.

alexandersamuel
Автор

"bullets are added at runtime so you cannot connect the signal to the enemy node"
This is literally false but metaphorically true.
You can connect signals at runtime via script and there are lots of scenarios you might want to do so. If you want to have a turn queue for a turn based game, but you will dynamically create actors throughout the game, when created they will need to connect their turn_start turn_end signals to the queue. This works fine.
Where this doesn't work for your bullet example is generally there will be lots of bullets created quite quickly, with LOTS of potential things to collide with and hooking up all those signals for every bullet is just too much overhead. Having a bullet only make one call specifically with the object it collides with is the best way.

NstalgicLeaf
Автор

thanks for this one :) soon I will need some knowledge on this topic in my game

noise_dev
Автор

if i want to create a toggle variable which all the scripts can access is it best to use the global variable ? eg. toggle for a help screen to turn of and off at any time

ivanmectin
Автор

Is it possible to use option 2 or 3 if the different things are in 2 different scenes but added as a child instance into the same scene?

noob-killer
Автор

Thank you so much for this. I'm a godot beginner and this really helped me out.

keithestanol
Автор

With the third variant instead of the name you can use a group

export var hit = 1.0

func _on_Area_entered(_body):
if
_body.enemy_hit_damage(hit)

bormotunDa
Автор

how do you declare a global variable in the global script and make it change in another script. it seems like when i declare a global variable in the global script, when i change it in another script, the variable will change back to the value in the global script

ivanmectin
Автор

eu estava procurando isso a muito tempo

nnaammee-urlw
Автор

Nice tutorial! Does this work for 3D games as well? What I need is a scene switcher that loads the next level in the background while a loading screen is shown to a player.

Betegfos
Автор

Hello! I'm trying to make a game with objects that moves only in y axis in one diretion, and player in x axis, i have a question, if the player touch an object and this object has to cause that all objects change their speed, how would i solve it? With a signal emitted through this body? or by other means? All objects have their own scene and are instantiated in a main scene, their movement argument are as follow:

var speed = 270

func _process(delta):
global_position.y += speed * delta

felixcuche