News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Problem with values not being read AS they explicitly are?

Started by LemonWizard, January 30, 2013, 12:39:59 PM

Previous topic - Next topic

LemonWizard

Okay so I've got this space game I'm trying to make.
I have a powerup move that basically creates 5 new bullets.
The way I'm keeping track of this is the shot part inside the type

See here:

Quote
type missile2
   x
   y
   speed
   shot
endtype

I have an iterative loop it WAS a function.
The idea is, if a missiles .shot property=true
It should NOT be used.
There are several things in the game engine that set the missile to false.
So ... let's begin

Here we have:

it was a function I changed it into a subroutine because the function wasn't doing the job and even as a subroutine it's not doing the job :/

PlayBASIC Code: [Select]
specialblast:
blasttype=2
select blasttype

case 1


case 2

for temp=1 to 5
for temp2=1 to 200

if missiles(temp2).shot=false
index=temp



exitfor
endif

next temp2
missiles(index).shot=true
xx=mousex()+(temp-1)*20-50
yy=mousey()+CameraY
missiles(index).y=yy
missiles(index).x=xx
missiles(index).speed=7

next temp





endselect



return




What seems to be happening is that the missiles(temp).shot property seems to be being completely ignored, or CONSTANTLY evaluated false.
Instead of running the temp2 loop inside this originally I had a function called getfreemissile which returned an index.
There was a for loop that went from 1 to five inside of that, and returned a value. Then. It checked it against the typed array.. or.. yeah
Basically it went like this

Quote
index=getfreemissile() ;;the call not in the same part

function getfreemissile()
for temp=1 to 200
if missiles(temp).shot=false
index=temp
exitfor
endif

next temp

endfunction index



what that is supposed to do is iterate through.
Now I've got this part here.. which you saw. The part that calls the main subroutine (at this point) that basically says "Fire five missiles at once"
the subroutine was originally a function which utilized the getfreemissiles
The thing is yes this iterates in ORDER but it still should ONLY return missiles properties that are false.

I tried making the parameters for all functions to be missiles().missile2 (for the type) just like in the pb's about page.
Basically I'm having a harder time than I should getting playbasic to accept acknowledge and keep track of very simple variables.
I should not have to create a HUGE roundabout way for this stuff to work...

That being said if you can find any flaws in my main codebase let me know and maybe you can discover why it isn't working.
I have a few checks that turn the missiles(index).shot=false but the thing is.. I deleted those in my codebase and i was still getting the same bug.

To be clear the bug I'm trying to eliminate is a bug where yes the five missiles being shot at once works, but when I shoot five more while the first five are on screen, the original set of five missiles vanishes on me.
This should not happen as I've tried to make sure that the missiles(index).shot value is checked first so that missiles that HAVE been shot do not end up being used AGAIN for the new powerup... WHAT am I doing wrong o.0 Can anyone figure?
The reason the subroutines are a mess btw is because like I said, I changed them from functions to subs thinking "oh maybe it's a scope problem and maybe itll work somehow if it's part of the main codebase and not scoped at all locally"

PlayBASIC Code: [Select]
; PROJECT : health_bar
; AUTHOR : Laptop
; CREATED : 9/30/2012
; EDITED : 1/29/2013
; ---------------------------------------------------------------------
openscreen 1024, 768, 32, 1
;#include "input"
health#=360
maxhealth#=360
shieldbar#=360
max_missiles=300
missile_time=20
menurow=1
meninputtimer=timer()+100 ;;this keeps track of the first menu scrolling where the files menuimages are cycled from 1-5 depending on what item is selected

dim killcount(1)
killcount(1)=0

maxrange=10
minrange=-2

alienx=15
alieny=100
alienrange=8
aliendirection=1
scrollspeed=2

missiles_fired=1
missile_soundtrigger=1
Stage_Size=10000
//put all of my set up code and variable/array declarations HERE.
CameraX=0
CameraY=Stage_Size
star_size=1
currentalien=1
amount_of_aliens=25
missiletype=1
missiletypetimer=timer()+20

