finish - dictionary-ifying this?

http://www.pasteall.org/70757/python

I need help lines 120 on :smiley:

import bge

import copy
def set(cont,Next):
    own=cont.owner
    target = own.scene.objects[Next[1]]
    prop = Next[2]
    target[prop]=Next[3]
    own['actStrip'].pop(0)


def toggle(cont,Next):
    own=cont.owner
    target = own.scene.objects[Next[1]]
    prop = Next[2]
    if target[prop]==False:
        target[prop]=True
    else:
        target[prop]=False
    own['actStrip'].pop(0)
    
    
def act(cont,Next):
    own=cont.owner
    Time = Next[2]
    if Time<Next[3]:
                    #print('Playing')
        own.parent['GFX'].playAction(Next[1],Next[2],Next[2],layer=6, priority=1, blendin=0, play_mode=1, layer_weight=0.0, ipo_flags=0, speed=0.0, blend_mode=1)                 
        Next[2]+=1
    else:
        own.parent['GFX'].stopAction(1)
        own['actStrip'].pop(0)    
           
           
           
def walk(cont,Next):
    own = cont.owner
    Mark=own.scene.objects[Next[1]]
    Vect2 = own.parent.getVectTo(Mark)
    if len(Next)<3:
        
        
        Next.append(int(Vect2[0]*(100-own.parent['MoveRateWalk'])))
    else:
        Time = Next[2]
        if Time>=1:
            own.parent.alignAxisToVect(Vect2[1],1,.1)
            own.parent.alignAxisToVect([0,0,1],2,1)
            own.parent.applyMovement(Vect2[1]*(Vect2[0]/Time),0)
            Next[2]-=1   
        if Time==0:
            own.parent.worldPosition=Mark.worldPosition
            Diff = own.parent.worldOrientation.col[1]-Mark.worldOrientation.col[1]
            own['Read2']=Diff.magnitude
            if Diff.magnitude>.05:
                own.parent.alignAxisToVect(Mark.worldOrientation.col[1],1,.1)
                own.parent.alignAxisToVect([0,0,1],2,1)
                
            else:
                own.parent.worldOrientation=Mark.worldOrientation
                own['actStrip'].pop(0)
            
            
            
def run(cont,Next):
    own=cont.owner
    Mark=own.scene.objects[Next[1]]
    Vect2 = own.parent.getVectTo(Mark)
    if len(Next)<3:
        
        
        Next.append(int(Vect2[0]*(100-own.parent['MoveRateRun'])))
    else:
        Time = Next[2]
        if Time>=1:
            own.parent.alignAxisToVect(Vect2[1],1,.1)
            own.parent.alignAxisToVect([0,0,1],2,1)
            own.parent.applyMovement(Vect2[1]*(Vect2[0]/Time),0)
            Next[2]-=1   
        if Time==0:
            own.parent.worldPosition=Mark.worldPosition
            Diff = own.worldOrientation.col[1]-Mark.worldOrientation.col[1]
            own['Read2']=Diff.magnitude
            if Diff.magnitude>.05:
                own.parent.alignAxisToVect(Mark.worldOrientation.col[1],1,.1)
                own.parent.alignAxisToVect([0,0,1],2,1)
                
            else:
                own.parent.worldOrientation=Mark.worldOrientation
                own['actStrip'].pop(0)












#______________________________________________________________________#
########################################################################


def main():
    cont = bge.logic.getCurrentController()
    own = cont.owner


    
    if 'GFX' not in own.parent:
        for child in own.parent.children:
            if 'Armature' in child:
                own.parent['GFX']=child
                print('FoundGFX')
    Collision = cont.sensors['Collision']
    X = cont.sensors['KeyX']
    
    if 'actStrip' not in own:
        if Collision.positive and X.positive:
            own['actStrip']=copy.deepcopy(Collision.hitObject['ActStrip'])
            
            own['Active']=True
    
    else: 
           
        own['Read']=str(own['actStrip'])
        if len(own['actStrip'])>=1:
            Next = own['actStrip'][0]
            
            
            
            
            if Next[0]=='Set':
                set(cont,Next)
                
                
                
            
            
                
            if Next[0]=='Toggle':
                toggle(cont,Next)
                    
                    
                    
            #Run to mark       
            if Next[0]=='Run':
                run(cont,Next)
                
            #walk to mark                
            if Next[0]=='Walk':
                walk(cont,Next)
            
            #Act                    
            elif Next[0]=="Act":
                act(cont,Next)
        
        else:
            print('RemActStip')
            del own['actStrip']
            own['Active']=False            
                    
main()



I need some help wrapping my head around calling a function with a dictionary and using arguments **

ok I made a method using eval…

import bge

import copy
def set(cont,Next):
    own=cont.owner
    target = own.scene.objects[Next[1]]
    prop = Next[2]
    target[prop]=Next[3]
    own['actStrip'].pop(0)


def toggle(cont,Next):
    own=cont.owner
    target = own.scene.objects[Next[1]]
    prop = Next[2]
    if target[prop]==False:
        target[prop]=True
    else:
        target[prop]=False
    own['actStrip'].pop(0)
    
    
