0 votes


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 setup() function I've created for the class, already it seems my variables aren't being 'set'. Here's the function within the octalcontainer 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.

Godot version 3.3.1
in Engine by (36 points)

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):

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 isinstancevalid() 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 isinstancevalid() 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")

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.