Skip to content
Advertisement

Ursina update function within class

In a project I am working on, I have multiple classes in which I wish to each have an update function when an object is created. How do I get these update functions to run every frame? i.e for this example code, how do I get both class a and b to run update functions?

from ursina import *
app = Ursina()

class a:
    def update(self):
        print("a updating")

class b:
    def update(self):
        print("b updating")

a = a()
b = b()

app.run()

At the moment neither function gets run. Any help is appreciated

Advertisement

Answer

For update to run automatically it has to be on an Entity. Simply inherit Entity like this:

class A(Entity):
    def update(self):
        print('update')

The way this works is that when you instantiate an Entity, ursina will add it to a list (scene.entities). Every frame ursina will loop through that list and call entity.update() if that entity has a method named update

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement