Reading/Writing & Types

Blitz3D Forums/Blitz3D Beginners Area/Reading/Writing & Types

That's TWICE now I've been caught out by the strange 'quirkiness' of ReadLine/Int etc. and Types. It's just annoying how I have been struggling with all sorts of other code until I finally remembered about this issue, so I thought Id post here to help anyone else that may have the same difficulty in the future!

Blitz will not read the data whilst trying to populate the Type field.

i.e


File=OpenFile ("Myfile.txt")

NewType.MyType = New MyType

NewType\FieldX=ReadInt (File)

CloseFile File



Will result in NewType\FieldX=0 regardless of the data in the file.

I have had to overcome this by a very simple workaround of:


File=OpenFile ("Myfile.txt")

NewType.MyType = New MyType

temp=ReadInt (File)

NewType\FieldX=temp

CloseFile File



maybe can't create a type inside a file read/write
operation?

Im not sure, but I think it maybe to do with the order that Blitz compiles things (i.e. Types first), so it tries to populate the type fields before the values are resolved, effectively filling them with '0's as other undeclared vars...

not sure though ;)

Glad you mentioned this - just about to start doing similar.

Odd, reading values directly into type fields works fine here. :/

Odd, reading values directly into type fields works fine here. :/



Works fine here as well.

Heh... never worked for me, even from v1.83

This works on 1.90
Type mytype
  Field x
End Type
File=OpenFile ("Myfile.txt")
WriteInt(file,5)
CloseFile file
file=ReadFile("myfile.txt")

NewType.MyType = New MyType

NewType\x=ReadInt(File)

CloseFile File
Print newtype\x


Okay, well - I seem to have another problem then...

I believe it's connected to Global Types. I think I'm not declaring the Type as global properly so that when I come to populate it within the function, the main program doesn't record the instance...


My function is:

