Help - Search - Members - Calendar
Full Version: Gun Handling Array Problem
Game Maker Community > Working with Game Maker > Advanced Users Only
frankpiet
using gm 7 pro

my scripts wont work! they're supposed to fire a bullet out the tip (working), take off one ammo (kinda working) and wait (not working)
then load up again and wait. depending if youve ran out of ammo, reload the rack and wait.
by wait i mean use my own alarm (aka gun_stats[gt,3])
the problem is that it doesnt wait and fires a continuous stream of bullets, and when it gets to 1, it wont fire (rather than reload)

before you look at the scripts, some stuff you should know:
- gt represents gun type
- gs is an object which holds the array of gun_stone
- the rifle (gt 2) turns into sniper mode (gt 6) in a key press event. they have the same properties, exept for the z and the firing position.

script 1: step event of obj_player
CODE
//gun_stats array: 0=ammo, 1=racks, 2=have?, 3=time til next fire, 4=canfire
//gun_stone array: 0=name, 1=reload amt, 2=round time, 3=reload time 4=pie, 5=forc, 6=bur, 7=velocity, 8=size, 9=z

//rifle=sniper
if gt=2
gun_stats[6,0]=gun_stats[2,0]
gun_stats[6,1]=gun_stats[2,1]
gun_stats[6,2]=gun_stats[2,2]
gun_stats[6,3]=gun_stats[2,3]
gun_stats[6,4]=gun_stats[2,4]
if gt=6
gun_stats[2,0]=gun_stats[6,0]
gun_stats[2,1]=gun_stats[6,1]
gun_stats[2,2]=gun_stats[6,2]
gun_stats[2,3]=gun_stats[6,3]
gun_stats[2,4]=gun_stats[6,4]

//skipping guns i dont have them
while gun_stats[gt,2]=0
{
repeat (6)
{
if gt=1 or 2 or 3 or 4
{gt+=1}
if gt=5
{gt=0}
if gt=6
{gt=3}
}
}

//anti-below 0
if gun_stats[gt,0]<0 gun_stats[gt,0]=0;
if gun_stats[gt,1]<0 gun_stats[gt,1]=0;
if gun_stats[gt,2]<0 gun_stats[gt,2]=0;
if gun_stats[gt,3]<0 gun_stats[gt,3]=0;
if gun_stats[gt,4]<0 gun_stats[gt,4]=0;

//self-alarm for reload
if gun_stats[gt,3]>0
gun_stats[gt,3]-=1


script 2: space bar (fire) for player 1
CODE
//gun_stone array: 0=name, 1=reload amt, 2=round time, 3=reload time 4=pie, 5=forc, 6=bur, 7=velocity, 8=size, 9=z
//gun_stats array: 0=ammo, 1=racks, 2=have?, 3=time til next fire, 4=canfire

if gun_stats[gt,0]>0 and gun_stats[gt,4]=1 and gun_stats[gt,3]=0
{

        var nb; nb=0;
        x1=x + lengthdir_x(26, direction)+ sin(direction*pi/180)*5; y1=y + lengthdir_y(26, direction)+ cos(direction*pi/180)*5;
        x2=x + lengthdir_x(26, direction); y2=y + lengthdir_y(26, direction);

if gt =!6 //check if you are in sniper mode, therefore having the gun in front of you
    {nb=instance_create(x1,y1,obj_bullet);}
    else
    {nb=instance_create(x2,y2,obj_bullet);}
    
    
    //give the bullet the properties as stated in the create of gun_stone which is a array of object gs
    nb.pie=gs.gun_stone[gt,4]; nb.forc=gs.gun_stone[gt,5]; nb.bur=gs.gun_stone[gt,6];
    nb.velocity=gs.gun_stone[gt,7]; nb.size=gs.gun_stone[gt,8]; nb.z=gs.gun_stone[gt,9];
    nb.direction=direction;;
    
//take off ammo and prevent shooting    
gun_stats[gt,0]-=1
gun_stats[gt,4]=0

//reload bullet
if gun_stats[gt,0]>0
{gun_stats[gt,3]-=gs.gun_stone[gt,2]; gun_stats[gt,4]=1;}

//reload rack
if gun_stats[gt,0]=0 and gun_stats[gt,1]>0
{gun_stats[gt,3]-=gs.gun_stone[gt,3]; gun_stats[gt,0]=gs.gun_stone[gt,1]; gun_stats[gt,4]=1;}

}


if you really need the arrays creation event scripts then tell me and i will post them

thanks very much
frank
ale_jrb
Firstly, for the love of all things good in our world, please learn to indent your code properly...


For your reloading, there are a number of problems. First, you subtract from the wait time rather than adding to it, so the pause time is always < 0, and thus you shoot continuously. You also have problems with some if statements, and specifically when code will be executed. As well as this, you are messing around with both a wait timer, and a canfire variable. In this type of system, the canfire should be reserved for when the user actually can't fire for some reason - like they're in a room full of gas/whatever.

Try this:
CODE
//take off ammo and prevent shooting - copy from here down
gun_stats[gt,0] -= 1; // reduce available ammo by 1

// check the reloading of bullets
if (gun_stats[gt,0] > 0) // if we have ammo left in the round, just add on to the wait timer
{
    gun_stats[gt,3] += gs.gun_stone[gt,2]; // set the wait time
}
else if (gun_stats[gt,1] > 0) // if not, check for racks
{ // we have one - use it
    gun_stats[gt,1] -= 1; // remove the rack we just used
    gun_stats[gt,0] = gs.gun_stone[gt,1]; // increase the ammo the correct amount
    gun_stats[gt,3] += gs.gun_stone[gt,3]; // set the wait time
}
else
{
    // no ammo, no racks - do nothing. :)
}
frankpiet
ok, i put that in my fire code in place for my take off ammo and below (with the bracket).
but the reload doesnt work properly!
the rack gets taken off and the timer works fine, but the ammo doesnt reload.

and also, sorry for not indenting the code properly snitch.gif
ale_jrb
Well, it should do. Have you set gs.gun_stone[gt, 1] correctly? (that should hold the amount of ammo in a rack. if it doesn't, replace that bit with the variable that does).
frankpiet
whoops lol my fault
had a value wrong in gun_stone

thanks for the help!
ale_jrb
wink1.gif

No problem.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.