sound_shipexplodes=loadnewsound("pshhh.wav")
sound_shipshoots=loadnewsound("testshootings2.wav")
stars=loadnewfximage("stars2.bmp")
imagemaskcolour stars, rgb(0,0,0)

alien_missile=loadnewfximage("Bullet_Vertical_Small_red.bmp")
imagemaskcolour alien_missile, rgb(255, 255, 255)

ship=loadnewfximage("Player_Ship_Norm.png")
imagemaskcolour ship, rgb(0, 0, 255)

small_missile_green=loadnewfximage("Bullet_Vertical_Small_darkgreen.bmp")
imagemaskcolour small_missile_green, rgb(255, 255, 255)

small_missile_red=loadnewfximage("Bullet_Vertical_Small_darkred.bmp")
imagemaskcolour small_missile_red, rgb(255, 255, 255)

small_missile_blue=loadnewfximage("Bullet_Vertical_Small_darkblue.bmp")
imagemaskcolour small_missile_blue, rgb(255, 255, 255)

if fileexist("save.txt")

TEST=loadnewfximage("MainMenuWContinue.png")
else

TEST=loadnewfximage("MainMenu.png")

endif



dim alienship(4)
curship=22
for temp=1 to 4
alienship(temp)=loadnewfximage("spaceship"+str$(curship)+".bmp")
imagemaskcolour alienship(temp), rgb(255, 255, 255)
curship=curship+1
next temp

dim explosion(6)
for temp=1 to 6
explosion(temp)=loadnewfximage("alienexplodes"+str$(temp)+".bmp")
imagemaskcolour explosion(temp), rgb(255, 255, 255)
next temp


game_menu=loadnewfximage("Ship_Interface2.png")
menu_bg=loadnewfximage("menu_bg.png")
menu_items=loadnewfximage("menu_items.png")
imagemaskcolour menu_items, rgb(28, 33, 104)

big_enemyy=loadnewfximage("Big_enemy.bmp")
spawnship=loadnewfximage("Spawnship.png")
imagemaskcolour big_enemyy, rgb(255, 255, 255)
imagemaskcolour spawnship, rgb(255, 255, 255)

dim menuimages(5)
for temp=1 to 5
menuimages(temp)=loadnewfximage("menu_items"+str$(temp)+".png")
next temp





type star
x
y
hue
hue_changing
endtype

type alien
x
y
moved
direction
angle
speed
r
explosionframe
living
explosionx
explosiony
collided
explosionframetimer
currentmissile
respawn
currentframe
frametimer
endtype

dim aliens(amount_of_aliens) as alien
dim createdaliens(25) as alien

for temp=1 to 25
createdaliens(temp).living=false
createdaliens(temp).explosionframe=1
next temp



for temp=1 to amount_of_aliens
aliens(temp).x=rndrange(getscreenwidth()/2-getscreenwidth()/4, getscreenwidth()/2+getscreenwidth()/4 )
aliens(temp).y=rndrange(getscreenheight()/2-getscreenheight()/4, getscreenheight()/2+getscreenheight()/4)
aliens(temp).moved=0
aliens(temp).direction=rndrange(1, 2)
aliens(temp).angle=rndrange(1, 360)
aliens(temp).speed=rndrange(20, 50)
Login required to view complete source code


Please get back to me on this soon, anyone who can figure this out.
If you need the actual game's files in order to help me let me know in your response and I'll send what I have.
Thanks in advance.
P.S. SORRY FOR THE MESSY CODE I'm trying to force this one thing to work that's why it's such a cluster.

kevin

  There's a number simple logic faults in it and this one is no exception.   All you needed to do is #PRINT the value of INDEX in the loop, and it soon becomes apparent where the fault is.  

 So if we look at this subset of the original source..

PlayBASIC Code: [Select]
health#=360
maxhealth#=360
shieldbar#=360
max_missiles=300
missile_time=20
menurow=1
meninputtimer=timer()+100 ;;this keeps track of the first menu scrolling where the files menuimages are cycled from 1-5 depending on what item is selected

dim killcount(1)
killcount(1)=0

maxrange=10
minrange=-2

alienx=15
alieny=100
alienrange=8
aliendirection=1
scrollspeed=2