Function Loadship(shipname$,scale#,pitch,yaw,roll,shininess#,alpha#,x,y,z)

Decal=OpenFile("Visual\3D\Ships"+shipname$+"\Decals.DAT")

Dim a(24)

For f=1 To 24
	temp=ReadInt(Decal)
	a(f)=temp
Next
	
CloseFile(Decal)

LoadDecal.Shiplights = New shiplights

LoadDecal\FS_Sprite=CopyEntity (LightSprite)
LoadDecal\FS_x=a(1)
LoadDecal\FS_y=a(2)
LoadDecal\FS_z=a(3)

LoadDecal\FP_Sprite=CopyEntity (LightSprite)
LoadDecal\FP_x=a(4)
LoadDecal\FP_y=a(5)
LoadDecal\FP_z=a(6)

LoadDecal\U_Sprite=CopyEntity (LightSprite)
LoadDecal\U_x=a(7)
LoadDecal\U_y=a(8)
LoadDecal\U_z=a(9)

LoadDecal\T_Sprite=CopyEntity (LightSprite)
LoadDecal\T_x=a(10)
LoadDecal\T_y=a(11)
LoadDecal\T_z=a(12)

LoadDecal\AP_Sprite=LoadSprite("Visual\3D\Ships"+shipname$+"\Aerial Port.bmp")
LoadDecal\AP_x=a(13)
LoadDecal\AP_y=a(14)
LoadDecal\AP_z=a(15)

ScaleSprite LoadDecal\AP_Sprite,(scale#/10),(scale#/10)

LoadDecal\AS_Sprite=LoadSprite("Visual\3D\Ships"+shipname$+"\Aerial Starboard.bmp")
LoadDecal\AS_x=a(16)
LoadDecal\AS_y=a(17)
LoadDecal\AS_z=a(18)

ScaleSprite LoadDecal\AS_Sprite,(scale#/10),(scale#/10)

LoadDecal\AT_Sprite=LoadSprite("Visual\3D\Ships"+shipname$+"\Aerial Top.bmp")
LoadDecal\AT_x=a(19)
LoadDecal\AT_y=a(20)
LoadDecal\AT_z=a(21)

ScaleSprite LoadDecal\AT_Sprite,(scale#/10),(scale#/10)

LoadDecal\H_Sprite=CopyEntity (LightSprite)
LoadDecal\H_x=a(22)
LoadDecal\H_y=a(23)
LoadDecal\H_z=a(24)

		logfile("GRAPHICS: POPULATED DECAL TABLE")

	
	mesh=LoadMesh("Visual\3D\Ships"+shipname$+"\Mesh.x")
	
	logfile("GRAPHICS: LOADED MESH "+mesh)
	
	texture=LoadTexture("Visual\3D\Ships"+shipname$+"\Texture.bmp")
	
	logfile("GRAPHICS: LOADED TEXTURE "+texture)
	
	ScaleMesh mesh,scale#,scale#,scale#
	
	logfile("GRAPHICS: SCALED MESH")
	
	EntityTexture mesh,texture
	
	logfile("GRAPHICS: PAINTED MESH WITH TEXTURE")
	
	temp=texture
	
	FreeTexture texture
	
	logfile("GRAPHICS: TEXTURE "+temp+" FREED")
	
	EntityShininess mesh,shininess#
	
	logfile("GRAPHICS: ADDED LIGHT REFLECTION TO MESH "+mesh)
	
	EntityAlpha mesh,alpha#
	
	logfile("GRAPHICS: SET TRANSPARENCY LEVEL TO MESH "+mesh)
	
	EntityType mesh,1
	
	PositionEntity mesh,x,y,z,1

	logfile("GRAPHICS: POSITIONED MESH "+mesh+ "AT X:"+x+" Y:"+y+" Z:"+z)
	
	PositionEntity LoadDecal\FS_Sprite,x,y,z
	PositionEntity LoadDecal\FP_Sprite,x,y,z
	PositionEntity LoadDecal\AP_Sprite,x,y,z
	PositionEntity LoadDecal\AS_Sprite,x,y,z
	PositionEntity LoadDecal\AT_Sprite,x,y,z
	PositionEntity LoadDecal\U_Sprite,x,y,z
	PositionEntity LoadDecal\T_Sprite,x,y,z
	PositionEntity LoadDecal\H_Sprite,x,y,z
	
MoveEntity LoadDecal\FS_Sprite,(FS_x*scale#),(FS_y*scale#),(FS_z*scale#)
EntityParent LoadDecal\FS_Sprite,mesh,1

MoveEntity LoadDecal\FP_Sprite,(FP_x*scale#),(FP_y*scale#),(FP_z*scale#)
EntityParent LoadDecal\FP_Sprite,mesh,1

MoveEntity LoadDecal\U_Sprite,(U_x*scale#),(U_y*scale#),(U_z*scale#)
EntityParent LoadDecal\U_Sprite,mesh,1

MoveEntity LoadDecal\T_Sprite,(T_x*scale#),(T_y*scale#),(T_z*scale#)
EntityParent LoadDecal\T_Sprite,mesh,1

MoveEntity LoadDecal\AP_Sprite,(AP_x*scale#),(AP_y*scale#),(AP_z*scale#)
EntityParent LoadDecal\AP_Sprite,mesh,1

MoveEntity LoadDecal\AS_Sprite,(AS_x*scale#),(AS_y*scale#),(AS_z*scale#)
EntityParent LoadDecal\AS_Sprite,mesh,1

MoveEntity LoadDecal\AT_Sprite,(AT_x*scale#),(AT_y*scale#),(AT_z*scale#)
EntityParent LoadDecal\AT_Sprite,mesh,1

MoveEntity LoadDecal\H_Sprite,(H_x*scale#),(H_y*scale#),(H_z*scale#)

EntityParent LoadDecal\H_Sprite,mesh,1

	logfile("GRAPHICS: PLACED DECALS RELATIVE TO "+(H_x*scale#)+" , "+(H_y*scale#)+" , "+(H_z*scale#))
	
		RotateEntity mesh,pitch,yaw,roll
	
	logfile ("GRAPHICS: SET MESH "+mesh+" FACING P:"+pitch+" Y: "+yaw+" R:"+roll)
	
Return mesh

End Function



and my Type declaration goes very simply like this:

Global ShipLights

Type ShipLights

Field FS_Sprite
Field FS_x
Field FS_y
Field FS_z

Field FP_Sprite
Field FP_x
Field FP_y
Field FP_z

Field U_Sprite
Field U_x
Field U_y
Field U_z

Field T_Sprite
Field T_x
Field T_y
Field T_z

Field AP_Sprite
Field AP_x
Field AP_y
Field AP_z

Field AS_Sprite
Field AS_x
Field AS_y
Field AS_z

Field AT_Sprite
Field AT_x
Field AT_y
Field AT_z

Field H_Sprite
Field H_x
Field H_y
Field H_z

End Type


Gah, see below - double posted myself!!

You should consider outputting debuglog or text statements as you type fields get populated. At least you'll know whether they are set within the function or not.
If they are set in the function and the debuglog statements suggest it's 0 outside then it's likely to be a global
issue.
BTW : Isn't it the type handle that needs to be global and not the type name?
Type mytype
  Field x
End Type
Global newtype.mytype
writing()
reading()
Print "Out function : " + newtype\x
WaitKey()
Function writing()
	File=WriteFile ("Myfile.txt")
	WriteInt(file,5)
	CloseFile file
End Function
Function reading()
	file=ReadFile("myfile.txt")
	NewType.MyType = New MyType
	NewType\x=ReadInt(File)
	CloseFile File
	Print "In function : " + newtype\x
End Function


I think you may have fallen for the common misunderstanding of how TYPES work in Blitz (I know I did!).

Whenever you create a new instance of a TYPE ie.

(LoadDecal.Shiplights = New shiplights)

then the instance is ALWAYS global. Each instance of a TYPE you create is global and exists in your main routine and all functions.

However!!! The pointer to that instance ('LoadDecal') is only local to your function. If you want to access this instance in your main code or another function, you will need to get a new pointer to that instance!

Try doing this in your main code after (and maybe before!) calling your function:

for test.shiplights = each shiplights
;test some 'type' data here
next

Hope that helps a bit,
Rhy :)

Well, in the Globals tab on the right of the 'debug' window, Shiplights always = 0 that's how I know it's not being populated....

But thanks, now that you mention it, Types have to be global because they can be iterated from wherever within a program!

I think the debug window only shows you the current instance?? Not sure, cos I use Protean IDE. What does it show if you put a stop in your function after populating the type?

Rhy :)


Well, in the Globals tab on the right of the 'debug' window, Shiplights always = 0 that's how I know it's not being populated....

but the debuglog/text messages would have suggested that shiplights wasn't the problem. Never mind.

DOH!!!

Sussed it! In my PositionEntity commands, I forgot to enter 'MyType\.....'
resulting in the Compiler assuming null values for undeclared variables such as 'My_Field' etc. etc.

Changed to 'MyType\My_Field' and it's working fine! :)

ah! the infamous "i forgot to do this" trick! :)

rem: if you get a coding problem,
break it down to the simplest example
possible, test that out, make sure you
know whats going wrong,
(and of course how to fix it!), then go
back to your big code and add it.

saves alot of hassle.

The biggest problem I have, is where "Galactic" has grown into a modular project with all the functions and routines etc. split across several files for 'Include' this is great for adding/changing bits, but very difficult to test small portions as so much relies on prior configurations. It's a shame that the compiler cannot be fed values at run-time, i.e. set a load of variables. I am currently trying to implement a debugging tool which will essentially be a copy of part of the code which will read relevant data from an editable file...

It's a lot of work, and its tough because I wanna get back on with the flight engine :)

thanks for the help & advice!!!

Ahh. The blitzmax strict command would be handy to have in Blitz3D :)