move an object to a list of points

So I am trying to move an object to a list of points. For some reason, I just can’t figure out how to handle the lists. This example doesn’t work because the “listOfPoints” is constantly being re-updated to the original points, so popping doesn’t work.


import bge

cont = bge.logic.getCurrentController()
obj = cont.owner
motion = cont.actuators["Motion"]


listOfPoints = [1,5,8]


def moveTo(startingPosition, endingPosition):
    dis = endingPosition - startingPosition
    
    if dis < 0.05:
        motion.dLoc = [0,0,0]
        cont.activate(motion)    
        return 1
        
    else:
        motion.dLoc = [1,0,0]
        cont.activate(motion)
    

position = obj.position
if moveTo(position[0],listOfPoints[0]):
    print("yes")
    listOfPoints.pop(0)

The same thing would happen if I tried to create an index variable and sort through the list.


import bge

cont = bge.logic.getCurrentController()
obj = cont.owner
motion = cont.actuators["Motion"]


listOfPoints = [1,5,8]
index = 0

def moveTo(startingPosition, endingPosition):
    dis = endingPosition - startingPosition
    
    if dis < 0.05:
        motion.dLoc = [0,0,0]
        cont.activate(motion)    
        return 1
        
    else:
        motion.dLoc = [1,0,0]
        cont.activate(motion)
    

position = obj.position
if moveTo(position[0],listOfPoints[index]):
    print("yes")
    index = index + 1

The box moves to the first point just fine, I am just at a loss as to how to move on to the next point. Surely it can’t be that difficult? Can somebody help please?

as far as i can see you need to define the new starting position when it jumps to the first point, hope that helps

Sorry, I don’t really see what you mean. Do you mean the startingPosition in the function? The object position is constantly being updated, so I don’t see redefining it would help.

Create your ‘list of points’ variable once, and update it only when necessary, like this:


if not 'init' in obj:
     obj['init'] = 1
     obj['listofpoints'] = [[1, 0, 2]]

Notice that I made the list of points variable a list that holds another list - this way, you can add points to the list with the append() or insert() functions:


obj['listofpoints'].append([2, 3, 4])

Every time you pop the list, it should delete whatever entry is at the top of it - you might want to have more control by removing the specific point (obj[‘listofpoints’].remove(startingPosition) might work) that you’re working with.

EDIT: That code is untested, so you might have to deal with some bugs.