Python If statements and Properties problem

Hello all!

I’ve been working on a gun swapping demo as requested by a fellow forumer, hoping to share it as a resource once I get it working right.

What I have so far is a basic moving character, three empties which spawn weapon pickups, and the weapon pickups themselves hidden in a different layer.

The basic idea is that the player has two properties representing gun slots: “gun01” and “gun02”, which by default are set to “none”. When the play gets in collision with a pickup object, either for an assault rifle, a blaster, or a shotgun, the player’s script checks if any weapons slots are empty (set to “none”) and fills them with whatever object the player is in collision with. In turn, the weapon pickups have logic setup which deletes them as soon as they collide with the player while any of his “gun” slots are set to “none”.

The problem is with the “gun01” and “gun02” properties when the game starts: they are immediately set to “AR” for assault rifle, even though the player hasn’t collided with anything. I don’t get any errors when I check the console, and the “if” statements seem fine. I’ve even gone as far as to initialize the “gun01” and “gun02” properties within the script in order to try to fix the problem.

Here is the code:

import bge

def main():

    cont = bge.logic.getCurrentController()
    player = cont.owner
    ## Sensors
    assaultRifle = cont.sensors['AR']
    blaster = cont.sensors['Blaster']
    shotgun = cont.sensors['Shotgun']
    player['gun01'] = "none"
    player['gun02'] = "none"
    if player['gun01'] == "none":
        if assaultRifle:
            player['gun01'] = "AR"
        elif blaster:
            player['gun01'] = "Blaster"
        elif shotgun:
            player['gun01'] = "Shotgun"
            print('no pickups')
    if player['gun02'] == "none":
        if assaultRifle:
            player['gun02'] = "AR"
        elif blaster:
            player['gun02'] = "Blaster"
        elif shotgun:
            player['gun02'] = "Shotgun"
            print('no pickups')
        print('gun slots full')


And the .blend:

gunSwapping.blend (587 KB)

Thanks in advance!

You’re evaluating each sensor as boolean true. This is valid. However you’re intending to validate the sensor state, hence use if assault rifle.positive

Sent from my Nexus 7 using Tapatalk HD

Dang it lol. xD One of the most basic rules to using sensors that i completely forgot. This is what happens when you leave scripting for over a year. :stuck_out_tongue: Thanks agoose! :smiley:

I feel like I should go and watch my own tutorials to recollect this stuff. :stuck_out_tongue: