help with key events

hey can someone give me a tut on how keyevets work ?

key events are covered in both, the manual and several samples which come with your panda installation. for example the roamingRalp sample, solar system, asteroid, infinite tunnel, etc. they all use keyboard input.
have a look at them :slight_smile:

see they use

self.accept("escape", sys.exit)

but i’m new to python and panda 3d and everything and i just trying to get my model to move but is there a tut that tells me how it works ? or is it just the samlpes ?

First learn python, then panda.

What you did not understand? There are lots of samples showing you how it works. In short:

Node.accept(‘key’, function)

1.) Attach the .accept() to a node(better not to the render or base nodes). It’s useful later. Example: w.accept(‘key’, func)

2.) Write down the key you need to be attached to a function. It’s not case sensitive! you can use -pressed and -released. Example: w.accept(‘p-pressed’, func)

3.) Put the function that will be called after pressing the key. Example: w.accept(‘p-pressed’, move)

4.) If the function does not exist, create it! Example:

def move():
     teapot.setPos(teapot.getX()+1, teapot.getY()+2, 0)

–Your Mešča with love 8)

An addition to that: in MentalDisaster’s post, ‘w’ and ‘Node’ are instances of the DirectObject class. Many people make their class inherit from DirectObject so they can use self.accept - but this is not a necessity.
Note that ShowBase (the ‘base’ variable) is also a DirectObject so you can also use base.accept().

thanks alot guys you really helped see the way i learn things is i try to do fun things and make them work when i learned VB6 i made a turn base game for fun took me about 5 - 9 months but i learned soo much about how VB6 worked i could do just about anything in VB now that i play Angels Fall First i always wanted to make something like that but and i asked what he used ETC then i started useing 3d panda so i just have to keep trying things its the only way i understand things is to mess around and make things and thanks again !! :smiley: i will be back later with more dumb dumb things to ask

still have a problem now sure where i gone wrong heres my code

import direct.directbase.DirectStart
import sys
import math
import OS



class World(DirectObject):
  def __init__(self):
    
    self.accept('escape', sys.exit)
    self.accept('a', move_())
        
    def __load__(self):
 self.model = loader.loadModel("models/environment")
 self.model.reparentTo(render)
 self.model.setScale(1,1,1)
 self.model.setPos(-8,42,0)
 self.ship = loader.loadModel("models/box")
 self.ship.reparentTo(render)
 self.ship.setscale(15,15,1)
 self.ship.setPos(0,0,0)
     def move_(self):
    self.ship.setPos(ship.getX() + 1,0,0)
World()
run()

heres the out put

C:\game>python try1.py
  File "try1.py", line 15
    self.model = loader.loadModel("models/environment")
                                                      ^
IndentationError: unindent does not match any outer indentation level

am i doing something worng ?

In Python, indentation matters. Also I spotted some mistakes in your code. Corrected code:

class World(DirectObject):
  def __init__(self):
    self.accept('escape', sys.exit)
    self.accept('a', self.move_)
    self.__load__()
  
  def __load__(self):
    self.model = loader.loadModel("models/environment")
    self.model.reparentTo(render)
    self.model.setScale(1,1,1)
    self.model.setPos(-8,42,0)
    self.ship = loader.loadModel("models/box")
    self.ship.reparentTo(render)
    self.ship.setscale(15,15,1)
    self.ship.setPos(0,0,0)
  
  def move_(self):
    self.ship.setPos(ship.getX() + 1,0,0)

World()
run() 

oh shot i’m sooooo odumb XD !!!

ok i ran your code and it give me this error

    self.ship.setPos(ship.getX() + 1,0,0)
NameError: global name 'ship' is not defined 

Please learn Python (especially about object oriented programming) before attempting Panda.
If you did, you would have seen right away that that line should have been:

    self.ship.setPos(self.ship.getX() + 1,0,0)

ok i ran your code and it give me this error

    self.ship.setPos(ship.getX() + 1,0,0)
NameError: global name 'ship' is not defined 

i can read books all day and not learn a thing i need to just to playing around and try to make things work or i will never learn and i been reading python too soo i’m trying to make things to work ok

Python explained the problem very well. There is no “ship” in your code. There is “self.ship”. pro-rsoft wrote about it two posts above.

kingkong190

What everyone is trying to say is that what you are expericancing is NOT a panda3d problem but a basic problem in programming in PYTHON. You need to know that basics of programming and of python programming in particular before trying to use panda3d.

If you are not willing to do the basics,MANY(not ALL) people here will be unwilling to help you. The will just figure you do not want to help yourself first.

Another key part in learning python/panda is understanding the error output. In your particular case:

In my experience, this is almost always an instance where I forgot to put self. in front of a variable/function call. That’s not necessarily the only time you will see it, but as a beginner to panda/python, when you see it the first thing you look for is “should there be a self there?”. I simpathize with you in that I learn the same way, and so far knowing what the console output means has been far more helpful than knowing python (but as with everything, there are limits).

~Silver~

lol well i learning slowly but i did learn from this program how to load models and how to make them move and how to use classes :smiley: sooo happy see i learned VB6 makeing a turned base game and i learned too much in that i see everything thou VB which was the only thing i learned at 13 and used for 6 yaers now i’m trying my hand in python cuz vb you can’t make a good game in that and as i said before i paly angels fall first and i found out about this game engine soo i love it soo easy to work with :smiley: anything time to try to get this dumb box to shot and turn not sure how but not to worry i will be back to ask dumb dumb things when i get stumped

your noob~kingking190