moving a non-dynamic box with python

I’ve got a non-dynamic box that i want my character to push around with a spell. Depending on what side of the box gets hit, i want the box to move (with DLoc, because it’s not dynamic) a certain distance. I don’t want it to just automatically be one spot in one moment and in another spot in the next, I want it to gradually move a certan distance. I’ve been about to get kind of far on my script:

cont = GameLogic.getCurrentController()
own = cont.getOwner()
Motion = cont.getActuator("UP")

Motion.setDloc(0,0,1,1)
GameLogic.AddActiveActuator(Motion,1)

This code makes the box start moving, but it doesn’t stop (i’m using space to activate the script). How do i stop it?

The main reason i don’t want the box to be dynamic is because of the fact that the character needs to be able to climb up onto it. But this makes it so that the box goes through walls, any way i could prevent that?

Sma

cont = GameLogic.getCurrentController()
own = cont.getOwner()
Motion = cont.getActuator("UP")
Space = cont.getSensor("Space")

if Space.isPositive():
	Motion.setDloc(0,0,1,1)
	GameLogic.addActiveActuator(Motion, 1)
else:
	GameLogic.addActiveActuator(Motion, 0)

[EDIT] Oops, posted the code but forgot the explanation. Motion actuators stay active until they are deactivated. So you need to check if the key is pressed or not, and if it isn’t, deactivate the actuator.

I tried something like this not too long ago. I got it to work, but it could have been a lot better. Luckily I ran into a lot of the same problems.

To make the object stop when you let go of the key, just get rid of that addActiveActuator() function and add another “And” controller hooked up to the same things the Pyhton controller is wired to.
Collision can be pretty tricky. You can try to do something with rays. All you would have to do is add four rays, pointing one in each direction the object can move in. Then you can access those rays in python and tell the object to stop moving in that direction if a ray is firing.

That all sounds a bit confusing. Try a few things out though and post if you have any trouble.

Ok, now for my next question…

So i now i have the box moving a bit when space is pressed. How can i have it move a set distance and then stop? I’ll try and get it working, but can you have a script make it move 1 dloc at a time until it moves 5 spaces?

Pooba

Also, why can’t i do something like this:

cont = GameLogic.getCurrentController() 
own = cont.getOwner() 
Motion = cont.getActuator("UP") 
Space = cont.getSensor("Space") 
loop = 5

if Space.isPositive(): 
   while loop > 0:
        Motion.setDloc(0,0,1,1) 
        GameLogic.addActiveActuator(Motion, 1) 
        loop = loop - 1
GameLogic.addActiveActuator(Motion, 0) 

and just have a part of the code stop the motion actuator?

Sam

Python scripts are run inside the main game loop, each cycle of the main loop is 1 frame. It’s impossible to have a loop in a python script work over several frames, because the frame will just pause while the loop is executed, then continue after it finishes.

To get something to happen accross several frames, you’ll need to use a property on the object to keep track of the current loop position. (i.e. use an int property on the object, and increase it by 1 each frame. Then you can check the property to tell how many frames the script has run.)

example:

# this script assums your object has an int property called loop

cont = GameLogic.getCurrentController()
own = cont.getOwner()
Motion = cont.getActuator("UP")
Space = cont.getSensor("Space")

if Space.isPositive() and own.loop > 0:
        Motion.setDloc(0,0,1,1)
        GameLogic.addActiveActuator(Motion, 1)
        own.loop = own.loop - 1
GameLogic.addActiveActuator(Motion, 0)