Unable to set class variables

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

Hello,

I’ve created a class that serves as an 8-directional container. My intention is to be able to put objects of any type into this class, and be able to reference them by their direction:

class octal_container:
 var up
 var upright
 var right
 var downright
 var down
 var downleft
 var left
 var upleft

The issue is, in the set_up() function I’ve created for the class, already it seems my variables aren’t being ‘set’. Here’s the function within the octal_container class:

func set_up(u_, ur_, r_, dr_, d_, dl_, l_, ul_):
	self.up = u_
	self.upright = ur_
	self.right = r_
	self.downright = dr_
	self.down = d_
	self.downleft = dl_
	self.left = l_
	self.upleft = ul_
	
	if !is_instance_valid(up):
		print("warning: 'up' object is invalid on octal_container")
		print("up should be " + u_)

Here’s the call to that setup function from another class:

eye_animations = dir.octal_container.new()
eye_animations.set_up("u", "ur", "r", "dr", "d", "dl", "l", "ul")

And this gets printed to the console:

warning: ‘up’ object is invalid on octal_container
up should be u

Can anyone see what I’m doing wrong? Thank you.

P.S. Please forgive the weird formatting. I’m not sure what I did wrong there, either.

I’m not sure I understand your validity test. You seem to be passing a simple member variable to is_instance_valid(), which I wouldn’t expect to work. For example, this simple code will fail in the same way…

var a = 10
if !is_instance_valid(a):
    print("invalid!")

jgodfrey | 2022-08-10 01:34

Hah that’s why I come here, because I pass over seemingly obvious things like that all the time. Thank you.

I think my mistake was that I had recently started using is_instance_valid() as a catch-all substitute for null checks. I see now that this is not its intended use.

Just to make sure, would I be correct in saying is_instance_valid() should be used that way as long as the variable being checked was an instantiable object, and not a simple member variable?

so in other words:

var n = Node.new()
if is_instance_valid(n):
	print("this works for instantiable objects")
	
var v = 5
if v != null:
	print("this works for simple variables")

rainswell | 2022-08-11 20:41