python doesn't work

Hy,
I’m trying to make sort of a “worms” game.
I’m working on the python script for shooting a bullet.
This is my script:

cont = bge.logic.getCurrentController()

# sensors
fire = cont.getSensor("fire").isPositive()

# actuators
add_bullet = cont.getActuator("add_bullet")
recoil = cont.getActuator("play_recoil")

# variables
isFiring = False


# events #
##########
if fire:
    force = [ 0.0, -10.0, 0.0]
    local = True
    isFiring = True
        
    GameLogic.addActiveActuator(add_bullet, True)
        
    bullet = add_bullet.getLastCreatedObject()
        
    bullet.applyForce(force, local)
    
    GameLogic.addActiveActuator(recoil, isFiring)

Assume that all the actuators and sensors are spelled the right way.
Can anyone find a mistake? Because neither the “add_bullet” actuator or the “recoil” actuator work.

have you imported bge.logic?
if you’re using 2.55 use “import GameLogic”, then change bge.logic to GameLogic to make it easier to use old scripts.
Next you’re using deprecated code. i used this tutorial a while ago. i’ll change it

#with the latest 2.5* BGE you must import GameLogic
import GameLogic

#sets the script controller as cont
cont = GameLogic.getCurrentController()

# gets the sensors, now uses cont.sensors[""], not getSensor(""). also uses .positive, not isPositive()
fire = cont.sensors["fire"].positive

# gets the actuators, now uses cont.atuators[""], not getActuator("")
add_bullet = cont.actuators["add_bullet"]
recoil = cont.actuators["play_recoil"]

# variables
isFiring = False


# events #
##########
if fire:
    force = [ 0.0, -10.0, 0.0]
    local = True
    isFiring = True
    
    #new  version of GameLogic.addActiveActuator.
    controller.activate(add_bullet)
    
    #new  version of getObjectLastCreated()
    bullet = add_bullet.objectLastCreated
        
    bullet.applyForce(force, local)
    
    #the new api means that actuators are just activated or deactivated, not toggled depending on a second variable
    if isFiring == True:
        controller.activate(recoil)
    else:
        controller.deactivate(recoil)

Changed it, but didn’t help :s
And what can I do about the deprecated code?
I have only used python once or twice with the help of tutorials, so I’m still quit new to it.

And by the way, I am using blender 2.54 beta

sorry about the reply, didn’t see that you allready answered :s

But still not working. Even when I use your new peace of code.
What now?

have you got a blend file i can see?

ah ok, please, take your time.
I’m allready glad that you want to help XD

#with the latest 2.5* BGE you must import GameLogic
from bge import logic as GameLogic

#sets the script cont as cont
cont = GameLogic.getCurrentController()

gets the sensors, now uses cont.sensors[""], not getSensor(""). also uses .positive, not isPositive()

fire = cont.sensors[“fire”].positive

gets the actuators, now uses cont.atuators[""], not getActuator("")

add_bullet = cont.actuators[“add_bullet”]
recoil = cont.actuators[“play_recoil”]

variables

isFiring = False

events

##########
if fire:
force = [ 0.0, -10.0, 0.0]
local = True
isFiring = True

#new  version of GameLogic.addActiveActuator.
cont.activate(add_bullet)

#new  version of getObjectLastCreated()
bullet = add_bullet.objectLastCreated
    
bullet.applyForce(force, local)

#the new api means that actuators are just activated or deactivated, not toggled depending on a second variable
if isFiring == True:
    cont.activate(recoil)
else:
    cont.deactivate(recoil)

Thanks, it’s getting better.
The bullet appears. :applause:
and the animation plays. another :applause:
But there’s still something weird.
When I click once (which is the sensor fire) it keeps playing the animation and in fact it should stop after playing it once.
Secondly the bullet appears somewhere around the gun, but when releasing the mousebutton, it disappears again.
Any idea?

can you upload your blend file

Yeah sure
here it is.
The textures will probably give this pink color.
and there might be other stuff that’s not working, but still…

Attachments

Character_sys.blend (632 KB)

#with the latest 2.5* BGE you must import GameLogic
import GameLogic

#sets the script controller as cont
cont = GameLogic.getCurrentController()

# gets the sensors, now uses cont.sensors[""], not getSensor(""). also uses .positive, not isPositive()
fire = cont.sensors["fire"].positive

# gets the actuators, now uses cont.atuators[""], not getActuator("")
add_bullet = cont.actuators["add_bullet"]
recoil = cont.actuators["play_recoil"]

# variables
isFiring = False


# events #
##########
if fire:
    force = [ 0.0, -10.0, 0.0]
    local = True
    isFiring = True
    
    #new  version of GameLogic.addActiveActuator.
    cont.activate(add_bullet)
    
    #new  version of getObjectLastCreated()
    bullet = add_bullet.objectLastCreated
        
    bullet.applyForce(force, local)
    
    #the new api means that actuators are just activated or deactivated, not toggled depending on a second variable
if not fire:
    isFiring = False
        
if isFiring == True:
    cont.activate(recoil)
if isFiring == False:
    cont.deactivate(recoil)
       
   

It’s getting better and better.
But it’s not fully working yet.
The animation is working now, but the bullet’s aren’t right.
When I click, more than one bullet appears, and they don’t shoot away.
And when I release the mouse button, the bullets disappear again. :s

Character_sys.blend (632 KB)
You had the addobject time as “2” in the add object actuator. That meant the bullet lasted for 2 logic ticks i think.
Then if you want the gun to only shoot one bullet, even if you hold it-make the fire sensor a tap sensor and remove the true pulse. this attatchment does this. :slight_smile: hope it helps

Yeah this works:p
Thanks a lot really, I couldn’t have done it myself.

Maybe another question on which I don’t know an answer, but it’s not really related to the previous topic.
Should I make a new topic or just ask it here? I will just ask it and if necessary I will make a new topic.

I actually wanted to add a map of the environment like in a lot of fps games. On which you can see your enemies and allies.
Is this possible? (probably yes) But is it a lot of trouble?
Because I’m working on a small project and a first “beta-release” should be done in a few days. So could you judge if it’s possible to get this in this amount of time?

Okay, another question (feels like a Déjà vu XD)
I wanted to and multiple characters (ennemies and allies).
They each have a turn in which they can first move and secondly shoot. (each character should use the same buttons so left mouse button to shoot).
I wanted to do this with the logic bricks since I don’t know much of python.
But this didn’t turn out well. I feel that this would be much easier in python.
Any idea how I can start with something like that?

Yes it is possible, you can download a script for that and there is a video showing you how to use it.
My blog post about that, video and script.

And the second thing about multiple players… i think it is very easy to make with python. I don’t have time right now, but if you don’t get help here just PM me tomorrow.

okay, thanks.
I’ll let you know.
Now I’m going to test your script.:stuck_out_tongue:
Thanks

wow, nice tutorial.
One problem, my mini squares fall through the minimap.
Any idea why that happens?