OnscreenText and tasks

I’m having some trouble setting the OnscreenText function to update when my task runs. I’ve got the following code:

#Basic joystick under Panda using Pygame

import direct.directbase.DirectStart
from direct.gui.DirectGui import *
from direct.task import Task

class World:
    def __init__(self):
        self.title = OnscreenText(text="Joystick Example", pos=(-1.0,0.95), scale=0.07, align=TextNode.ALeft)
        base.setBackgroundColor(255,255,255)
        base.disableMouse()
        self.joystickTask = taskMgr.add(self.joystickInput, "joystickInput")
        self.joystickSetup()

    def joystickSetup(self):
        from pygame import *
        init()

        j=joystick.Joystick(0)
        j.init()
        if j.get_init() == 1: OnscreenText(text="Joystick Initialized", pos=(-1.0, 0.85), scale=0.07, align=TextNode.ALeft)
        joystickID = OnscreenText(text="Joystick ID: " + str(j.get_id()), pos=(-1.0, 0.75), scale=0.07, align=TextNode.ALeft)
        joystickName = OnscreenText(text="Joystick Name: " + str(j.get_name()), pos=(-1.0, 0.65), scale=0.07, align=TextNode.ALeft)
        joystickNumAxes = OnscreenText(text="No. of Axes: " + str(j.get_numaxes()), pos=(-1.0, 0.55), scale=0.07, align=TextNode.ALeft)
        joystickNumTrackball = OnscreenText(text="No. of Trackballs: " + str(j.get_numballs()), pos=(-1.0, 0.45), scale=0.07, align=TextNode.ALeft)
        joystickNumButton = OnscreenText(text="No. of Buttons: " + str(j.get_numbuttons()), pos=(-1.0, 0.35), scale=0.07, align=TextNode.ALeft)
        joystickHatControl = OnscreenText(text="No. of Hat Controls: " + str(j.get_numhats()), pos=(-1.0, 0.25), scale=0.07, align=TextNode.ALeft)

    def joystickInput(self, task):
        from pygame import *
        init()
        if task.time < 2.0:
            for e in event.get():
                if e.type != QUIT:
                    joystickActivity = OnscreenText(text="%s: %s" % (event.event_name(e.type), e.dict))
            return Task.cont

w=World()
run()

Whenever I run it, I get the following result:
.

How can I get the OnScreenText function to update as data from the joystick comes in??

P.S. Sorry if this is unclear. It’s hard to think through the fog of vicadin.

You are creating a new OnscreenText object with each iteration, but you aren’t removing the old one, so they’re all just stacking up and smearing together.

You’ll need to save joystickActivity in a class member, e.g. self.joystickActivity. Then when you want to change the text, just call self.joystickActivity.setText(‘new text’) instead of reassigning to a new OnscreenText object.

David