Cycle through list of weapons and draw them,

List based logic selection V 1.0

Keys

q- pick up weapon

0 - empty hands

up arrow = cycle weapons up list

down arrow = cycle weapons down list

R=reload

Left mouse = fire

Attachments

WeaponManagerAlpha.blend (634 KB)

Your code does too much- It is going to be “god code”.
-> It is hard to read
-> It is hard to maintain.
-> It is even hard to use because of all this hard-coded names that describe something, but not what they do.

You already need a lot of comments (sign of complex code / dirty code). You will need much more documentation to describe how to use that.

The code contains some syntax I never saw before:


if own[add['MyAmmo']=]=>=add['Clip']:

Can you describe what this is doing (or is supposed to do). Because for me it look like incorrect syntax. So I’m a bit surprised.

You call your “draw weapon” code “main”. I guess this is because it does much more than drawing a weapon.
But “picks up the weapons” is called “main” too. If you write “god code” wouldn’t it make sense to have them all in the same “main”.

If I remove all the code and keep the comments the code is much shorter:


   ## Gun Changed
    ## if Gun is changed and gun 1 is selected
        ##if you already have a gun
                ##take it's ammo and end the object
        ##add new Gun
        ##fill the clip full else: what you have
        ##Parent to Gun Manager  
        ##move out and down as init is set in gun already to rise up   
    ## if Gun is changed and gun 1 is selected
        ##if you already have a gun
                ##take it's ammo and end the object
        ##add new Gun
        ##fill the clip full else: what you have
        ##Parent to Gun Manager  
        ##move out and down as init is set in gun already to rise up
     
   ##initiate reload if holding gun and pressing R        
        ##initiate shooting if holding gun and pressing left mouse                   

This is a better description at an higher level of what you want to do. If you write your code in that way, I think it would be more readable (and shorter).

My suggestions:

  • forget the god code. The reasons you see above.
  • forget main, it is the incorrect context.
  • have one code for one purpose, you can have even different objects for different purposes.

Now lets check what operations you want to do:

pick up a weapon
switch a weapon
reload a weapon
shoot a weapon
aim with a weapon

There remains some questions:
Switch weapon: switch to what weapon? If there are more then two there are several options

Reload weapon: Where is the ammunition coming from? What handles the stripes? Does this depend on the weapon type (stripe type/stripe size, loading speed …)? Does this depend on the specific weapon?

Shoot a weapon: Does this depend on the weapon type? Does this depend on the specific weapon (ammunition type, amount of ammunition used per shoot, time between shots, time from activation till fire, power loading …)? Does this depend on the specific weapon?

aim with a weapon: Does this depend on the weapon type? Does this depend on the specific weapon (zoom, stability …)? Does this depend on the specific weapon?

So own= gun manager,
add= added gun to scene

if own[add[‘MyAmmo’]=]=>=add[‘Clip’]:

is actually =


if manager[ammo type] > = gun clip size

so

if manager[‘Shells’] >= 8:

if drawing gun 1

(each gun stores what ammo it uses and how to fire and reload it)

this code is actually,

what bullets does it use?

Do you have enough to fill the clip?

Part 2

Each gun contains the logic to fire it, reload it, and animate it. as well as zoom etc eventually

the Gun manager triggers the logic, but does not know anything but is it ready to fire? ready to reload?
am I ready to switch weapons? Do I have a gun?

this is trigger reload if the gun is ready


if len(x)!=0:        
        if reload.positive:
            if own.children[0]['Reload']==0 and own.children[0]['Fire']==0 and own.children[0]['Init']==60:
                own.children[0]['Reload']=1

I added two of Gun_1 to make sure my list logic would not have two "Gun_1"s

SIDE NOTE

it keeps changing my code :frowning:


it will not stay correct if I reload the page.


this is what it keeps “mutating” to,

I did some tests, but I could not reproduce this behavior of the post. It looks like your code misinterprets the BB code of the post. It might be something with the browser itself. I do not know.

Anyway, yes this other code makes much more sense.

How do you think I would go about rewriting to cycle through the list?

