need help converting a script

I’m using an online tutorial to make a game, but a script in it is written for blender 2.4. Can anyone help me convert it, or tell me how to do it myself? Thanks!

cont = GameLogic.getCurrentController()

#define sensors
fired = cont.getSensor(“fire”).isPositive()
reload = cont.getSensor(“reload”).isPositive()

#define actuator
recoil = cont.getActuator(“play_recoil”)

#variables
isFiring = False
Ammo = getattr(cont.getOwner(), “Ammo”)
Clips = getattr(cont.getOwner(), “Clips”)

#define events…
if fired:
if Ammo > 0:
setattr(cont.getOwner(), “Ammo”,Ammo - 1)
isFiring = True
else:
if Clips > 0:
setattr(cont.getOwner(), “Clips”, Clips - 1)
setattr(cont.getOwner(), “Ammo”, 200)
if reload:
if Clips > 0:
setattr(cont.getOwner(), “Clips”, Clips - 1)
setattr(cont.getOwner(), “Ammo”, 200)
GameLogic.addActiveActuator(recoil, isFiring)

GameLogic => bge.logic
getSensor(“sensor”) => senosrs[“senosr”]
getActuator(“actuator”) => actuators[“actuator”]
getOwner() => owner

and gameObject properties are now accessed through the usual dictionary method (object[“property”], but I think get/setattr still work (?).
Besides that, just read the errors when you run the game.

It’s a bit difficult to read the code, if you hit “Go advanced” when posting you have a load more options and the button with a “#” will insert the code tags for you that will keep indentation. Or type the tags <code> and </code> around your script but with square brackets instead of angles.

I can’t test this since I don’t have your files, but the direct conversion would be something like the script below (it probably needs debugging). There is probably a more efficient way of doing it.


#cont = GameLogic.getCurrentController()
import bge
cont = bge.logic.getCurrentController()

#define sensors
#fired = cont.getSensor("fire").isPositive()
fired = cont.sensors["fire"].positive
#reload = cont.getSensor("reload").isPositive()
reload = cont.sensors["reload"].positive

#define actuator
#recoil = cont.getActuator("play_recoil")
recoil = cont.actuators["play_recoil"]

#variables
isFiring = False
#Ammo = getattr(cont.getOwner(), "Ammo")
Ammo = cont.owner["Ammo"]
#Clips = getattr(cont.getOwner(), "Clips")
Clips = cont.owner["Clips"]

#define events....
if fired:
    if Ammo &gt; 0:
        #setattr(cont.getOwner(), "Ammo",Ammo - 1)
        cont.owner["Ammo"] = Ammo - 1
        isFiring = True
    else:
    if Clips &gt; 0:
        #setattr(cont.getOwner(), "Clips", Clips - 1)
        cont.owner["Clips"] = Clips - 1
        #setattr(cont.getOwner(), "Ammo", 200) 
        cont.owner["Clips"] = Clips - 1
if reload:
    if Clips &gt; 0:
        #setattr(cont.getOwner(), "Clips", Clips - 1)
        cont.owner["Clips"] = Clips - 1
        #setattr(cont.getOwner(), "Ammo", 200) 
        cont.owner["Ammo"] = 200
#GameLogic.addActiveActuator(recoil, isFiring)
if isFiring:
    cont.activate[recoil]
else:
    cont.deactivate[recoil]

It’s strange to see how much has changed in such a short time, but the changes are mainly in syntax. The underlying data structures are pretty much the same. Hope this helps. :slight_smile:

FunkyWyrm is correct. You can also switch out

cont.owner[‘Clips’] = Clips - 1

with

cont.owner[‘Clips’] -= 1

thanks everyone! the script works now :smiley: