[Solved(more or less)]fullScreen() width hieight not existing


#1
def setup():
    fullScreen()
    
# class BlackHole:
#     def __init__(self):
#         self.vec = PVector(height/2,width/2)
    
#     def show(self):
#         ellipse(height/2,width/2,100,100)
        
# hole = BlackHole()
    
def draw():
    #hole.show()
    ellipse(width/2,height/2,100,100)

Why I cant do the same for my class?

Solution:

def setup():
    fullScreen()
    class BlackHole:
        def __init__(self):
            self.vec = PVector(height/2,width/2)
        
        def show(self):
            ellipse(width/2,height/2,100,100)
    global hole
    hole = BlackHole()
    

    
def draw():
    global hole
    hole.show()

#2

Because you are doing hole = BlackHole() outside of setup() or draw() - which is then using __init__(self) function to init at the same time.

The thing here is how Processing orders operations: First it runs through your code, defines classes and functions like setup() and draw() - and only after your code finishes getting parsed, only then it starts using setup() and draw().
As a result, you do hole = BlackHole() before setup() is run, but it determines width and height only in setup().

I don’t know how Python works, and I don’t know how to define a thing of BlackHole class without initializing it, so I don’t know how to help properly here. But I think you got the gist of it.


#3

I just needed to know how Processing compiles,
I tought exactly what you said, but I wasn’t sure
Thanks


#4

No problem!
Although, I don’t know if that’s a good practice in Python or not, but I think you could just leave __init__(self) definition empty, and make init2(self) function inside of your class that sets the PVector, and then call that init2(BlackHole) function in setup().