key [---------and-------Add 1 to index , but how do I have it “wrap” back to 0?

Just check the bounds of the property, IE if the property is greater than the maximum allowed value, set it to 0.

Ok, I almost have it working, but something about the way I am filtering the index is not working,


import bge




def main():


    cont = bge.logic.getCurrentController()
    own = cont.owner


    sens = cont.sensors['Down']
    sens2=cont.sensors['Up']
    sens3=cont.sensors['Switch_0']
    index=own['Index']
    Len=len(own['Gun_List'])
    x=own.children
    if sens.positive and sens3.positive:
        if (index+1)>Len:
            own['Index']=0
        else:
            own['Index']=+1
        own['Switch']=120        
    if sens2.positive and sens3.positive:
        if (index-1)==-1:
            own['Index']=Len
        else:
            own['Index']=own['Index']-1
        own['Switch']=120        
    
    if sens3.positive:
        if sens.positive or sens2.positive:
             
            print("initiated")    
            ##if you already have a gun
            if len(x)!=0:
                for objects in own.children:
            ##take it's ammo and end the object
                    ammo=objects['MyAmmo']
                    own[ammo]+=objects['Bullets']
                    objects.endObject()
            print("initiated2")        
            scene=bge.logic.getCurrentScene()
            ##add new Gun
            add=scene.addObject(own['Gun_List'][own['Index']-1],own,0)
            ##fill the clip full else: what you have
            if own[add['MyAmmo']]>=add['Clip']:
                add['Bullets']=add['Clip']
                own[add['MyAmmo']]-=add['Clip']
            else:
                add['Bullets']=own[add['MyAmmo']]
                own[add['MyAmmo']]=0
                ##Parent to Gun Manager  
            add.setParent(own,0,1)
                ##move out and down as init is set in gun already to rise up
            add.applyMovement((0,2,-2),1)
        
       
            
main()




up and down keys are supposed to cycle up and down through list, it’s not quite right

so Q= Grab weapon

R=Reload

Mouse click =fire

but I still and going to need to fix

Up arrow, Down Arrow - cycle weapon, as it’s not perfect.

Attachments

WeaponSelfContainedLogicV8 (1s).blend (634 KB)


 Len=len(own['Gun_List'])
 if sens.positive and sens3.positive:
        if (index+1)>Len:
            own['Index']=0
        else:
            own['Index']=+1
        own['Switch']=120        
    if sens2.positive and sens3.positive:
        if (index-1)==-1:
            own['Index']=Len
        else:
            own['Index']=own['Index']-1
        own['Switch']=120        

this is the code that is not working correctly, any ideas?

I suggest you look at the implementation of the switcher.

Especially look for the modulo operation.

I still recommend to keep weapon switching away from the other operations.

I don’t understand it as well because it’s all function-ified

it’s hard to see where everything is,

I see a sensor adding a value and removing, and

idx=0

idx = (idx + step) % len(objects)

but I don’t understand it,

This is the code I mean (the second line).

I strongly suggest you look up, modulo. Then you should be able to understand this code. Run some tests with different values. That should help.

I strongly suggest you check what a function is and how to use it. This is essential for any further programming in Python.

ahh, so it’s a method to truncate -numbers into positive value?

a - (n * int(a/n))

No, that’s the modulus, but what you have written will give you a modulo result, provided that the int() function returns the floor of a division

Modulo can be useful for implemented wrap around, to keep a value in bounds:


maxmimum_value = 10

value = 11

print(value % (maximum_value + 1))
# >> 0

Yay!

it works perfectly!

Thank you sir!

Attachments

WeaponSelfContainedLogicMonsterIsCool.blend (634 KB)

This is some sort of absolute calculation (but not an efficient one). The according function is typically called abs(term)

Thanks again monster, never really understood what % was doing in the bge with lists,

Nice! this is getting there…I can just feel ( and see ) it happening

The answer to most FPS creator’s worst problem…finally answered
It was like when the mouselook script made by ryyy-something came around

Oh, and you might want to update your “Help” text file, as it’s old and doesn’t work anymore
I can’t seem to use any gun I pick up…but I’ll look at the script later and find out