Is it possible to put a varible in a handle.

Blitz3D Forums/Blitz3D Beginners Area/Is it possible to put a varible in a handle.

Newbie31(Posted 2012) [#1]
I'm having problems with checking if images exist, I have been trying to check if and image is loaded and drawn. but I get getting an error that the image doesn't exist.

this is what I have:
[bbcode]
Global image_sub1=LoadImage(image_1.png")
Global image_sub2=LoadImage(image_2.png")
Global image_sub3=LoadImage(image_3.png")
Global image_sub4=LoadImage(image_4.png")
Global image_sub5=LoadImage(image_5.png")

draw image_sub2,x,y
[/bbcode]

And this what I want
[bbcode]
t=2

Global image_sub1=LoadImage(image_1.png")
Global image_sub2=LoadImage(image_2.png")
Global image_sub3=LoadImage(image_3.png")
Global image_sub4=LoadImage(image_4.png")
Global image_sub5=LoadImage(image_5.png")

draw "image_sub+t",x,y
[/bbcode]

Is something like this possible?


Yasha(Posted 2012) [#2]
Well for a start your quote signs are unbalanced, as the syntax colouring shows...

If I understand correctly, what you're asking for is an array or something similar: being given several slots and accessing different ones by numeric index?

You definitely cannot however do anything that looks like the second code example. Variable names only exist at compile-time: any code that tries to manipulate variable names will not work, and has no way of possibly working, because that information has been stripped from the program (in fact, it never existed) at runtime.

You can create an array in two ways, using the Dim command, or using a subscript when declaring a variable. For more information about the different approaches, see here: http://www.blitzbasic.com/Community/posts.php?topic=98674#1154877


Newbie31(Posted 2012) [#3]
yasha thanks for the reply, I'll have alook at dims but just to be sure I 'll post my code so you can see what I'm trying to do,

[bbcode]
Include "MainMenu.bb"

;Set 2d Graphics
Graphics 1280,1000,32,0;640,480,32,2
SetBuffer BackBuffer()


;-------------------------------------------------------------------------------------------------------------------
;---------------------------------------------- SET GLOBAL CONSTRAINTS ---------------------------------------------
;-------------------------------------------------------------------------------------------------------------------


;Set Global Constraints
Global posizionex
Global posizioney
Global jetmovX=400
Global jetmovY=300
Global rate_of_fire = 10 ; number of milliseconds between each shot.
Global timer


;SET Health Globals
;[wall1]
Global wall1_main_health#=1
Global wall1_sub1_health#=50
Global wall1_sub2_health#=50
Global wall1_sub3_health#=50
Global wall1_sub4_health#=50
Global wall1_sub5_health#=50
Global wall1_sub6_health#=50
Global wall1_sub7_health#=50
Global wall1_sub8_health#=50
Global wall1_sub9_health#=50
Global wall1_sub10_health#=50



;Global Background Image Position And Scales
Global imX=-0
Global imY=-0

Global ippX=-0
Global ippY=-0

;Load Global Images
Global fighterjet=LoadImage("_Ships\fighter2.png") ;a simple sprite
Global missil=LoadImage("_Other\blast1a.png") ;a simple sprite

;Load Global Background Images
Global gfx_bg=LoadImage(level_name$+"\"+level_name$+"_001.jpg") ;Background


;Load Global Destructible Bricks

Global wall1_main=LoadImage(level_name$+"\wallpieces\wall1_0.png") ;Destructible Barrier Image wall1 BASE
Global wall1_sub1=LoadImage(level_name$+"\wallpieces\wall1_1.png") ;Destructible Barrier Image wall1 PIECE1
Global wall1_sub2=LoadImage(level_name$+"\wallpieces\wall1_2.png") ;Destructible Barrier Image wall1 PIECE2
Global wall1_sub3=LoadImage(level_name$+"\wallpieces\wall1_3.png") ;Destructible Barrier Image wall1 PIECE3
Global wall1_sub4=LoadImage(level_name$+"\wallpieces\wall1_4.png") ;Destructible Barrier Image wall1 PIECE4
Global wall1_sub5=LoadImage(level_name$+"\wallpieces\wall1_5.png") ;Destructible Barrier Image wall1 PIECE5
Global wall1_sub6=LoadImage(level_name$+"\wallpieces\wall1_6.png") ;Destructible Barrier Image wall1 PIECE6
Global wall1_sub7=LoadImage(level_name$+"\wallpieces\wall1_7.png") ;Destructible Barrier Image wall1 PIECE7
Global wall1_sub8=LoadImage(level_name$+"\wallpieces\wall1_8.png") ;Destructible Barrier Image wall1 PIECE8
Global wall1_sub9=LoadImage(level_name$+"\wallpieces\wall1_9.png") ;Destructible Barrier Image wall1 PIECE9
Global wall1_sub10=LoadImage(level_name$+"\wallpieces\wall1_10.png") ;Destructible Barrier Image wall1 PIECE10



;Set Handles
MidHandle fighterjet
MidHandle missil

;Hide Pointer
;HidePointer

;Set Global Constants
Const limite_sx=100
Const limite_dx=540


;Load GLOBAL FONTS
fntArial=LoadFont("Arial",24,False,False,False)
fntMSS=LoadFont("microsoft sans serif",16,True,False,False)
fntblitz=LoadFont("blitz",12,False,False,False)

;-------------------------------------------------------------------------------------------------------------------
;----------------------------------------- OPEN READ AND CLOSE FILES -----------------------------------------------
;-------------------------------------------------------------------------------------------------------------------

; Open the file to Read
filein = ReadFile("_Data"+"\"+level_name$+"_Data.txt")
;select lines to read from
Read1$ = ReadLine( filein ) ;Image Width
Read2$ = ReadLine( filein ) ;Image Height
Read3$ = ReadLine( filein ) ;wall1 Pieces
Read4$ = ReadLine( filein ) ;wall2 Pieces
Read5$ = ReadLine( filein ) ;
Read6$ = ReadLine( filein ) ;
Read7$ = ReadLine( filein ) ;
Read8$ = ReadLine( filein ) ;
Read9$ = ReadLine( filein ) ;
Read10$ = ReadLine( filein ) ;
Read11$ = ReadLine( filein ) ;
Read12$ = ReadLine( filein ) ;
Read13$ = ReadLine( filein ) ;
Read14$ = ReadLine( filein ) ;
Read15$ = ReadLine( filein ) ;
Read16$ = ReadLine( filein ) ;
Read17$ = ReadLine( filein ) ;
Read18$ = ReadLine( filein ) ;
Read19$ = ReadLine( filein ) ;
Read20$ = ReadLine( filein ) ;
Read21$ = ReadLine( filein ) ;
Read22$ = ReadLine( filein ) ;
Read23$ = ReadLine( filein ) ;
Read24$ = ReadLine( filein ) ;
Read25$ = ReadLine( filein ) ;
Read26$ = ReadLine( filein ) ;
Read27$ = ReadLine( filein ) ;
Read28$ = ReadLine( filein ) ;
Read29$ = ReadLine( filein ) ;
Read30$ = ReadLine( filein ) ;
Read31$ = ReadLine( filein ) ;
Read32$ = ReadLine( filein ) ;
Read33$ = ReadLine( filein ) ;
Read34$ = ReadLine( filein ) ;
Read35$ = ReadLine( filein ) ;
Read36$ = ReadLine( filein ) ;
Read37$ = ReadLine( filein ) ;
Read38$ = ReadLine( filein ) ;
Read39$ = ReadLine( filein ) ;
Read40$ = ReadLine( filein ) ;
Read41$ = ReadLine( filein ) ;
Read42$ = ReadLine( filein ) ;
Read42$ = ReadLine( filein ) ;
Read43$ = ReadLine( filein ) ;
Read44$ = ReadLine( filein ) ;
Read45$ = ReadLine( filein ) ;
Read46$ = ReadLine( filein ) ;
Read47$ = ReadLine( filein ) ;
Read48$ = ReadLine( filein ) ;
Read49$ = ReadLine( filein ) ;
Read50$ = ReadLine( filein ) ;
Read51$ = ReadLine( filein ) ;
Read52$ = ReadLine( filein ) ;
Read53$ = ReadLine( filein ) ;
Read54$ = ReadLine( filein ) ;

; Close the file once reading is finished
CloseFile( filein )
;Rename the information
wall1pieces%=Right(read3,2)
wall2pieces%=Right(read4,2)
wall1main_xpos%=Right(Read5,4)
wall1main_ypos%=Right(Read6,4)
wall2main_xpos%=Right(Read7,4)
wall2main_ypos%=Right(Read8,4)
wall1sub1_xpos%=Right(Read10,4)
wall1sub1_ypos%=Right(Read11,4)
wall1sub2_xpos%=Right(Read12,4)
wall1sub2_ypos%=Right(Read13,4)
wall1sub3_xpos%=Right(Read14,4)
wall1sub3_ypos%=Right(Read15,4)
wall1sub4_xpos%=Right(Read16,4)
wall1sub4_ypos%=Right(Read17,4)
wall1sub5_xpos%=Right(Read18,4)
wall1sub5_ypos%=Right(Read19,4)
wall1sub6_xpos%=Right(Read20,4)
wall1sub6_ypos%=Right(Read21,4)
wall1sub7_xpos%=Right(Read22,4)
wall1sub7_ypos%=Right(Read23,4)
wall1sub8_xpos%=Right(Read24,4)
wall1sub8_ypos%=Right(Read25,4)
wall1sub9_xpos%=Right(Read26,4)
wall1sub9_ypos%=Right(Read27,4)
wall1sub10_xpos%=Right(Read28,4)
wall1sub10_ypos%=Right(Read29,4)



;-------------------------------------------------------------------------------------------------------------------
;------------------------------------------------- CREATE TYPES ----------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------

;Create Missile Type
Type missile ;an object called missile
Field posx Field posy Field avanzamento Field contatore
Field image
Field missile_life_time
Field missile_life_timer
End Type

;Set Image Alpha Masks
MaskImage fighterjet,255,255,255 ;White Is Transparent
MaskImage missil,255,255,255 ;White Is Transparent




;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& MAIN GAME AREA &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Repeat

Cls

;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;------------------------------------------------- DRAW IMAGES -----------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------


;Draw Background Image
DrawImage gfx_bg,imX,imY

;_____________________________________________________________________________
;_____________________________________________________________________________

;############################################
;############################################
;Draw Destructible Image Barriers

If wall1_sub1_health#>0 And wall1pieces%>=1
DrawImage wall1_sub1,imX+wall1sub1_xpos%,imY+wall1sub1_ypos%
EndIf
If wall1_sub2_health#>0 And wall1pieces%>=2
DrawImage wall1_sub2,imX+wall1sub2_xpos%,imY+wall1sub2_ypos%
EndIf
If wall1_sub3_health#>0 And wall1pieces%>=3
DrawImage wall1_sub3,imX+wall1sub3_xpos%,imY+wall1sub3_ypos%
EndIf
If wall1_sub4_health#>0 And wall1pieces%>=4
DrawImage wall1_sub4,imX+wall1sub4_xpos%,imY+wall1sub4_ypos%
EndIf
If wall1_sub5_health#>0 And wall1pieces%>=5
DrawImage wall1_sub5,imX+wall1sub5_xpos%,imY+wall1sub5_ypos%
EndIf
If wall1_sub6_health#>0 And wall1pieces%>=6
DrawImage wall1_sub6,imX+wall1sub6_xpos%,imY+wall1sub6_ypos%
EndIf
If wall1_sub7_health#>0 And wall1pieces%>=7
DrawImage wall1_sub7,imX+wall1sub7_xpos%,imY+wall1sub7_ypos%
EndIf
If wall1_sub8_health#>0 And wall1pieces%>=8
DrawImage wall1_sub8,imX+wall1sub8_xpos%,imY+wall1sub8_ypos%
EndIf
If wall1_sub9_health#>0 And wall1pieces%>=9
DrawImage wall1_sub9,imX+wall1sub9_xpos%,imY+wall1sub9_ypos%
EndIf
If wall1_sub10_health#>0 And wall1pieces%>=10
DrawImage wall1_sub10,imX+wall1sub10_xpos%,imY+wall1sub10_ypos%
EndIf



;############################################
;############################################

;Draw Player Image
DrawImage fighterjet,MouseX(),MouseY()


;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;------------------------------------ SET KEYBOARD AND MOUSE FUNCTIONS ---------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------



;Create Movement Keys And Align Image Movement



If KeyDown( 205 )
If imX>= -ImageWidth(gfx_bg) + GraphicsWidth()+5
imX=imX-5 ;Right Arrow
EndIf

ElseIf KeyDown( 203 )
If imX<= -1
imX=imX+5 ;Left Arrow
EndIf

ElseIf KeyDown( 208 )
If imY>= -ImageHeight(gfx_bg) + GraphicsHeight()+5
imY=imY-5 ;Up Arrow
EndIf

ElseIf KeyDown( 200 )
If imY<= -1
imY=imY+5 ;Down Arrow
EndIf
EndIf


;Set Keys for Image Position Tuning

;Determines the speed of the tuning varibles IPPX and IPPY
Color 0,0,0
If KeyHit(2) Then imspd#=0;
If KeyHit(3) Then imspd#=1;
If KeyHit(4) Then imspd#=2;
If imspd# = 0
moveslow()
Text 10,10,"Image movement : Fast " + imspd#
ElseIf imspd#=1
movefast()
Text 10,10,"Image movement : Slow " + imspd#
ElseIf imspd#=2
moverealfast()
Text 10,10,"Image movement : REAL FAST " + imspd#
EndIf


;Create Player Weapon Fire
If MouseDown(1)=1 ;if i hit the mouse button i create a missile
If MilliSecs() > timer + rate_of_fire Then ;set weapon firerate
create_missile() ;create bullets

;create_badfire() ;create alien fire ******* TESTING *******

timer = MilliSecs() ; reset the timer to the current millisecs value.
EndIf
EndIf

;This Updates The Weapon Fire
play_missile(wall1pieces%,wall1sub1_xpos%,wall1sub1_ypos%,wall1sub2_xpos%,wall1sub2_ypos%,wall1sub3_xpos%,wall1sub3_ypos%,wall1sub4_xpos%,wall1sub4_ypos%,wall1sub5_xpos%,wall1sub5_ypos%,wall1sub6_xpos%,wall1sub6_ypos%,wall1sub7_xpos%,wall1sub7_ypos%,wall1sub8_xpos%,wall1sub8_ypos%,wall1sub9_xpos%,wall1sub9_ypos%,wall1sub10_xpos%,wall1sub10_ypos%) ;this starts the missil anim


;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------- ONSCREEN TEXT AND HUD ------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------

SetFont fntblitz
Color 0,0,0

Text 0,40,"Number Of Destructible bricks: " + wall1pieces%


;Display My Testing Text
Text 0,80,"X Position (ippX): " +ippx
Text 0,100,"Y Position (ippY): " +ippY

Text 200,80,"X Position (imX): " +imX
Text 200,100,"Y Position (imY): " +imY


Text 0,120,"Mouse X Pos: " + MouseX() + " Mouse Y Pos: " + MouseY()


;show health test
Text 400,10,"wall1 sub 1 health : " +wall1_sub1_health#
Text 400,20,"wall1 sub 2 health : " +wall1_sub2_health#
Text 400,30,"wall1 sub 3 health : " +wall1_sub3_health#
Text 400,40,"wall1 sub 4 health : " +wall1_sub4_health#
Text 400,50,"wall1 sub 5 health : " +wall1_sub5_health#
Text 400,60,"wall1 sub 6 health : " +wall1_sub6_health#
Text 400,70,"wall1 sub 7 health : " +wall1_sub7_health#
Text 400,80,"wall1 sub 8 health : " +wall1_sub8_health#
Text 400,90,"wall1 sub 9 health : " +wall1_sub9_health#
Text 400,100,"wall1 sub 10 health : " +wall1_sub10_health#


Flip
Until KeyDown(1)=1 ;End program when ESC is pressed
End




; Clear all the fonts from memory!
FreeFont fntArial
FreeFont fntMSS
FreeFont fntblitz

;_______________________________________________________________________


;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& END OF GAME AREA &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&




;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------- CREATE GAME FUNCTIONS ------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------------------------




;-------------------------------------------------------------------------------------------------------------------
;--------------------------------------------- Create Player Missiles ----------------------------------------------
;-------------------------------------------------------------------------------------------------------------------

;Create Missiles Function
Function create_missile()
a.missile=New missile
a\image=missil
a\missile_life_time = 1500 ; bullet last 2 seconds (2000 milliseconds)
a\missile_life_timer = MilliSecs() ; set the timer to the same value as milliseconds,
;Keys
;a\posx=jetmovX ;the x position of missile creation is the mouse coords(the same coords of my jet of course)
;a\posy=a\posy+jetmovY-45;the missile creation start a bit up to the jet(for simulating the launch-bay hehe)
;Mouse
a\posx=MouseX();the x position of missile creation
a\posy=a\posy+MouseY()-35;the missile creation start a bit up To the jet

a\avanzamento=4 ;the speed of the missile
a\contatore=0 ;the starting value of the animation
End Function

;Update Missiles Function
Function play_missile(wall1pieces%,wall1sub1_xpos%,wall1sub1_ypos%,wall1sub2_xpos%,wall1sub2_ypos%,wall1sub3_xpos%,wall1sub3_ypos%,wall1sub4_xpos%,wall1sub4_ypos%,wall1sub5_xpos%,wall1sub5_ypos%,wall1sub6_xpos%,wall1sub6_ypos%,wall1sub7_xpos%,wall1sub7_ypos%,wall1sub8_xpos%,wall1sub8_ypos%,wall1sub9_xpos%,wall1sub9_ypos%,wall1sub10_xpos%,wall1sub10_ypos%)
For a.missile=Each missile
posizionex=a\posx
posizioney=a\posy
DrawImage a\image,a\posx,a\posy ;draw the missile image.
a\posy=a\posy-a\avanzamento ;this move the missile up
If posizioney=0 ;If missile is off screen delete it.
Delete a
Exit ;exit loop if missile is dead. will cause error as they's no missile to check
End If
;----------------------------------------------------------------------------------------------

;----------------------------------------------------------------------------------------------

;NON_MOVING IMAGES THAT COLLIDE WITH MISSILES , Detect collisions and determine damage

;THIS Caculates If All other Image Collidible Images are destroyed
;--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

;--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If ImagesCollide(a\image,a\posx,a\posy,0,wall1_sub1,imX+wall1sub1_xpos%,imY+wall1sub1_ypos%,0) And wall1_sub1_health# > 0
wall1_sub1_health#=wall1_sub1_health#-1
Text 0,220,"wall1 Sub 1 Collision "
Delete a
;wall1 Sub2
ElseIf ImagesCollide(a\image,a\posx,a\posy,0,wall1_sub2,imX+wall1sub2_xpos%,imY+wall1sub2_ypos%,0) And wall1_sub2_health# > 0
wall1_sub2_health#=wall1_sub2_health#-1
Text 0,240,"wall1 Sub 2 Collision "
Delete a
;wall1 Sub3
ElseIf ImagesCollide(a\image,a\posx,a\posy,0,wall1_sub3,imX+wall1sub3_xpos%,imY+wall1sub3_ypos%,0) And wall1_sub3_health# > 0
wall1_sub3_health#=wall1_sub3_health#-1
Text 0,260,"wall1 Sub 3 Collision "
Delete a
;wall1 Sub4
ElseIf ImagesCollide(a\image,a\posx,a\posy,0,wall1_sub4,imX+wall1sub4_xpos%,imY+wall1sub4_ypos%,0) And wall1_sub4_health# > 0
wall1_sub4_health#=wall1_sub4_health#-1
Text 0,280,"wall1 Sub 4 Collision "
Delete a
;wall1 Sub5
ElseIf ImagesCollide(a\image,a\posx,a\posy,0,wall1_sub5,imX+wall1sub5_xpos%,imY+wall1sub5_ypos%,0) And wall1_sub5_health# > 0
wall1_sub5_health#=wall1_sub5_health#-1
Text 0,300,"wall1 Sub 5 Collision "
Delete a
;wall1 Sub6
ElseIf ImagesCollide(a\image,a\posx,a\posy,0,wall1_sub6,imX+wall1sub6_xpos%,imY+wall1sub6_ypos%,0) And wall1_sub6_health# > 0
wall1_sub6_health#=wall1_sub6_health#-1
Text 0,320,"wall1 Sub 6 Collision "
Delete a
;wall1 Sub7
ElseIf ImagesCollide(a\image,a\posx,a\posy,0,wall1_sub7,imX+wall1sub7_xpos%,imY+wall1sub7_ypos%,0) And wall1_sub7_health# > 0
wall1_sub7_health#=wall1_sub7_health#-1
Text 0,320,"wall1 Sub 7 Collision "
Delete a
;wall1 Sub8
ElseIf ImagesCollide(a\image,a\posx,a\posy,0,wall1_sub7,imX+wall1sub8_xpos%,imY+wall1sub8_ypos%,0) And wall1_sub8_health# > 0
wall1_sub8_health#=wall1_sub8_health#-1
Text 0,320,"wall1 Sub 8 Collision "
Delete a
;wall1 Sub9
ElseIf ImagesCollide(a\image,a\posx,a\posy,0,wall1_sub7,imX+wall1sub9_xpos%,imY+wall1sub9_ypos%,0) And wall1_sub9_health# > 0
wall1_sub9_health#=wall1_sub9_health#-1
Text 0,320,"wall1 Sub 9 Collision "
Delete a
;wall1 Sub10
ElseIf ImagesCollide(a\image,a\posx,a\posy,0,wall1_sub7,imX+wall1sub10_xpos%,imY+wall1sub10_ypos%,0) And wall1_sub10_health# > 0
wall1_sub10_health#=wall1_sub10_health#-1
Text 0,320,"wall1 Sub 10 Collision "
Delete a
EndIf
Next
End Function

;__________________________________________________________________________________________________________________________________________
;IPPX and IPPY Speed Functions

Function moverealfast()
If KeyDown( 77 )
ippX=ippX+10 ;Number Pad 6
ElseIf KeyDown( 75 )
ippX=ippX-10 ;Number Pad 4
ElseIf KeyDown( 72 )
ippY=ippY-10 ;Number Pad 8
ElseIf KeyDown( 80 )
ippY=ippY+10 ;Number Pad 2
EndIf
End Function
Function movefast()
If KeyDown( 77 )
ippX=ippX+1 ;Number Pad 6
ElseIf KeyDown( 75 )
ippX=ippX-1 ;Number Pad 4
ElseIf KeyDown( 72 )
ippY=ippY-1 ;Number Pad 8
ElseIf KeyDown( 80 )
ippY=ippY+1 ;Number Pad 2
EndIf
End Function
Function moveslow()
If KeyHit( 77 )
ippX=ippX+1 ;Number Pad 6
ElseIf KeyHit( 75 )
ippX=ippX-1 ;Number Pad 4
ElseIf KeyHit( 72 )
ippY=ippY-1 ;Number Pad 8
ElseIf KeyHit( 80 )
ippY=ippY+1 ;Number Pad 2
EndIf
End Function




;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

;THE FOLLOWING FUNCTIONS ARE NOT IN THE GAME AT ALL[/bbcode]

code + images link:

http://d01.megashares.com/dl/wAbnDEM/Wall_Bricks_V_0.1.rar

basically the game reads a textfile depending on the level selected and loads a diffirent amount of destructible images at diffirent positions(the positions and amount of images are in the text file)
then draws them if the image health varible is greater then zero, then checks to see if the weapon fire and the destructible images collide, if they do than the health varible decreases.

example: level "wall" has 10 destructible pieces and level "wall2" only has 5


the problem I'm having is that its trying to draw images that don't exist and than checking them for collisions(I tried adding a If wall1pieces%>=5.ect. but it still tries to draw nonexistant images). I think could make it work right if I use Select and Case and make a case for each amount of images, but I think that it would play slower?

So I was seeing if I could draw images based off a specific varible?


Yasha(Posted 2012) [#4]
First thing, for very long pieces of code, please use the codebox markup option (there's also currently a bug in the bbcode markup option that causes it to display the wrong thing, so the code you posted is different from what appears in your post above, which never helps).

As for the answer... Select is unlikely to help you with this. Instead of having a global for each wall segment (globals are bad, keep their use to a minimum), and trying to manually write the code for execution of each draw call, you should be using a loop with an indexed container of some kind.

Short example with an array:

Const MAX_WALL_SEGMENTS = 1000

Local wallSub[MAX_WALL_SEGMENTS], wallSubHealth#[MAX_WALL_SEGMENTS]
Local wallSubCount = ;... get the number of wall segments from somewhere

Local i : For i = 0 to wallSubCount - 1
    wallSub[i] = LoadImage(level_name$+"\wallpieces\wall1_" + i + ".png"

Next

; ... later

For i = 0 to wallSubCount - 1
    If wallSubHealth[i] > 0 then DrawImage wallSub[i], x, y   ;.etc for X and Y
Next


All wall pieces under one name, and accessed by strictly controlled indices, with a measured cap. i.e.

So I was seeing if I could draw images based off a specific varible?


...yes. Like that.

Once you get this done, you'll also want to investigate custom types so you can define "a wall segment" as a single "thing" with image, health, x and y as properties belonging to it, and just store the one array containing those segments-as-things. This will simplify your life enormously.


Newbie31(Posted 2012) [#5]
Heyo, Me again. I changed my code as you suggested, and created an array to load the "wall1sub_xpos" and "wall1sub_ypos" but I when I select a level without 10 wallpieces I get the error "Array index out of bounds". I know that the error is because in the array I specify 10 indexes, but if there are only 5 images then it doesn't know what to do with the rest. I still don't understand how I can load a varible into a handle.

Example: What Ihave.
[bbcode]
Dim xpos(i)
xpos(1) = wall1sub1_xpos%
xpos(2) = wall1sub2_xpos%
xpos(3) = wall1sub3_xpos%
xpos(4) = wall1sub4_xpos%
xpos(5) = wall1sub5_xpos%
xpos(6) = wall1sub6_xpos%
xpos(7) = wall1sub7_xpos%
xpos(8) = wall1sub8_xpos%
xpos(9) = wall1sub9_xpos%
xpos(10) = wall1sub10_xpos%

Dim ypos(i)
ypos(1) = wall1sub1_ypos%;
ypos(2) = wall1sub2_ypos%
ypos(3) = wall1sub3_ypos%
ypos(4) = wall1sub4_ypos%
ypos(5) = wall1sub5_ypos%
ypos(6) = wall1sub6_ypos%
ypos(7) = wall1sub7_ypos%
ypos(8) = wall1sub8_ypos%
ypos(9) = wall1sub9_ypos%
ypos(10) = wall1sub10_ypos%


;later......

DrawImage wallSub[i],imX+xpos(i),imY+ypos(i)
[/bbcode]

Example: What I want.
[bbcode]
Dim xpos(i)
xpos(i) = "wall1sub"+i+"_xpos%"

Dim ypos(i)
ypos(i) = "wall1sub"+i+"_ypos%"

;later......

DrawImage wallSub[i],imX+xpos(i),imY+ypos(i)
[/bbcode]

Now my code looks like this.



Yasha(Posted 2012) [#6]
There are two visible problems in your code:

1) Why did you remove the "- 1" from the loops (should be after "wallSubCount")? If you have five objects, and you iterate through 0 - 5, you cover six things and one of them is there in error; the "- 1" wasn't a typo, it ensures that you only iterate through 0 - 4 for a total of five. If you want to count from 1 (as you are doing with the other array), then remove the "- 1", but also start the loop at 1 or whatever you put into slot 0 will be some nonexistent error thing.

2) You've correctly identified the location of the error. However, I don't think you're fully understanding the point of arrays here: by manually loading each value from a static variable into an array variable, you are negating half the point of having an array there in the first place. You're not doing any check to see that there are ten elements before lining up ten array offsets and sticking things in them. While a loop would conceptually help, it doesn't help you get at the static variables by index because that can't be done.

The point is, having the "wall1subN_xpos%" variables at all is causing your bug. If you have ten variables and use them that way, then you have no choice but to have ten elements in that wall. You need to completely get rid of the "static" parts of this program for the arrays to work as intended (e.g. you're even still reading ten position lines from a file that you've decided has only five wall segments).

In other words, it is right not to know what to do with the rest, because "the rest" shouldn't exist: you just told it there were five, and now there are ten anyway all of a sudden?

Instead, try this:

filein = ReadFile("_Data"+"\"+level_name$+"_Data.txt") 
	;select lines to read from
wall1pieces = Right(ReadLine( filein ),  2) ;wall1 Pieces
wall2pieces = Right(ReadLine( filein ), 1) ;wall2 Pieces

Dim xpos(wall1pieces), ypos(wall1pieces)

For i = 1 to wall1pieces
    xpos(i) = Right(ReadLine(filein), 4)
    ypos(i) = Right(ReadLine(filein), 4)
Next

CloseFile filein


There was no reason for any of those intermediate variables to exist at all. They were artificially constraining the valid shape of your data.

(A minor bug also in your above was to size the array with "Dim(i)" after a loop over i just finished: never rely on a loop iterator to have any specific value after a loop ends. Technically it's undefined: in practice it's predictable but it would have been the wrong number for your purposes anyway.)


Something that will cause you some grief in the future, that I should point out now, is that your program as it stands is making a lot of hardcoded assumptions about the shape of the config file. Not only the number of lines to read being fixed, but also the particular positions to the right or left in the text..? You might want to consider replacing this with a file parser from the code archives that can read more loosely-formatted data (there are several, to suit your choice of config file format).


Newbie31(Posted 2012) [#7]
I made the changes and it works now, yay.. I made changes to the datafile to try and make it neater, I prefer to save and load .txt files for my data because I can make alterations quickly in notepad. I haven't looked at file parsing yet but its on my to do list. I also altered where I get the wall1pieces varible to "MainMenu.bb".

This it what it looks like now:
:MainMenu.bb



wallbricks_V.0.2


Megashares Link: http://d01.megashares.com/dl/Oy1Danf/Wall_Bricks_V_0.2.rar

I'm not sure what you meant about the loop iterator being undefined after the loop ends?

Also I have question about the image collisions, in my code I use a function to update the weapon fire and check for collisions, a type within a function, but in the command reference under the Dim command it says that they must be used in the main program, does that mean that to access them in the function I have to reference them in the function brackets or create a new "for" loop within the function?


Yasha(Posted 2012) [#8]
Dim arrays must be defined in the main program; that is, they have to be first declared with Dim somewhere outside a function. This is just so that the compiler can see the name of the array in the global scope. You can access the array to set and get values anywhere you like, and you can resize it by calling Dim again within a function, just so long as it was defined once in the global area.

Dim arrays don't play very well with functions though, and there are some obscure bugs in the compiler (basically, make sure your initial declaration comes higher up in the source code than the function definitions that use it, or it may decide it doesn't recognise the array name). The other kind of arrays work much better with functions, but have their own limitations (as mentioned above).


I'm not sure what you meant about the loop iterator being undefined after the loop ends?


I mean that if you do this:

Local i : For i = 0 to 4
    ; ... stuff
Next
Print i


...technically you shouldn't make any assumptions about what number is printed there. In practice it will usually be 5 (but if you break out of the loop with Exit it may be something else), and much existing code relies on this, but it's a bad habit because in theory we don't know exactly what the loop does to i when it ends; the value of i is therefore only well-defined within the bounds of the loop structure, or after we give it a new value. The compiler would be within its rights to insert code that sets i to 10000000 there, for no reason at all; we're done with i after the loop.

(This doesn't matter very much in B3D, since as I mentioned above the loop actually is predictable in practice, and B3D is a stable implementation, and much code relies on these quirks; but it's a useful kind of thing to remember - "don't rely on undefined behaviour" - if you want to move on to other languages that aren't all so friendly, like C.)


Newbie31(Posted 2012) [#9]
I'm back again....and with a new problem.
I'm trying to get the images to check for collisions but I keep getting one of 3 diffirent errors.

If I have:
[bbcode]
play_missile(wallSub[i],xpos(i),ypos(i))
;,,,,
;Update Missiles Function
Function play_missile(wallSub[i],xpos(i),ypos(i))
For a.missile=Each missile
posizionex=a\posx
posizioney=a\posy
DrawImage a\image,a\posx,a\posy ;draw the missile image.
a\posy=a\posy-a\avanzamento ;this move the missile up
If posizioney=0 ;If missile is off screen delete it.
Delete a
Exit ;exit loop if missile is dead. will cause error as they's no missile to check
End If
;NON_MOVING IMAGES THAT COLLIDE WITH MISSILES , Detect collisions and determine damage
If ImagesCollide(a\image,a\posx,a\posy,0,wallSub[i],imX+xpos(i),imY+ypos(i),0)
Text 0,220,"Collision"
Delete a
EndIf
Next
End Function
[/bbcode]
I get the error : "Expecting ')'"

And if I have:
[bbcode]

play_missile(wallSub[i],imY,imY) ;this starts the missil anim
;,,,,,
;Update Missiles Function
Function play_missile(wallSub[i],imY,imY)
For a.missile=Each missile
posizionex=a\posx
posizioney=a\posy
DrawImage a\image,a\posx,a\posy ;draw the missile image.
a\posy=a\posy-a\avanzamento ;this move the missile up
If posizioney=0 ;If missile is off screen delete it.
Delete a
Exit ;exit loop if missile is dead. will cause error as they's no missile to check
End If
;NON_MOVING IMAGES THAT COLLIDE WITH MISSILES , Detect collisions and determine damage
If ImagesCollide(a\image,a\posx,a\posy,0,wallSub[i],imX+xpos(i),imY+ypos(i),0)
Text 0,220,"Collision"
Delete a
EndIf
Next
End Function
[/bbcode]
I get the Error:"Blitz Array sizes must be constant"

And If I have:
[bbcode]
play_missile()
;,,,,,
Function play_missile()
For a.missile=Each missile
posizionex=a\posx
posizioney=a\posy
DrawImage a\image,a\posx,a\posy ;draw the missile image.
a\posy=a\posy-a\avanzamento ;this move the missile up
If posizioney=0 ;If missile is off screen delete it.
Delete a
Exit ;exit loop if missile is dead. will cause error as they's no missile to check
End If
;NON_MOVING IMAGES THAT COLLIDE WITH MISSILES , Detect collisions and determine damage
If ImagesCollide(a\image,a\posx,a\posy,0,wallSub[i],imX+xpos(i),imY+ypos(i),0)
Text 0,220,"Collision"
Delete a
EndIf
Next
End Function
[/bbcode]
I get the Error: "Variable must be a Blitz array"

I'm not sure what the last one means, I thought that whenever I put wallSub[i] it would reference the array created at the top of the code?

Full Code:



Yasha(Posted 2012) [#10]
OK... you need to decide what you want the functions to do. In all three cases, you're doing some combination of trying to pass in whole arrays - collections of wall segments - when in each case they only actually operate on the one piece of wall. So really there's no need to have the array involved at all.

So in the first code example, the function call is what I would say is the correct version: you are accessing elements of the three arrays, and thus what you are actually passing to the function is three integers. Not three arrays: the values have been extracted. To make this version work, all you have to do is take the subscripts off your parameter declarations so that the function knows it is expecting three values which are not in any way arrays, like this:

Function play_missile(wallSub,xpos,ypos)
    ; ...
End Function


These three local integers now happen to have the same names as three arrays from the surrounding scope, so you may want to rename them for the sake of clarity (but it's not a problem for the compiler).


Your other two examples have a couple of errors of their own:

-- In the second example, the parameter "wallSub[ i ]" is telling the function to expect a whole array as argument, not just one element. Unfortunately "i" is not a constant, and the size of Blitz arrays must be constant, so this is not allowed. It also won't work with the calling line, since you're only passing it element i of the array, not the whole array.

-- In the third example, the array wallSub is not visible from inside the function because it is local to the main program. The Dim arrays are fine because Dim arrays are always also global, but Blitz arrays must be declared with global, or passed in as parameters.

(If you don't fully understand the implications of "local scope" yet, please read up on this before continuing. Scope is one of the most important and fundamental concepts in all of programming, and you must understand it in order to do well.)

Last edited 2012


Newbie31(Posted 2012) [#11]
Actually, I tried writing it like that early on.
[bbcode]
Function play_missile(wallSub,xpos,ypos)
;,,,,,
End Function
[/bbcode]

but I kept getting the error "Identifier 'xpos' may not be used like this". I assumed that the 'xpos' required the (i) after it to determine which array index it was using.

Also with the [i] array needing to be declared globally is it better than using Dim 'something'(i) array. Should all my arrays use [] brackets?


Yasha(Posted 2012) [#12]
I kept getting the error "Identifier 'xpos' may not be used like this".


Weird, I didn't get that. To be on the safe side you may as well give the parameter a different name. It'll be less confusing as well since the name won't overlap with the array name.


with the [i] array needing to be declared globally


There is no need for the array to be global (at least, not for this reason). You don't use the array in that function, only one element from it. Passing the element itself in, and forgetting all about the existence of the array while within the function, is a far better way to design that one.

As for whether you should use () or [] arrays, I would refer you to the link originally in my second post: http://www.blitzbasic.com/Community/posts.php?topic=98674#1154877

...i.e. They have different advantages and disadvantages, so the better option depends on the situation. (For my own part I like to think anything you can do with a Dim array, you can do better in other ways, but that requires more advanced techniques so don't worry about it right now.)


Newbie31(Posted 2012) [#13]
I've spent the last hour trying to rename the parameter, but I keep getting either "Identifier '????' may not be used like this" or "Illegal type conversion". I did have the collisions working for one image earlier but I stupidly changed something without backing up first, and now I just get those to errors. It makes me want to snap my computer.


Yasha(Posted 2012) [#14]
This may be a stupid question on my part, but how are you going about renaming the parameter? I obviously don't know how well you understand functions and it's a bit hard to guess from your posts.

But if you:

1) rename all three parameters in the function declaration line

2) make sure all references that were to the array value within the function are now to the renamed parameter, and

3) pass array elements, not whole arrays

... then you should avoid that error.


i.e. like this:

;Update Missiles Function
Function play_missile(wallSeg ,xp, yp)
	For a.missile=Each missile
		posizionex=a\posx
		posizioney=a\posy
		DrawImage a\image,a\posx,a\posy ;draw the missile image.
		a\posy=a\posy-a\avanzamento ;this move the missile up
		If posizioney=0 ;If missile is off screen delete it.
			Delete a
			Exit    ;exit loop if missile is dead. will cause error as they's no missile to check
		End If
                        ;NON_MOVING IMAGES THAT COLLIDE WITH MISSILES , Detect collisions and determine damage
		If ImagesCollide(a\image,a\posx,a\posy,0,wallSeg,imX+xp,imY+yp,0)
			Text 0,220,"Collision"
			Delete a
		EndIf
	Next
End Function

;Call thusly
play_missile(wallSub[i], xpos(i), ypos(i))


With no references to arrays at all, or attempting to shadow array names with locals (which may not work for xpos and ypos, because Dim arrays are bugged that way), there is no reason for it to object to your use of identifiers.


Newbie31(Posted 2012) [#15]
Ok that works, kind of. Think my mistake was leaving the (i) in the function play_missile(subWall,xpos(i),ypos(i)). When I take it out any rename works, kind of..

What happens now is the game loads fine and displays the wallsubs in the right position but when a missle is fired I get "Image does not exist" this is the same error I was getting a couple of days ago with the old code, which I attributed to the function trying to check collisions for an image that doesn't exist, so I tried removing the -1 from the for loop at the start.

[bbcode]
Const MAX_WALL_PIECES = 15
Global wallSub[MAX_WALL_PIECES], wallSubHealth#[MAX_WALL_PIECES]
Local wallSubCount =wall1pieces ;... get the number of wall segments from somewhere

Local i : For i = 0 To wallSubCount ;- 1
wallSub[i] = LoadImage(level_name$+"\wallpieces\wall1_" + i + ".png")
wallSubHealth[i] = 50
Next

; Open the file to Read
filein = ReadFile("_Data"+"\"+level_name$+"_Data.txt")


Dim xpos(wall1pieces),ypos(wall1pieces)


For i = 0 To wall1pieces ;- 1
xpos(i) = Right(ReadLine(filein), 4)
ypos(i) = Right(ReadLine(filein), 4)

Next

CloseFile filein
[/bbcode]
and then I don't get the error.........or any collisions. but anyway I will keep trying to find whats going wrong.


Newbie31(Posted 2012) [#16]
Turns out there are collisions are collisions there just not in the right place.(it wouldn't let me edit my previous post)


Newbie31(Posted 2012) [#17]
Turns out there are collisions there just not in the right place.(it wouldn't let me edit my previous post)


Yasha(Posted 2012) [#18]
I think at this point it might be sensible to take what you've learned about arrays, references, parameters, etc., and rewrite that loading and collision code from scratch, applying a few simplifications where you can see they would fit (e.g. you don't need two variables "wallSubCount" and "wall1pieces" to hold the same value). Trying to patch little fixes onto something you write before fully understanding the principle you;re now trying to use will result in very confusing code.


I would also like to take a moment to recommend you use the wonderful replacement editor IDEal. It'll auto-indent your code, syntax colour variables and functions, and point out undeclared variables and potential errors for you (plus many other features that will become more useful as you become more confident). Not to mention having an undo feature, so the next time you make a breaking change and don't know what it was, you don't have to worry about it!


Newbie31(Posted 2012) [#19]
Heyo, I'm back again. I finished retyping my code in IDEal, it came out pretty much the same. Anyway I'm still working on image collision, and I've incountered somthing strange(It doesn't really make sense to me).

I call my function like you suggested:
[bbcode]play_missile(wallSub[i],xpos(i),ypos(i),wallSubHealth[i])
;,,,,Later
Function play_missile(wallSub,xp,yp,heal)
For a.missile=Each missile
posizioneX=a\posx
posizioneY=a\posx
DrawImage a\image,a\posx,a\posy ;Draw the missile image.
a\posy=a\posy-a\misp ;This move the missile up
If posizioneY=0 ;If missile is off screen delete it.
Delete a
Exit ;Exit loop if missile is dead. will cause error as they's no missile to check
End If

;NON_MOVING IMAGES THAT COLLIDE WITH MISSILES , Detect collisions and determine damage
If ImagesCollide(a\image,a\posx,a\posy,0,wallSub,imX+xp,imY+yp,0) And heal>0
Rect imX+xp,imY+yp,ImageWidth(wallSub),ImageHeight(wallSub),0
wallSubHealth[i] = wallSubHealth[i] - 1
Text 0,220,"Collision " + wallSub
Delete a
EndIf
Next
End Function
[/bbcode]

I always asumed that the words in the function() were place holders for whatever was placed in the brackets when the function was called.

I thought that what my code above was the same as:


[bbcode]
play_missile(wallSub[i],xpos(i),ypos(i),wallSubHealth[i])
;,,,,Later
Function play_missile(wallSub,xp,yp,heal)
For a.missile=Each missile
posizioneX=a\posx
posizioneY=a\posx
DrawImage a\image,a\posx,a\posy ;Draw the missile image.
a\posy=a\posy-a\misp ;This move the missile up
If posizioneY=0 ;If missile is off screen delete it.
Delete a
Exit ;Exit loop if missile is dead. will cause error as they's no missile to check
End If

;NON_MOVING IMAGES THAT COLLIDE WITH MISSILES , Detect collisions and determine damage
If ImagesCollide(a\image,a\posx,a\posy,0,wallSub,imX+xpos(i),imY+ypos(i),0) And heal>0
Rect imX+xp,imY+yp,ImageWidth(wallSub),ImageHeight(wallSub),0
wallSubHealth[i] = wallSubHealth[i] - 1
Text 0,220,"Collision " + wallSub
Delete a
EndIf
Next
End Function
[/bbcode]
[I copied the code from IDEal, its making the indents look a little wacky its also adding a semicolon to "heal>0"]

but when I compile it the collisions occur at 2 diffirent positions.
Shouldn't the 2 pieces of code have the same effect?


Yasha(Posted 2012) [#20]
its also adding a semicolon to "heal>0"


That's a bug in the forum markup codes when you use the coloured code boxes. I prefer to use the uncoloured ones for this reason.


The code looks like it does the same thing to me, but perhaps I'm not seeing something.

In any case, you really need to decide how you want your design to work. Are you passing values into a function as parameters, or does the function have access to globals? You'll really find it so much easier if you stop mixing the two different methods of passing data.

Passing in the health as a value is obviously not going to work by itself because you can't modify the source of the value, which you've obviously noticed and are still using the global access point for wallSubHealth. What you can do to avoid this however is define a type describing wall segments, and pass in wall segments as single unified objects containing the x, y, and health properties - and which most importantly, are mutable in-place. You obviously already have some handle on this since you're using it for missiles, so... why not ditch the mess that is arrays altogether and group your data by use? You can still have the one array that just holds WallSub objects outside if you need a container for them, but you can ignore all of this trying to make different scopes work together and set values outside the function and all of that: within the function, just operate on the WallSub object.


Newbie31(Posted 2012) [#21]
I did think about making the wall segments into a TYPE when I started, but wasn't sure if it would work if my wallsegments were diffirent sizes(currently all the images are the same size). I thought that Types were best when drawing lots of one object, like weapon fire or enermy ships,etc.


. why not ditch the mess that is arrays altogether and group your data by use? You can still have the one array that just holds WallSub objects outside if you need a container for them, but you can ignore all of this trying to make different scopes work together and set values outside the function and all of that: within the function, just operate on the WallSub object.



I'm actually only a beginner and my use of types is really from the most basic tutorial I could find, as I use more functions and codes I do see alot of where I can shorten my code or do diffirent things, I'm still don't know to much about this.


Yasha(Posted 2012) [#22]
wasn't sure if it would work if my wallsegments were diffirent sizes(currently all the images are the same size). I thought that Types were best when drawing lots of one object, like weapon fire or enermy ships,etc.


Yes, it will work fine.

A field (as specified in a type) is exactly the same as a variable. It's merely a variable located in a dynamically created object, instead of a variable located in the main program stack. You can put anything into a field that you could put into a regular variable. So anything you could do with images normally, you can also do by storing them in an object. If you need extra data to manipulate the image, you can store that in the object too.


Newbie31(Posted 2012) [#23]
So, I've been busy for a couple of days, and only had a quick look at types but I figured I'd try to load my wallSubs into a TYPE, and have not been having much luck. I know how to make then work like a bullet, loading 1 image and updating it repeatedly. But I can't get the diffirent images to be positioned in there right spots, I think that all the images are there but they're being piled one ontop the other. I been calling the data from the array "wallSub[i]", within a FOR loop(I think...I'm not very good with types yet)

[bbcode]
Type wallchunk ;Create a TYPE called "wallchunk".
Field ch_xpos Field ch_ypos
Field theimage
End Type
;later,,,,,
;Draw Destructible Image Barriers
For i = 0 To wallSubCount - 1
wallbitdraw()
If wallSubHealth[i] > 0 And wallSubHealth[i] < 50 ;Checks If health is between 0 and 50
Rect imX+xpos(i)-10,imY+ypos(i)-10,ImageWidth(wallSub[i])+20,ImageHeight(wallSub[i])+20,0 ;draws a rectangle around the collidible image
EndIf
Next
;Later,,,,,,,
Function wallbitdraw(); Function that reads from my array and draws the wall pieces;
b.wallchunk = New wallchunk
b\ch_xpos = xpos(i)
b\ch_ypos = ypos(i)
b\theimage = wallSub[i]
DrawImage b\theimage,imX+b\ch_xpos,imY+b\ch_ypos
End Function
[/bbcode]

I tried creating a function to load images and then one to update there position but I kept getting that error about "Variable needs to be a blitz array" and I couldn't figure out how fix it so I went to this simpler version where the images are loaded and positioned in one function.

Could you recommend any tutorials about TYPES and arrays that would help me better understand?


Adam Novagen(Posted 2012) [#24]
Well, here's something that helped me understand it. Types are individual things; each Type is a thing unto itself that has a bunch of variables, all of which have the same name as the other Types, but contain something unique. For instance:

[bbcode]Type thing
Field name$
End Type[/bbcode]
Now, if you make three of those thing Types, all three of them will have a field (variable) which is named "name$" in your code, but each one will be whatever you set it to. Could be "thing 1," "thing 2," and so on. The name of the variable is the same, but it's not actually the same variable.

Now, here's the deal: all of those Types exist in a list, or stack, which is hidden from your view entirely. You don't need to think about it. What you do need to think about is that the list has a specific order, which generally goes from first-created Type to last-created, and every time you make a New Type, the list gets one entry bigger. In your code above, you're making new Types all the time, but they're never being deleted; this is called a "memory leak," because your program will gradually use more and more memory over time as it adds more and more Types to the list.

Let's try a sample program, to demonstrate Types with three balls (you can copy this into a new bb file and run it):



Hopefully this will clear some things up for you about Types and handles. A Type will remain in existence until it is deleted by you, so you should now be able to see how your wallbitdraw() function is causing a problem by creating Types endlessly and never deleting them. The power of Types is in creating many different things (but all of the same TYPE) that stick around as long as you want them, like bullets or spaceships or anything you need to persist for more than one frame.

Last edited 2012


RemiD(Posted 2012) [#25]
Adam>> I find some of your explanations very confusing, my understanding is not the same as yours, but maybe i am wrong. If so can you please explain with more details what you mean.


; Here's how this goes. Blitz is now holding a list of "ball" Types. There are three
; items in that list.


No. Blitz is now holding a list of objects of the type "ball". There are three objects of this type.




To GET to them, though, we need a variable. This is a lot like
; handles - when you load an image, you use a variable to store its handle, so that
; you can later tell Blitz "I want you do draw such-and-such an image here." Same deal
; with Types - the important difference is that the variable can contain the handle
; for each of the Types in the list, in turn, one at a time.


No, in Blitz there are pointers and handles but they are not the same thing.
The handles are used only with types and it is the objects count of all objects of all types.

See the difference :
Graphics3D(800,600,32,2)
HidePointer()
SetBuffer(BackBuffer())

Type TCharacter
 Field Name$
 Field Age%
 Field Energy#
 Field Mesh
End Type

Type TFood
 Field Name$
 Field EnergyValue#
 Field Mesh
End Type

C.TCharacter = New TCharacter
 C\Name = "Ashley"
 C\Age = 25
 C\Energy = 100.0
 C\Mesh = CreateCube()
 ;DebugLog("C = "+C)
 DebugLog("Handle(C) = "+Handle(C))
 DebugLog("Mesh pointer = "+C\Mesh)

C.TCharacter = New TCharacter
 C\Name = "Sydney"
 C\Age = 30
 C\Energy = 150.0
 C\Mesh = CreateCube()
 ;DebugLog("C = "+C)
 DebugLog("Handle(C) = "+Handle(C))
 DebugLog("Mesh pointer = "+C\Mesh)

C.TCharacter = New TCharacter
 C\Name = "Romeo"
 C\Age = 35
 C\Energy = 200.0
 C\Mesh = CreateCube()
 ;DebugLog("C = "+C)
 DebugLog("Handle(C) = "+Handle(C))
 DebugLog("Mesh pointer = "+C\Mesh)

F.TFood = New TFood 
 F\Name = "Apple"
 F\EnergyValue = 10.0
 F\Mesh = CreateCube()
 ;DebugLog("F = "+F)
 DebugLog("Handle(F) = "+Handle(F))
 DebugLog("Mesh pointer = "+F\Mesh)

F.TFood = New TFood 
 F\Name = "Banana"
 F\EnergyValue = 15.0
 F\Mesh = CreateCube()
 ;DebugLog("F = "+F)
 DebugLog("Handle(F) = "+Handle(F))
 DebugLog("Mesh pointer = "+F\Mesh)

F.TFood = New TFood 
 F\Name = "Raisin"
 F\EnergyValue = 12.5
 F\Mesh = CreateCube()
 ;DebugLog("F = "+F)
 DebugLog("Handle(F) = "+Handle(F))
 DebugLog("Mesh pointer = "+F\Mesh)

FlushKeys()
WaitKey()
End()

You can see the difference between a handle and a pointer here.
A handle for a Type in Blitz3d is like an index for a Dim array except the handle count is shared with all Type lists.



"b" is the variable that will act as the handle. ".ball" tells Blitz that b is now to
; be used as the handle for "ball" Types. In other words, b now contains the handle for
; whichever ball we choose.


Again no, see the example above, you can't see what "b" is because it is not the handle, it is the object of a type.



; This loop here, the FOR...EACH loop, does exactly what you think it does: it loops
; through EACH ball Type, one at a time. It starts at the first Type in the list, #1,
; and stores its handle in b. When the loop reaches "Next," it loops back around, takes
; the next Type in the list - #2 - and stores THAT handle in b. In this way, b ends up
; temporarily acting as a handle for every ball Type in the list, one at a time.


No, it loops through each object of the type "ball". Again b is not an handle, b represents the object of a type (the thing that holds all the variables and pointers)

Add this to the other code:
I% = 1

C.TCharacter = Object.TCharacter(I) 
 DebugLog("C\Name = "+C\Name)
 DebugLog("C\Age = "+C\Age)
 DebugLog("C\Energy = "+C\Energy) 

F.TFood = Object.TFood(I) 
 DebugLog("F\Name = "+F\Name)
 DebugLog("F\EnergyValue = "+F\EnergyValue)

If I% is equal to 1 or 2 or 3, it is possible to retrieve an object in the TCharacter list, else it porvokes an error.
If I% is equal to 4 or 5 or 6, it is possible to retrieve an object in the TFood list, else it porvokes an error.

This is why when working with several type lists, you need to name your colliders with their type and handle. Else, after you retrieve the infos of a collision or of a linepick, you will not be able to know in which list to search.
For example :
NameEntity(C\Mesh,Handle(C))
vs
NameEntity(C\Mesh,"CHA"+Handle(C))
(it is possible to do the same thing when working with objects stored in Dim arrays)

Please let me know what you think is true or false. I want to understand it correctly.

Thanks,

Last edited 2012


Yasha(Posted 2012) [#26]
RemiD, your post is right for the most part. There's a bit of a clash of terminologies here though: you're using the "correct" terminology as established by BlitzMax and every other language in the universe; Adam is using the Blitz3D Manual's terminology, which... is confusing and ambiguous because it refers to objects as "types", and intentionally avoids talking about pointers, and all of that sort of thing.

Personally I think using the universal convention, as you've been doing, is better, as the B3D convention is... ambiguous even within the context of B3D (if you use "Type" and "Handle" to refer to object and pointer... what are a syntactic type and handle? Don't even suggest dragging "kind" and "reference" into this - those mean different things yet again in general CS!).


You are wrong on one minor point though:

If I% is equal to 1 or 2 or 3, it is possible to retrieve an object in the TCharacter list, else it porvokes an error.
If I% is equal to 4 or 5 or 6, it is possible to retrieve an object in the TFood list, else it porvokes an error.


The Object command never raises an error, it just returns Null for invalid objects of the requested type. So you can actually test for the stored type of an int handle like this:

Select True
    Case Object.MyType1 hdl <> Null : Return "MyType1"
    Case Object.MyType2 hdl <> Null : Return "MyType2"
    Case Object.MyType3 hdl <> Null : Return "MyType3"
End Select


...or, more usefully, actually do something with it, but anyway you get the idea. This is how you're intended to implement polymorphic methods in B3D (otherwise, what would be the point of having Handle at all?).


RemiD(Posted 2012) [#27]

RemiD, your post is right for the most part. There's a bit of a clash of terminologies here though: you're using the "correct" terminology as established by BlitzMax and every other language in the universe; Adam is using the Blitz3D Manual's terminology, which... is confusing and ambiguous because it refers to objects as "types", and intentionally avoids talking about pointers, and all of that sort of thing.


Good for me.




(otherwise, what would be the point of having Handle at all?).


I don't know if this is a question or a statement so :

I often associate the handle (for types) or the index (for Dim arrays) of an object (the thing that holds all the variables and pointers) to a collider, also with its List name so that when i retrieve the EntityName() of the collider which has been collided or picked, i can retrieve its List name and its index (or handle).
With this, there is no need to use a For Next loop, i can use the index to search in the appropriate Dim array or the command Object.TType(Handle) to search in the appropriate Type and retrieve directly the object parameters.

Last edited 2012


Adam Novagen(Posted 2012) [#28]
I do understand that most of the terminology is wrong there. Since Newbie31 had no experience of Types whatsoever, though, I figured it'd be best to approach him as I wish I had been approached when I first started out, by keeping terms as simple and familiar as possible. He'll discover the details later on the road; back when I was at his level, explanations of pointers and objects and the Handle() command versus actual variable handles proved utterly unhelpful, and simply confused me even more.

Last edited 2012


Newbie31(Posted 2012) [#29]
Heyo,

I'm back again, I went back over my code and found the error that was making the images not appear in there correct places('I had forgot to declare a varible as global in my include file.'). Anyway I'm still having problems with detecting collisions between images with TYPES.

This is how I draw the images(this one works):
[bbcode]
Type wallchunk ;Create a TYPE called "wallchunk".
Field ch_xpos Field ch_ypos
Field theimage
Field imagehealth
End Type
;,,,,later
;Draw Destructible Image Barriers
wallbitdraw() ;this function draws the destructible image barrier

If wallSubHealth[i] > 0 And wallSubHealth[i] < 50 ;Checks If health is between 0 and 50
Rect imX+xpos(i)-10,imY+ypos(i)-10,ImageWidth(wallSub[i])+20,ImageHeight(wallSub[i])+20,0 ;draws a rectanlge around the collidible image
EndIf
;,,,,,later
Function wallbitdraw(); Function that reads from my array and draws the wall pieces;
For i = 0 To wall1pieces% - 1
b.wallchunk = New wallchunk
b\ch_xpos = xpos(i)
b\ch_ypos = ypos(i)
b\theimage = wallSub[i]
If wallSubHealth[i] > 0
DrawImage b\theimage,imX+b\ch_xpos,imY+b\ch_ypos
EndIf
If wallSubHealth[i] < 1
Delete b
EndIf
Next
End Function
[/bbcode]
I've put it in one function, I'm not sure if I need a second to update because the images don't move. The aim is to have them disappear when their health is depleted.

This is the code I'm trying to get working for collisions(doesn't work yet).
[bbcode]
Type wallchunk ;Create a TYPE called "wallchunk".
Field ch_xpos Field ch_ypos
Field theimage
Field imagehealth
End Type
;,,,,later
;Draw Destructible Image Barriers
wallbitdrawandcol() ;this function draws the destructible image barrier and checks for collisions.

If wallSubHealth[i] > 0 And wallSubHealth[i] < 50 ;Checks If health is between 0 and 50
Rect imX+xpos(i)-10,imY+ypos(i)-10,ImageWidth(wallSub[i])+20,ImageHeight(wallSub[i])+20,0 ;draws a rectanlge around the collidible image
EndIf
;,,,,,later
Function wallbitdrawandcol(); Function that reads from my array and draws the wall pieces and Checks for collisions
For i = 0 To wall1pieces% - 1
b.wallchunk = New wallchunk
b\ch_xpos = xpos(i)
b\ch_ypos = ypos(i)
b\theimage = wallSub[i]
b\imagehealth = wallSubHealth[i]
DrawImage b\theimage,imX+b\ch_xpos,imY+b\ch_ypos
If wallSubHealth[i] < 1
For b.wallchunk = Each wallchunk
If ImagesCollide(a\image,a\posx,a\posy,0,b\theimage,imX+b\ch_xpos,imY+b\ch_ypos,0)
b\imagehealth = b\imagehealth - 1
Text 0,220,"Collision "
Delete a
EndIf
Next
Delete b
EndIf
Next
End Function
[/bbcode]
I keep getting the error "Varible must be a type. Line: 290, Column: 4". Thats the line with "ImagesCollide"


My Full Code:


Code and Images Megashares link:
http://d01.megashares.com/dl/2k0jckt/Wall_Bricks_V_0.1%20-%20[IDEal].rar

Could you guys please have look and see what I've done wrong this time?


Adam Novagen(Posted 2012) [#30]
I keep getting the error "Varible must be a type. Line: 290, Column: 4". Thats the line with "ImagesCollide"
Well of course you're getting an error, silly. Look at your code:

[bbcode]If ImagesCollide(a\image[/bbcode]
Where'd the a\ come from? :o


Newbie31(Posted 2012) [#31]
I create a\image in the previous function(the one that creates the weapon fire).

[bbcode]
;Create Missiles Function
Function create_missile()
a.missile=New missile
a\image=jetfire
a\missile_life_time = 1500 ;Bullet last 2 seconds (2000 milliseconds)
a\missile_life_timer = MilliSecs() ;Set the timer to the same value as milliseconds,
a\posx=MouseX() ;The x position of missile creation
a\posy=a\posy+MouseY()-35 ;The missile creation starts a bit up from the jet
a\misp=4 ;The speed of the missile
a\animstrt=0 ;The starting value of the animation
End Function

;Update Missiles Function
Function play_missile(wallSub,xp,yp)
For a.missile=Each missile
posizioneX=a\posx
posizioneY=a\posx
DrawImage a\image,a\posx,a\posy ;Draw the missile image.
a\posy=a\posy-a\misp ;This move the missile up
If posizioneY=0 ;If missile is off screen delete it.
Delete a
Exit ;Exit loop if missile is dead. will cause error as they's no missile to check
End If
Next
End Function
[/bbcode]

I thought that I could use the handle "a\image" in any function after I created it, It won't let me make it global so I assumed it already was?