Not being able to change boolean from another script

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

I’ve been trying to make this code work for the past couple of days, but i can’t pinpoint exactly why it’s not working:

extends LineEdit

var GridSize_Range = range(1,99)

var NT = 1

onready var GridGenerator_Script = load("res://Scripts/")

func _on_LineEdit_text_entered(new_text):
	if new_text.is_valid_integer():
			NT = str(new_text)
			for i in GridSize_Range:
				var i2 = str(i)
				if NT == i2:
					GridGenerator_Script.ValidGridSize == true
		GridGenerator_Script.ValidGridSize == false

The Boolean (on another script):

var ValidGridSize = false

This is supposed to make so that the boolean ends up being true or false depending on user input.

but when i test it by writing something it gives me this error:

Invalid Get Index 'ValidGridSize' (on base: 'GDScript).

Why is the code not working?

:bust_in_silhouette: Reply From: skysphr

You have to create an instance of the packed class:

onready var GridGenerator_Script = load("res://Scripts/").new()

It worked! Thanks for the answer

Megatt4 | 2021-09-15 23:00

:bust_in_silhouette: Reply From: MathieuJazz

Why are you using double equals in this situation? When you write: GridGenerator_Script.ValidGridSize == true
this simply returns a True or a False value, and you seem to do nothing with it…

To affect a value, you would simply use the equal sign (=).
GridGenerator_Script.ValidGridSize = true

But anyway, once you loaded your script, it needs to be instantiated in order to use it dynamically. When you use the load function, the object is not the script itself… It’s a packed class. So skysphr is right, you need to instantiate it with the .new() method.

Then, do not forget to change your double equals for equals, and everything should work!

PS: If you didn’t want to instantiate the script in order to use it like a global script, you should really read about Singletons. This way, the script is instantiated once (for all the game) and you still can play with its values.


I’ll make sure to read about them

Also, thanks for reminding me about the (==), i left them in there accidentally because i was testing how to make the code work, if you hadn’t pointed this out i’d probably still be scratching my head looking for a way to make this script work, so thanks a lot for the answer

Megatt4 | 2021-09-15 23:05