def act(cont,Next):
    own=cont.owner
    Time = Next[2]
    if Time<Next[3]:
                    #print('Playing')
        own.parent['GFX'].playAction(Next[1],Next[2],Next[2],layer=6, priority=1, blendin=0, play_mode=1, layer_weight=0.0, ipo_flags=0, speed=0.0, blend_mode=1)                 
        Next[2]+=1
    else:
        own.parent['GFX'].stopAction(1)
        own['actStrip'].pop(0)    
           
           
           
def walk(cont,Next):
    own = cont.owner
    Mark=own.scene.objects[Next[1]]
    Vect2 = own.parent.getVectTo(Mark)
    if len(Next)<3:
        
        
        Next.append(int(Vect2[0]*(100-own.parent['MoveRateWalk'])))
    else:
        Time = Next[2]
        if Time>=1:
            own.parent.alignAxisToVect(Vect2[1],1,.1)
            own.parent.alignAxisToVect([0,0,1],2,1)
            own.parent.applyMovement(Vect2[1]*(Vect2[0]/Time),0)
            Next[2]-=1   
        if Time==0:
            own.parent.worldPosition=Mark.worldPosition
            Diff = own.parent.worldOrientation.col[1]-Mark.worldOrientation.col[1]
            own['Read2']=Diff.magnitude
            if Diff.magnitude>.05:
                own.parent.alignAxisToVect(Mark.worldOrientation.col[1],1,.1)
                own.parent.alignAxisToVect([0,0,1],2,1)
                
            else:
                own.parent.worldOrientation=Mark.worldOrientation
                own['actStrip'].pop(0)
            
            
            
def run(cont,Next):
    own=cont.owner
    Mark=own.scene.objects[Next[1]]
    Vect2 = own.parent.getVectTo(Mark)
    if len(Next)<3:
        
        
        Next.append(int(Vect2[0]*(100-own.parent['MoveRateRun'])))
    else:
        Time = Next[2]
        if Time>=1:
            own.parent.alignAxisToVect(Vect2[1],1,.1)
            own.parent.alignAxisToVect([0,0,1],2,1)
            own.parent.applyMovement(Vect2[1]*(Vect2[0]/Time),0)
            Next[2]-=1   
        if Time==0:
            own.parent.worldPosition=Mark.worldPosition
            Diff = own.worldOrientation.col[1]-Mark.worldOrientation.col[1]
            own['Read2']=Diff.magnitude
            if Diff.magnitude>.05:
                own.parent.alignAxisToVect(Mark.worldOrientation.col[1],1,.1)
                own.parent.alignAxisToVect([0,0,1],2,1)
                
            else:
                own.parent.worldOrientation=Mark.worldOrientation
                own['actStrip'].pop(0)












#______________________________________________________________________#
########################################################################


def main():
    cont = bge.logic.getCurrentController()
    own = cont.owner


    
    if 'GFX' not in own.parent:
        for child in own.parent.children:
            if 'Armature' in child:
                own.parent['GFX']=child
                print('FoundGFX')
    Collision = cont.sensors['Collision']
    X = cont.sensors['KeyX']
    
    if 'actStrip' not in own:
        if Collision.positive and X.positive:
            own['actStrip']=copy.deepcopy(Collision.hitObject['ActStrip'])
            
            own['Active']=True
    
    else: 
           
        own['Read']=str(own['actStrip'])
        if len(own['actStrip'])>=1:
            Next = own['actStrip'][0]
            func = eval(Next[0])
            func(cont,Next)
            
            
            
           
        
        else:
            print('RemActStip')
            del own['actStrip']
            own['Active']=False            
                    
main()



is there a non eval method?

It would help if we knew what was in own[‘actStrip’] (or [‘ActStrip’]).
Is it a list of numbers containing something? It took me a good 15 minutes to figure that out.
That’s what’s implied by this line:

Next.append(int(Vect2[0]*(100-own.parent['MoveRateWalk'])))

In which case you don’t need to eval it because it is already an int.
Or are you redefining ‘Next’ lots of times.

I would advise using better variable names, more functions etc. to help make your code clearer.

Quite frankly, I have no idea what you are trying to do with this code. What should it achieve?

As you know, using eval is bad. normally you can parse the string some other way. It it’s a string of an integer, use ‘int(string)’ if it’s a function, either use a lookup dictionary or the globals dictionary. Or just redesign it so that you don’t need to parse anything. What are you parsing here anyway?

accidental overpost error*

a function call

Next = next action list

so Next[0] = the function called, then the rest of each next is function specific*

func = eval(Next[0])
func(cont, Next)

calls a function
and sends it the data

edit:
Next.append(int(Vect2[0](100-own.parent[‘MoveRateWalk’])))
this is how I set the movement rate

Vect2[0]= inital distance to mark

is adding a timer to the end of the walk to mark and run to mark actions,
if one does not exist*

basically ensuring they end up at the target and move at a smooth rate,
but later I intended on also adding a ‘pathTo’ function that uses A* to get to a mark,

Bump - I still would like to remove the eval() and use a dictionary but I am unsure of how to call a function using a dictionary

(I just don’t get the syntax)

