I need help.

I just looked at the roaming ralph tutorial and tried to make a similar code to create motion for my space ship. It is not working for some reason.

here’s my code:
"
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.task.Task import Task

starstealth = loader.loadModel("/c/Documents and Settings/Albert/Desktop/Blender Files (Mac)/StarStealth.egg")
starstealth.reparentTo(render)
starstealth.setScale(.25, .25, .25)
starstealth.setPos(0,5,-.5)

dlight = PointLight(‘my dlight’)
dlnp = render.attachNewNode(dlight)
dlnp.setPos(0,5,5)
render.setLight(dlnp)

base.cam.setPos(0,5,5)
base.cam.setHpr(0,-90,0)

def init(self):

self.accept("arrow_left", self.setKey, ["left",1])
self.accept("arrow_right", self.setKey, ["right",1])
self.accept("arrow_up", self.setKey, ["forward",1])
self.accept("arrow_left-up", self.setKey, ["left",0])
self.accept("arrow_right-up", self.setKey, ["right",0])
self.accept("arrow_up-up", self.setKey, ["forward",0])

taskMgr.add(self.move,"moveTask")

def setKey(self, key, value):
self.keyMap[key] = value

def move(self, task):

elapsed = globalClock.getDt()

startpos = self.starstealth.getPos()

if (self.keyMap["left"]!=0):
    starstealth.setH(starstealth.getH() + elapsed*300)
if (self.keyMap["right"]!=0):
    starstealth.setH(starstealth.getH() - elapsed*300)
if (self.keyMap["forward"]!=0):
    starstealth.setY(starstealth, -(elapsed*25))

run()
"

I am not very proficient in python and am still contemplating using C++ which I already understand the concepts for(seeing as I have programmed C# and Java before). I would like to know what is wrong with the code that I have presented.

Thanks,

The Count

Please post your code with the Code tag, otherwise the indentation will be lost.

I saw your code has a function init. I believe you have cut it from a class. You shall learn a bit more on Python class syntax. init is the constructor of the python class. Since you have not defined a class, your program will not work.

Even you call this function explicitly, the function code will not work and will die at the very beginning.

Thanks

I have just changed that aspect of the code.

What is wrong now it still won’t work.

[import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.task.Task import Task

starstealth = loader.loadModel("/c/Documents and Settings/Albert/Desktop/Blender Files (Mac)/StarStealth.egg")
starstealth.reparentTo(render)
starstealth.setScale(.25, .25, .25)
starstealth.setPos(0,5,-.5)

dlight = PointLight('my dlight')
dlnp = render.attachNewNode(dlight)
dlnp.setPos(0,5,5)
render.setLight(dlnp)

base.cam.setPos(0,5,5)
base.cam.setHpr(0,-90,0)

def accep(self):

    self.accept("arrow_left", self.setKey, ["left",1])
    self.accept("arrow_right", self.setKey, ["right",1])
    self.accept("arrow_up", self.setKey, ["forward",1])
    self.accept("arrow_left-up", self.setKey, ["left",0])
    self.accept("arrow_right-up", self.setKey, ["right",0])
    self.accept("arrow_up-up", self.setKey, ["forward",0])

    taskMgr.add(self.move,"moveTask")

def setKey(self, key, value):
        self.keyMap[key] = value
        
def move(self, task):
    
    elapsed = globalClock.getDt()
    
    startpos = self.starstealth.getPos()

    if (self.keyMap["left"]!=0):
        starstealth.setH(starstealth.getH() + elapsed*300)
    if (self.keyMap["right"]!=0):
        starstealth.setH(starstealth.getH() - elapsed*300)
    if (self.keyMap["forward"]!=0):
        starstealth.setY(starstealth, -(elapsed*25))

run()]

is it just me or did you try and make your whole code block a giant list?

first of all you need to learn Python. Start by reading some python tutorials, or download “Dive into Python”.

Several blatant errors:
-as before your code is enclosed in “[” and “]” making it a gigantic list
-you have “self” as a parameter (also known as “arguments”) for several functions however the functions are not methods bound to any classes(objects).
-again, your code uses “self.x” when it is not a attribute for any class
-you don’t have function calls that actually execute your functions “accep()” and thus in turn “setKey()” is never called, nor is the task “move()” ever started
-after calling these functions, you need to remember to end the “move()” function with “return Task.cont” if you want it to run next frame; otherwise it will only run once.

Try this: (by the way I haven’t tested to see if it works or not, the computer I’m on doesn’t have panda installed)

import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.task import Task
from direct.actor import Actor
from direct.interval.IntervalGlobal import *
import math

starstealth = loader.loadModel("models/panda")
starstealth.reparentTo(render)
starstealth.setScale(.25, .25, .25)
starstealth.setPos(0,5,-.5)

dlight = PointLight('my dlight')
dlnp = render.attachNewNode(dlight)
dlnp.setPos(0,5,5)
render.setLight(dlnp)

base.cam.setPos(0,5,5)
base.cam.setHpr(0,-90,0)

def setKey(key, value):
    keyMap[key] = value
       
def move(task):
    elapsed = globalClock.getDt()
   
    startpos = starstealth.getPos()

    if (keyMap["left"]!=0):
        starstealth.setH(starstealth.getH() + elapsed*300)
    if (keyMap["right"]!=0):
        starstealth.setH(starstealth.getH() - elapsed*300)
    if (keyMap["forward"]!=0):
        starstealth.setY(starstealth, -(elapsed*25))

    return Task.cont

def keys():

    base.accept("arrow_left", setKey, ["left",1])
    base.accept("arrow_right", setKey, ["right",1])
    base.accept("arrow_up", setKey, ["forward",1])
    base.accept("arrow_left-up", setKey, ["left",0])
    base.accept("arrow_right-up", setKey, ["right",0])
    base.accept("arrow_up-up", setKey, ["forward",0])

    taskMgr.add(move,"moveTask")

keyMap ={'forward': 0, 'right': 0, 'left': 0}
keys()
run()

Edit: and I realized you have inconsistent indents and if something doesn’t work can you please post the error message too?