onScreenText function

Hello,

I use the onScreenText function to display some text.

self.infoText = OnscreenText(text = 'Some text', pos = (-0.5, 0.02), scale = 0.07)

But when I try to change this text in a later stage of the game I have to destroy the object first and then make a new one. Is there another way of doing this?

I tried to do

self.infoText.text = 'Updated text'

and

self.infoText.setText = 'Updated text'

But somehow this does not works. The only thing that seems to work is to destroy it first

self.infoText.destroy()

and then create a new one using

self.infoText = OnscreenText(text = 'Updated text', pos = (-0.5, 0.02), scale = 0.07)

If I don’t destroy it I get a new object on top of the other one.
Sorry for these questions but I am new to python and panda.

Thank you

Read the manual once again.
There is mayChange option, if you set it true, the text would be editable.
And the way to change it are :
self.infoText.setText(‘new text’)
OR
self.infoText[‘text’]=‘new text’

Yup the answer was

self.infoText = OnscreenText(text = 'Mohahah', pos = (-0.5, 0.02), scale = 0.07, mayChange=1)

Missed that totally :confused:

Better memory optimization :wink:

Thank you for your fast reply

Another question how do you print something that is not a string e.g.

P = Point3(x,y,0)

Thank you

Pass its string output :
P.pPrintValues()
OR
P.str()

Hmm, I do not recommend calling the second, the official way to call it is:
str§

Ok, I am a bit lost…

What I try to do is update an image according to the position of the mouse. Somehow I am unable to get the position of the mouse. Below is my code but I can’t even place the mouse position in the textObject.

import direct.directbase.DirectStart
from pandac.PandaModules import TextNode
from pandac.PandaModules import Point2,Point3,Vec3,Vec4
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task
from direct.gui.OnscreenImage import OnscreenImage 
from direct.gui.DirectGui import *
from pandac.PandaModules import TransparencyAttrib 
import cPickle, sys

from pandac.PandaModules import TextNode

class World(DirectObject):

    def __init__(self):
        foo=1024 / 600
        self.imageObject = OnscreenImage(image = 'textures/background.png', pos = (0, 0, 0), scale=(1024.0/800.0,1,1024.0/600.0), parent=render2d)
        self.imageObject.setTransparency(TransparencyAttrib.MAlpha)
        self.gameTask = taskMgr.add(self.drawMenu, "drawMenu")
        self.menuItems = []
        for item in range(3):
            self.menuItems.append(
            OnscreenImage(image = 'textures/button.png', pos = (-0.5, 0, -0.2+(0.10342*item)),scale=(256.0/800.0,1,64.0/600.0),parent=render2d)
            )
            self.menuItems[item].setTransparency(TransparencyAttrib.MAlpha)
        self.infoText = OnscreenText(text = 'Mohahah', pos = (-0.5, 0.02), scale = 0.07, mayChange=1)
    
    def drawMenu(self, task):
        menuRowHeight = 32
        mousePosition = self.getMousePosition
        self.infoText.setText(str(mousePosition))

    def getMousePosition():
        p = None
        if base.mouseWatcherNode.hasMouse():
            x=base.mouseWatcherNode.getMouseX()
            y=base.mouseWatcherNode.getMouseY()
            p = Point3(x,y,0)
        return p 

w = World()
run()

I want to create my own buttons and not use the directbutton because that is really ugly. Next to that I want it to be dynamic so when I change a tuple or a file the menu changes… Next to that how can I detect a mouse click event?

I dont yet understand the whole thing you try to achieve.

But i’ve got 1 concern into the snippet:

  1. your drawmenu(self,task) does not return Task.cont or Task.stop for example
    (to say the task must be played again or must be stopped)

So i don’t know if this task is ever executed or executed more than once.

Could you add a print statement on the drawmenu method

like print str(x)

and check something is written into the console.

Doesn’t the taskmanager assumes to keep playing the loop until you tell it to stop? anyway I included

return Task.cont 

Documentation is poor about this(Actually it has to much unstructured info)

What I really want to know is
How can I display the coordinates I get from getMousePosition on the screen and how can I listen to the “mouseButtonClicked” event?

Thank you

  1. first check with print statement that you get something from your get position function

  2. I would recommend to read
    www.panda3d.org/manual/index.php/Event_Handlers

and then use the “mouse” link to get the list of mouse related event

(mouse1 Mouse Button 1 Pressed
mouse2 Mouse Button 2 Pressed
mouse3 Mouse Button 3 Pressed
mouse1-up Mouse Button 1 Released
mouse2-up Mouse Button 2 Released
mouse3-up Mouse Button 3 Released
wheel_up Mouse Wheel rolled upwards
wheel_down Mouse Wheel rolled downwards)

  1. I would clearly not say Panda3D documentation is unstructured.
    It’s the opposite .
    BUT
    each Chapter gives you much more information if you have read the previous chapter.
    The KEY KEY Chapter are:
    a)Scenegraph
    b) Message and Task