[SOLVED] Ursina update function within class

Issue

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

Solution

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

Answered By – pokepetter

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.