Blitz3D Lesson4 Error

Blitz3D Forums/Blitz3D Beginners Area/Blitz3D Lesson4 Error

Jillinger(Posted 2014) [#1]
Hi, I seem to have messed up the lesson4.bb file, because Blitz3d demo does not undo changes, unfortunately, and it also apparently auto saves the file, so now the file is messed up.
However, I had created my own file, as I followed the lessons. I am getting this error: Memory Access Violation, and can't get it fixed. Can someone help please.
Here's the code:

;This is my lessons file
; Variables
Global escape_key = 1
Global screenW = 640, screenH = 480 ; create a Global variable for the game screen
Global run, run_vol#, runchannel ; Running sound effect handle and volume tags

Graphics3D (screenW,screenH,32,2) ; 32 bit color depth ; windowed
SetBuffer BackBuffer() ;

AmbientLight 200, 200, 200 ;add some Ambient light so that if the player gets too far from the point light source things won't go dark
Global myLight_01 = CreateLight(2)

; create single camera
Global myCam_01 = CreateCamera()
CameraViewport (myCam_01,0,0,screenW,screenH)

Include "spriteLoader.bb" ; Load/Use sprite functions
Include "soundFunctions.bb" ; INSERTS "soundFunctions.bb" file ; Load/Use sound functions

;FUNCTIONS
LoadMusic()
LoadSFX()
; Load world randomly with "tree" sprites using our NEW reuseable LoadRandomSprites() function
; Just give the sprite image, how many, and the X and Z range for random placement!
LoadRandomSprites ("tree.bmp", 500, -250, 250, -250, 250) ; call function

; Run a loop as long as no escap key is not pressed - so that I can see what happens
While Not KeyHit(escape_key)

; sprite control
; Use the sprite "RUN" sound volume to toggle sprite animation
If (run_vol# > 0) Then AnimSprite (sprChar, sprCharAnim_tex, 200, 4) ; execute function

UpdateWorld
RenderWorld

Flip
Wend

End


;This is the spriteLoader.bb file
; -----------------------------------------------;
; These functions are called from 'lesson003.bb' ;
;------------------------------------------------;

sprChar = CreateSprite()
sprCharAnim_tex = LoadAnimTexture ("char_back.bmp", 7, 32, 48, 0, 4)

EntityTexture (sprChar,sprCharAnim_tex,1) ; the 1 represents either the frame or the index (I'm not sure)
HandleSprite (sprChar,0,-1) ; Sets a sprite handle
ScaleSprite (sprChar,2,2)
PositionEntity (sprChar,0,0,10)

Function LoadRandomSprites (sprite$, total, x1, x2, z1, z2)
temp_sprite = LoadSprite (sprite$,7)
HandleSprite (temp_sprite,0,-1) ; Sets a sprite handle
EntityAutoFade (temp_sprite,100,120)
For s=1 To total ; total 500 trees
sx# = Rnd (x1,x2) : sz# = Rnd (z1,z2) ; randomize
PositionEntity (copied_sprite,sx,0,sz)
ScaleSprite (copied_sprite,Rand(2,3),Rand(4,6))
Next
FreeEntity (temp_sprite)
End Function

Function AnimSprite (obj, atex, d, f)
frame = MilliSecs () /d Mod f
EntityTexture obj,atex,frame
End Function


Jillinger(Posted 2014) [#2]
I managed to get that error sorted out. Apparently I placed this code:

; Load world randomly with "tree" sprites using our NEW reuseable
LoadRandomSprites() function
; Just give the sprite image, how many, and the X and Z range for random placement!
LoadRandomSprites ("tree.bmp", 500, -250, 250, -250, 250) ; call function

the wrong place. I put it back where it should be - at the beginning of the spriteLoader.bb file, and I get this error: 'Entity Does Not Exist'

on the HandleSprite (temp_sprite,0,-1) line.

Here is the code update:

;This is my lessons file
; Variables
Global escape_key = 1
Global screenW = 640, screenH = 480 ; create a Global variable for the game screen
Global run, run_vol#, runchannel ; Running sound effect handle and volume tags

Graphics3D (screenW,screenH,32,2) ; 32 bit color depth ; windowed
SetBuffer BackBuffer() ;

AmbientLight 200, 200, 200 ;add some Ambient light so that if the player gets too far from the point light source things won't go dark
Global myLight_01 = CreateLight(2)

; create single camera
Global myCam_01 = CreateCamera()
CameraViewport (myCam_01,0,0,screenW,screenH)

Include "spriteLoader.bb" ; Load/Use sprite functions
Include "soundFunctions.bb" ; INSERTS "soundFunctions.bb" file ; Load/Use sound functions

;FUNCTIONS
LoadMusic()
LoadSFX()
; Load world randomly with "tree" sprites using our NEW reuseable LoadRandomSprites() function
; Just give the sprite image, how many, and the X and Z range for random placement!
LoadRandomSprites ("tree.bmp", 500, -250, 250, -250, 250) ; call function

; Run a loop as long as no escap key is not pressed - so that I can see what happens
While Not KeyHit(escape_key)

; sprite control
; Use the sprite "RUN" sound volume to toggle sprite animation
If (run_vol# > 0) Then AnimSprite (sprChar, sprCharAnim_tex, 200, 4) ; execute function

UpdateWorld
RenderWorld

Flip
Wend

End


;This is the spriteLoader.bb file
; -----------------------------------------------;
; These functions are called from 'lesson003.bb' ;
;------------------------------------------------;
; Load world randomly with "tree" sprites using our NEW reuseable LoadRandomSprites() function
; Just give the sprite image, how many, and the X and Z range for random placement!
Global sprite$ = LoadSprite ("tree.bmp")
LoadRandomSprites (sprite, 500, -250, 250, -250, 250) ; call function

sprChar = CreateSprite()
sprCharAnim_tex = LoadAnimTexture ("char_back.bmp", 7, 32, 48, 0, 4)

EntityTexture (sprChar,sprCharAnim_tex,1) ; the 1 represents either the frame or the index (I'm not sure)
HandleSprite (sprChar,0,-1) ; Sets a sprite handle
ScaleSprite (sprChar,2,2)
PositionEntity (sprChar,0,0,10)

Function LoadRandomSprites (sprite$, total, x1, x2, z1, z2)
temp_sprite = LoadSprite (sprite$,7)
HandleSprite (temp_sprite,0,-1) ; Sets a sprite handle
EntityAutoFade (temp_sprite,100,120)
For s=1 To total ; total 500 trees
sx# = Rnd (x1,x2) : sz# = Rnd (z1,z2) ; randomize
PositionEntity (copied_sprite,sx,0,sz)
ScaleSprite (copied_sprite,Rand(2,3),Rand(4,6))
Next
FreeEntity (temp_sprite)
End Function

Function AnimSprite (obj, atex, d, f)
frame = MilliSecs () /d Mod f
EntityTexture obj,atex,frame
End Function


RemiD(Posted 2014) [#3]
Hello,

Firstly, if you have not already done that, when the Blitz3d IDE is open, in the menu, click on "Program" and make sure the "Debug Enabled" is activated, this will give you a better description of the error.

About the error "Entity Does Not Exist", it can mean that you try to access, to modify, to apply an action to an entity (pivot, mesh, sprite) which has not been created yet or which has not been loaded properly or which has already been freed.

To know if your entity exists at one place in the program, you can use Debuglog("")

For example :
Thing = LoadMesh("Thing.b3d") ;or CreateMesh() or LoadSprite("Thing.png") or CreateSprite() or CreatePivot()
Debuglog("Thing = "+Thing) ;if Thing is different from 0, it means it has been created or loaded then created properly

PositionEntity(Thing,0,0,5)


When you don't need an entity and you want to remove it from your world and from memory, instead of using only FreeEntity(Thing), you want to use :
FreeEntity(Thing)
Thing = 0

This is because if you need to debug your code later because you have an error in it and you only use FreeEntity(Thing), Thing will still contain the pointer number.

Example :
Graphics3D(800,600,32,2)
SetBuffer(BackBuffer())
SeedRnd(MilliSecs())

Camera = CreateCamera()

Thing = CreatePivot()
DebugLog("Thing = "+Thing)
FreeEntity(Thing)
DebugLog("Thing = "+Thing)
Thing = 0
DebugLog("Thing = "+Thing)

WaitKey()

EndGraphics()
End()



Jillinger(Posted 2014) [#4]
Thanks
I think that information will work for the basics, but this code is technical, as the lesson indicated.
For one thing I understand what
LoadRandomSprites ("tree.bmp", 500, -250, 250, -250, 250)
is supposed to do but, it does not seem like
temp_sprite = LoadSprite (sprite,7) ;
in the function is working.

I changed the code a bit

Global aSprite$ = LoadSprite ("tree.bmp")
LoadRandomSprites (aSprite, 500, -250, 250, -250, 250) ;

to see if that would fix the problem, but no such thing.
I also checked to make sure the image is fine, and it is.
I know the problem in in the string 'sprite$', but I can't figure out how to fix it.
I'd be glad if an expert out there could just use the code I posted and just help me fix it.


RemiD(Posted 2014) [#5]
After a quick look, here :
Function LoadRandomSprites (sprite$, total, x1, x2, z1, z2)
temp_sprite = LoadSprite (sprite$,7)
HandleSprite (temp_sprite,0,-1) ; Sets a sprite handle
EntityAutoFade (temp_sprite,100,120)
For s=1 To total ; total 500 trees
sx# = Rnd (x1,x2) : sz# = Rnd (z1,z2) ; randomize
PositionEntity (copied_sprite,sx,0,sz)
ScaleSprite (copied_sprite,Rand(2,3),Rand(4,6))
Next
FreeEntity (temp_sprite)
End Function


copied_sprite does not seem to be created anywhere.

Also if you plan to use copied_sprite = copyentity(temp_sprite), and then you use freeentity(temp_sprite) this will produce an error because each copied_sprite uses the temp_sprite as the "model"

A better approach would be to first create your prebuilt sprite (the one which will be used as the model) and then use copyentity to create many copies of it.


Jillinger(Posted 2014) [#6]
Thanks. I'll keep that information for if I need it later, but I found a work around and got the error with the trees fixed. Now all my trees are placed. Now I am getting an error on the character function, but I believe I can fix this with a few tweaks.
Thanks for all the suggestions.
Here's the code for getting the trees in:

; Load world randomly with "tree" sprites using our NEW reuseable LoadRandomSprites() function
; Just give the sprite image, how many, and the X and Z range for random placement!
aSprite$ = LoadSprite ("tree.png")
LoadRandomSprites (500, -250, 250, -250, 250) ; call function
; Set sprite view mode
SpriteViewMode aSprite,4 ; 4 sprite always remains upright. Gives a 'billboard' effect. Good for trees, spectators etc.

Function LoadRandomSprites (total, x1, x2, z1, z2)
temp_sprite = LoadSprite ("tree.png", 7) ; creates a sprite and textures it with whatever image we pass through sprite$ .
; Since we will be using a copy command this sprite (called temp_sprite) will only be used temporarily.
; The 7 is a commonly used flag For multiple image options... By Default, black background in any image becomes transparent.

HandleSprite (temp_sprite, 0,-1) ; Sets a sprite handle
EntityAutoFade (temp_sprite, 100, 120) ; cause an entity's alpha level to be adjusted at distances between near and far
; near# - distance in front of the camera at which entity's will start being faded
; far# - distance in front of the camera at which entity's will Stop being faded (And will be invisible)
; code above start to fade at 100 units and be invisible by 120 units. This helps prevent large levels with lots of objects to run faster

For s=1 To total ; total 500 trees
sx# = Rnd (x1,x2) : sz# = Rnd (z1,z2) ; randomize
; set x1 , x2 And z1 , z2 To -250, 250, And -250, 250 which will scatter our 500 trees in a 500 x 500 unit square centered in our level

copied_sprite = CopyEntity (temp_sprite) ; lets us quickly copy our previously loaded temp_sprite
; rather than waste time reloading the image 500 times off the hard drive!
PositionEntity (copied_sprite,sx,0,sz) ; an old command that now uses the randomly generated coordinates (sx,sz) to place things
ScaleSprite (copied_sprite,Rand(2,3),Rand(4,6)) ; different size trees no wider than 3, no higher than 6
; ScaleEntity works only for 3D objects. Sprites are flat and require their own scale command - ScaleSprite
Next
FreeEntity (temp_sprite) ; Since we don't need the temp_sprite any more we can save a little memory by freeing it up (deleting it)
End Function


RemiD(Posted 2014) [#7]
What i usually do when i want to create several entities which use the same model :
;i first load and create a prebuilt/prefab/model of the thing
Global ThingXSprite = LoadSprite("Thing.png")
SpriteViewMode(ThingXSprite,x)
ScaleSprite(ThingXSprite,1,1,1) 
HideEntity(ThingXSprite)

;then i create a list (with arrays) to store my things
Global ThingsCount% = 0
Dim ThingSprite(100)

;then i create my things and i store them in the list (in the arrays)
For i% = 1 to 100
 ThingsCount = ThingsCount + 1
 Id% = ThingsCount
 ThingSprite(Id) = CopyEntity(ThingXSprite)
 SXYZ#= rnd(0.5,1.5)
 ScaleEntity(ThingSprite(Id),SXYZ,SXYZ,SXYZ)
 PositionEntity(ThingSprite(Id),rnd(0,100),0,rnd(0,100)) 
Next

;then i can access/modify/update the things in the list
For Id% = 1 to ThingsCount
 MoveEntity(ThingSprite(Id),0.01,0,0)
Next



Jillinger(Posted 2014) [#8]
That looks like a good method for using Types. I'll try that. Thanks