memory access violation

Blitz3D Forums/Blitz3D Programming/memory access violation

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

As others suggested, running the debug helps recognizing the issue.

First of all, be aware that the functions in the castle demo are not as reusable as they could seem since there are references in them which point to global variables containing the required 3d models.

The first issue was noticed by markcw...
In the CreatePlayer() function you are referring to an entity which doesn't exist.

In order to call the entity from within the function you could either associate an entity to the global variable 'player_model' (which would make the function less modular and not reusable) or add a parameter which asks for the player model:
Function CreatePlayer.Player(player_model, x#, y#, z#)
	
	p.Player=New Player
	p\entity=CreatePivot()
	p\model=CopyEntity( player_model,p\entity )
	p\player_y=y
	PositionEntity p\entity,x,y,z
	EntityType p\entity,TYPE_PLAYER
	EntityRadius p\entity,1.5
	ResetEntity p\entity
	Return p

End Function


There is also a similar problem within the skybox function, which looks for the 'sky' entity which doesn't exist.

The skybox is created by the function Skybox(file$). However, the function doesn't return anything (in that case the mesh 'm'), so it doesn't output the required 3D skybox model:

Function skybox(file$)
	
	m=CreateMesh()
	;front face
	b=LoadBrush( file$+"_FR.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,-1,0,0:AddVertex s,+1,+1,-1,1,0
	AddVertex s,+1,-1,-1,1,1:AddVertex s,-1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;right face
	b=LoadBrush( file$+"_LF.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,+1,+1,-1,0,0:AddVertex s,+1,+1,+1,1,0
	AddVertex s,+1,-1,+1,1,1:AddVertex s,+1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;back face
	b=LoadBrush( file$+"_BK.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,+1,+1,+1,0,0:AddVertex s,-1,+1,+1,1,0
	AddVertex s,-1,-1,+1,1,1:AddVertex s,+1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;left face
	b=LoadBrush( file$+"_RT.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,+1,0,0:AddVertex s,-1,+1,-1,1,0
	AddVertex s,-1,-1,-1,1,1:AddVertex s,-1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;top face
	b=LoadBrush( file$+"_UP.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,+1,0,1:AddVertex s,+1,+1,+1,0,0
	AddVertex s,+1,+1,-1,1,0:AddVertex s,-1,+1,-1,1,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;bottom face	
	b=LoadBrush( file$+"_DN.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,-1,-1,1,0:AddVertex s,+1,-1,-1,1,1
	AddVertex s,+1,-1,+1,0,1:AddVertex s,-1,-1,+1,0,0
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	ScaleMesh m,100,100,100
	FlipMesh m
	EntityFX m,1
	return m

End Function


Then, in the SetUp() function you call the CreateMainCamera() function which in turn needs a valid skybox model (which again doesn't exist). It looks like you are creating the skybox in the Landscape() function which is located in the next include... however, the skybox is needed before the CreateMainCamera() function.

So, for the sake of simplicity, I removed the lines related to the skybox creation from the Landscape() function, and put them in the setup() function right before the CreateMainCamera(). By now, I have also added a call to the character model from the Castle demo to the CreatePlayer() function call:

Function SetUp()
	Graphics3D 800, 600, 32, 0
	Player1.Player = CreatePlayer(LoadAnimMesh("MARIORUN.X"),0,10,0)

	sky=skybox( sky$ )
	EntityOrder sky,10
	HideEntity sky

	Player1Camera.MainCamera = CreateMainCamera(Player1\entity)
End Function


The "world.bb" include must be put before the "setup.bb" include.

Now remove the 'sky' parameter from the Landscape() function, and compile.


It's absolutely not the best way to work (I was just ironing the bugs out), but at least it runs.


Be very aware of the use globals, types and where to put includes before trying to achieve a modular framework: in particular avoid to call global variables from within functions (use parameters instead), and always call functions which you have already included before.