missiles_fired=1
missile_soundtrigger=1
Stage_Size=10000
//put all of my set up code and variable/array declarations HERE.
CameraX=0
CameraY=Stage_Size
star_size=1
currentalien=1
amount_of_aliens=25
missiletype=1
missiletypetimer=timer()+20



type missile2
x
y
speed
shot
endtype


dim missiles(max_missiles) as missile2
for temp=1 to max_missiles
missiles(temp).speed=5+scroll_speed
missiles(temp).shot=false
next temp



gosub specialblast
gosub specialblast
sync
waitkey
end



specialblast:
blasttype=2
select blasttype

case 1


case 2

for temp=1 to 5

for temp2=1 to 200
if missiles(temp2).shot=false
index=temp
exitfor
endif
next temp2

; print the Index to the debug console.
#print Index

missiles(index).shot=true
xx=mousex()+(temp-1)*20-50
yy=mousey()+CameraY
missiles(index).y=yy
missiles(index).x=xx
missiles(index).speed=7

next temp

endselect

return





  When we run in F7, we see output in the console.


1
2
3
4
5
1
2
3
4
5


    Index is always returning a value from 1 to 5 each iteration.  Why, because Index is set to the outter FOR loops variable TEMP and not the inner loop counter variable TEMP2 which is used for the search.

   
PlayBASIC Code: [Select]
               for temp=1 to 5

for temp2=1 to 200
if missiles(temp2).shot=false
; So here Index should be set to TEMP2
index=temp2
exitfor
endif
next temp2

; print the Index to the debug console.
#print Index

missiles(index).shot=true
xx=mousex()+(temp-1)*20-50
yy=mousey()+CameraY
missiles(index).y=yy
missiles(index).x=xx
missiles(index).speed=7

next temp





   After makiing the change the missile array contains this,




----------------------------------------------------------
MISSILES.missile2
Type =missile2
Size =1204
Bank =149
----------------------------------------------------------


MISSILES(1).missile2
---------------------------------------------
X  =237
Y  =10213
SPEED  =7
SHOT  =1

MISSILES(2).missile2
---------------------------------------------
X  =257
Y  =10213
SPEED  =7
SHOT  =1

MISSILES(3).missile2
---------------------------------------------
X  =277
Y  =10213
SPEED  =7
SHOT  =1

MISSILES(4).missile2
---------------------------------------------
X  =297
Y  =10213
SPEED  =7
SHOT  =1

MISSILES(5).missile2
---------------------------------------------
X  =317
Y  =10213
SPEED  =7
SHOT  =1

MISSILES(6).missile2
---------------------------------------------
X  =237
Y  =10213
SPEED  =7
SHOT  =1

MISSILES(7).missile2
---------------------------------------------
X  =257
Y  =10213
SPEED  =7
SHOT  =1

MISSILES(8).missile2
---------------------------------------------
X  =277
Y  =10213
SPEED  =7
SHOT  =1

MISSILES(9).missile2
---------------------------------------------
X  =297
Y  =10213
SPEED  =7
SHOT  =1

MISSILES(10).missile2
---------------------------------------------
X  =317
Y  =10213
SPEED  =7
SHOT  =1

MISSILES(11).missile2
---------------------------------------------
X  =0
Y  =0
SPEED  =5
SHOT  =0

MISSILES(12).missile2
---------------------------------------------
X  =0
Y  =0
SPEED  =5
SHOT  =0

MISSILES(13).missile2
---------------------------------------------
X  =0
Y  =0
SPEED  =5
SHOT  =0


  etc etc etc

 





LemonWizard

#2
Thank you kevin I fixed it and I found out this line of my code was setting the missiles.shot trait to false too quickly!

This math must be wrong or something

o.0 look

I commented it out down there and the result? WALLS OF BULLETS YEAH! :D



screen_spot=CameraY-getscreenheight() ;;;SO THIS screen spot calculation was breaking it! and not allowing bullets to stay set to shot=true THIS MATH
;;IS WRONG
if missile_spot<screen_spot
;missiles(temp).shot=false
endif