You can call a function through a dictionary by using something like this:

import bge


def mike():
    print("Hello Mike")

def alex():
    print("Hello Alex")

func_list = {
    0 : "alex",
    1 : "mike"
}


# Calling the function

func_list[0]()

This method is actually used to mimic the “switch” or the “case” statement in python so you can save performance by reducing the number of checks and not use a bunch of “if-elif” statements.

string is not callable

eval works for this, but I need to use a dictionary, to call a function…

edit just found this -

def p1( args ):    whatever

def p2( more args):
    whatever

myDict={
    "P1": p1,
    "P2", p2,
    ...
    "Pn", pn
}

def myMain(name):
    myDict[name]That's all you need.

Wisaam didn’t mean to use strings there, instead just place the name of the function.

Oh my bad, remove the quote signs from the dictionary entries mike and alex, it should work.

Thanks Agoose, thanks Wisaam, got it working!
:smiley:


import bge

import copy

def set(cont,Next):
    own=cont.owner
    target = own.scene.objects[Next[1]]
    prop = Next[2]
    target[prop]=Next[3]
    own['actStrip'].pop(0)


def toggle(cont,Next):
    own=cont.owner
    target = own.scene.objects[Next[1]]
    prop = Next[2]
    if target[prop]==False:
        target[prop]=True
    else:
        target[prop]=False
    own['actStrip'].pop(0)
    
    
def act(cont,Next):
    own=cont.owner
    Time = Next[2]
    if Time<Next[3]:
                    #print('Playing')
        own.parent['GFX'].playAction(Next[1],Next[2],Next[2],layer=6, priority=1, blendin=0, play_mode=1, layer_weight=0.0, ipo_flags=0, speed=0.0, blend_mode=1)                 
        Next[2]+=1
    else:
        own.parent['GFX'].stopAction(1)
        own['actStrip'].pop(0)    
           
           
           
def walk(cont,Next):
    own = cont.owner
    Mark=own.scene.objects[Next[1]]
    Vect2 = own.parent.getVectTo(Mark)
    if len(Next)<3:
        
        
        Next.append(int(Vect2[0]*(100-own.parent['MoveRateWalk'])))
    else:
        Time = Next[2]
        if Time>=1:
            own.parent.alignAxisToVect(Vect2[1],1,.1)
            own.parent.alignAxisToVect([0,0,1],2,1)
            own.parent.applyMovement(Vect2[1]*(Vect2[0]/Time),0)
            Next[2]-=1   
        if Time==0:
            own.parent.worldPosition=Mark.worldPosition
            Diff = own.parent.worldOrientation.col[1]-Mark.worldOrientation.col[1]
            own['Read2']=Diff.magnitude
            if Diff.magnitude>.05:
                own.parent.alignAxisToVect(Mark.worldOrientation.col[1],1,.1)
                own.parent.alignAxisToVect([0,0,1],2,1)
                
            else:
                own.parent.worldOrientation=Mark.worldOrientation
                own['actStrip'].pop(0)
            
            
            
def run(cont,Next):
    own=cont.owner
    Mark=own.scene.objects[Next[1]]
    Vect2 = own.parent.getVectTo(Mark)
    if len(Next)<3:
        
        
        Next.append(int(Vect2[0]*(100-own.parent['MoveRateRun'])))
    else:
        Time = Next[2]
        if Time>=1:
            own.parent.alignAxisToVect(Vect2[1],1,.1)
            own.parent.alignAxisToVect([0,0,1],2,1)
            own.parent.applyMovement(Vect2[1]*(Vect2[0]/Time),0)
            Next[2]-=1   
        if Time==0:
            own.parent.worldPosition=Mark.worldPosition
            Diff = own.worldOrientation.col[1]-Mark.worldOrientation.col[1]
            own['Read2']=Diff.magnitude
            if Diff.magnitude>.05:
                own.parent.alignAxisToVect(Mark.worldOrientation.col[1],1,.1)
                own.parent.alignAxisToVect([0,0,1],2,1)
                
            else:
                own.parent.worldOrientation=Mark.worldOrientation
                own['actStrip'].pop(0)








MyDict = { 'run':run, 'walk':walk , 'act':act, 'toggle':toggle , 'set':set } 


#______________________________________________________________________#
########################################################################


def main():
    cont = bge.logic.getCurrentController()
    own = cont.owner


    
    if 'GFX' not in own.parent:
        for child in own.parent.children:
            if 'Armature' in child:
                own.parent['GFX']=child
                print('FoundGFX')
    Collision = cont.sensors['Collision']
    X = cont.sensors['KeyX']
    
    if 'actStrip' not in own:
        if Collision.positive and X.positive:
            own['actStrip']=copy.deepcopy(Collision.hitObject['ActStrip'])
            
            own['Active']=True
    
    else: 
           
        own['Read']=str(own['actStrip'])
        if len(own['actStrip'])>=1:
            Next = own['actStrip'][0]
            
            MyDict[Next[0]](cont,Next)
            
            
            
           
        
        else:
            print('RemActStip')
            del own['actStrip']
            own['Active']=False            
                    
main()



Attachments

ActStrip3.blend (662 KB)