Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / lagging on bullets

Author
Message
ruckertheron
8
Years of Service
User Offline
Joined: 12th Nov 2016
Location:
Posted: 5th Feb 2018 22:35
Ok I'm working on a 2d shooter. Every time I shoot multiple bullets, it seems to lag. I believe it could be somewhere in the loop that handles the bullets.
This is just the part that handles drawing the bullets..


HandleBullets:

q=0
do
inc q
if q>MaxBullets then exit


if getstringtoken(bullet[q],"|",1)="1"




if GetSpriteExists(BulletSpriteStart+q)=0
wea$=localsprite[sel_button]: wea=val(wea$)

// create how bullet looks
ima=GetBulletImage(wea$)

CreateSprite(BulletSpriteStart+q,ima)


SetSpriteSize(BulletSpriteStart+q,GetBulletImageXSize(ima),GetBulletImageYSize(ima))
SetSpriteDepth(BulletSpriteStart+q,CharacterDepth)

else

// draws bullets
bspr=BulletSpriteStart+q
bX_=val(getstringtoken(bullet[q],"|",2))
bY_=val(getstringtoken(bullet[q],"|",3))

SetSpritePosition(bspr, bX_, bY_)

/// gosub BulletCollision

/// gets bullets direction
bf$=getstringtoken(bullet[q],"|",4)
bd_=val(getstringtoken(bullet[q],"|",5))
d#=GetGunBulletSpeed(localsprite[sel_button])


if bf$="0,-1"
dec bY_,d#
setspriteangle(bspr,270)
endif
if bf$="0,1"
inc bY_,d#: setspriteangle(bspr,90)
endif
if bf$="1,0"
inc bX_,d#: setspriteangle(bspr,0)
endif
if bf$="-1,0"
dec bX_,d#: setspriteangle(bspr,180)
endif
inc bd_,1


/// gosub WriteNetworkMessages
/// gosub ReadNetworkMessages








/// if zombies get hit `
kk=0
do
inc kk
if kk>MaxZombies then exit
if getspriteexists(ZombieSpriteStart+kk) and getspriteexists(bspr)
if getspritecollision(bspr,ZombieSpriteStart+kk)
// hurt zombie by health
gd#=GetGunBulletDamage(localsprite[sel_button])
zombies[kk,Zhealth]=str(val(zombies[kk,Zhealth])-gd#)
// sound
playsound(ZombieHitSound)
/// push zombie back
n=val(getstringtoken(bf$,",",1))
w=val(getstringtoken(bf$,",",2))
zombies[kk,zX]=str(val(zombies[kk,zX])+(n*4))
zombies[kk,zY]=str(val(zombies[kk,zY])+(w*4))


/// display health meter
health_=val(zombies[kk,Zhealth])
zombies[kk,Zhealth_tstart]=str(GetMilliseconds())



// if zombie health is done.. he dies
if val(zombies[kk,Zhealth])<0
playsound(ZombieDieSound)
deletesprite(ZombieSpriteStart+kk)
sss=ZombieSpriteStart+kk
s1=sss+20000: s2=sss+40000
deletesprite(s1): deletesprite(s2)

rri=random(1,100)

if rri=1 or rri=5 or rri=17 or rri=19 or rri>40
ih=FindFreeItem()
ri=GetRandomItemImage()
CreateSprite(ItemSpriteStart+ih,ri)
items[ih,iX]=(zombies[kk,zX])
items[ih,iY]=(zombies[kk,zY])
if ri=healthkit
items[ih,iImage]=str(ri)
items[ih,iInv]=str(health)
items[ih,iQuanity]=str(15)
endif
if ri=moneyitem
items[ih,iImage]=str(ri)
items[ih,iInv]=str(money)
items[ih,iQuanity]=str(1)
endif
if ri=ammo
items[ih,iImage]=str(ri)
items[ih,iInv]=str(weapon1ammo)
items[ih,iQuanity]=str(10)
endif
endif

j=0
do
inc j
if j>20 then exit
zombies[kk,j]=""
loop

endif


// kill bullet once it hit zombie
if val(localsprite[sel_button])<>cannon
bd_=0: bullet[q]="0|0|0|0,0|0"
deletesprite(bspr)
endif













else /// if it doesnt collide with zombie
bullet[q]="1|"+str(bX_)+"|"+str(bY_)+"|"+bf$+"|"+str(bd_)
/// gosub BulletCollision
endif



endif
loop


/// if bullets goes its distance then kill it
if bd_>GetGunBulletRange(localSprite[sel_button])
bd_=0: bullet[q]="0|0|0|0,0|0"
deletesprite(bspr)
endif


` gosub BulletCollision



endif
endif
loop
return
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 7th Feb 2018 00:31 Edited at: 7th Feb 2018 00:40
i think its because you push endless messages into outgoing network buffer.
its better if send only a goto x,y message, not 60 FPS the position.

here its maybe a integer, but position/speed/angle is better in float.

for network messages try using a message id (and
AddNetworkMessageFloat
AddNetworkMessageInteger
if you get a message you can make a select case id and read the struct from it.
this getstringtoken & string casting to value it not good.
you should also put the received message into a type/struct.

the code snippet shouts for more usage of functions.

example how to split into different kind of message blocks.
AGK (Steam) V2017.12.12 : Windows 10 Pro 64 Bit : AMD (17.12.1) Radeon R7 265 : Mac mini OS High Sierra (10.13)

Login to post a reply

Server time is: 2024-11-24 02:40:01
Your offset time is: 2024-11-24 02:40:01