GLBasic forum

Main forum => GLBasic - en => Topic started by: Ian Price on 2008-Aug-18

Title: GLBasic equivilent of ReadInt?
Post by: Ian Price on 2008-Aug-18
This is going to sound really silly, as I've been loading and saving data with GLB for months, but I've just come across a problem.

I'm working on a new game where data has been stored in INTs (4bytes). The data was created in another language (BlitzMax) and can be read properly. The data is fully retrievable in PlayBASIC too.

However, despite successfully using ReadByte in my B'lox! game to hold the level data, I'm not able to read the INT data with GLBasic.

I've tried READLONG and READSHORTIEEE, both without success.

Is GLBasic unable to read proper INTs or am I braindead (it is 1:30 in the morning after all :P)

Code (glbasic) Select

SETSCREEN 640,480,0

GLOBAL sizex, sizey, number

load_data("game.dat")

WHILE TRUE

PRINT NUMBER,10,30
PRINT SIZEX,10,50
PRINT SIZEY,10,60

SHOWSCREEN

WEND

END


// Load data
FUNCTION load_data: filename$

ok=DOESFILEEXIST(filename$)

  IF ok
   
   OPENFILE (1,filename$,TRUE)

    READLONG 1, number
    READLONG 1, sizex
    READLONG 1, sizey
   
   CLOSEFILE 1

  ENDIF

ENDFUNCTION


The code above is virtually identical to the working BlitzMax and PlayBasic code. I'm only attempting to read the first 3 INTs, without success. The file is 32kb, so definitely holds more than 12bytes.

The results should be 2,80,50 IIRC.

Here's the data - http://www.iprice.remakes.org/stuff/game_dat.rar

Title: Re: GLBasic equivilent of ReadInt?
Post by: Hatonastick on 2008-Aug-18
To the best of my knowledge Ints are actually 2 bytes, not 4.  However I wouldn't trust what I remember. :P  I'd be giving READWORD a go as it sounds like an unsigned Int from my reading of GLBasic's documentation.
Title: Re: GLBasic equivilent of ReadInt?
Post by: Kitty Hello on 2008-Aug-18
No. ints on 32bit are 32 bits.
The rar contains an empty dat file?

What do you get with READLONG (which is the correct one in your case)?
Title: Re: GLBasic equivilent of ReadInt?
Post by: Ian Price on 2008-Aug-18
I've now got it working. Amazing what fresh eyes can do.  ;/

Sorry about the dead .dat file - GLB must have corrupted/wiped it and I didn't notice D'oh!

I've changed the code to read 100 values into an array and that displayed the values correctly. Strange though that my original code didn't. It also brought up "Probably unassigned variable for number, sizex and sizey" despite them being Global...
Title: Re: GLBasic equivilent of ReadInt?
Post by: Moru on 2008-Aug-18
Activate debugger, read the output and you will see that you have six variables, not three. If you change the casing on the variables NUMBER, SIZEX, SIZEY you will see differences. GL-Basic is cAsE SeNsItIvE
Title: Re: GLBasic equivilent of ReadInt?
Post by: Moru on 2008-Aug-18
Also, if you accidentaly write FALSE on the end of the OPENFILE command, it will wipe your file thinking it's an empty file you want to write to.
Title: Re: GLBasic equivilent of ReadInt?
Post by: Ian Price on 2008-Aug-18
Quote from: Moru on 2008-Aug-18
Activate debugger, read the output and you will see that you have six variables, not three. If you change the casing on the variables NUMBER, SIZEX, SIZEY you will see differences. GL-Basic is cAsE SeNsItIvE

Since when? I've never noticed this before. I don't normally use uppercase, and I don't even know why the variables were that way inclined - slip of the Caps Lock maybe? :/

I may well have accidentally used FALSE, hence the data wipeout. I was too tired to notice (not normally a problem at that time of night, but hey ho).

Tiredness has a lot to answer for.

Cheers to one and all :)
Title: Re: GLBasic equivilent of ReadInt?
Post by: Moru on 2008-Aug-18
Case sensitive? Probably from the start. At least since I started a year ago but what do I know? :-)

You keep deleting your posts faster than I can answer them so I have to edit again :-)
Title: Re: GLBasic equivilent of ReadInt?
Post by: Ian Price on 2009-Apr-01
Whilst this was working a while back, I'm getting problems with ReadLong again.

Has ReadLong changed in an update at all?

Gernot stated -
QuoteI might have to change the READLONG and other functions. No big deal, though.
in this thread - http://www.glbasic.com/forum/index.php?topic=2103.msg17282#msg17282

Did this change and if so, what's the correct usage now?


Simple code
Code (glbasic) Select

SETSCREEN 640,480,0

GLOBAL sizex=100, sizey=200, number=99

save_data("game.dat")

sizex=0
sizey=0
number=0

load_data("game.dat")

WHILE TRUE

PRINT number,10,30
PRINT sizex,10,50
PRINT sizey,10,60

SHOWSCREEN

WEND

END


// Load data
FUNCTION load_data: filename$

ok=DOESFILEEXIST(filename$)

  IF ok

   OPENFILE (1,filename$,TRUE)

    READLONG 1,number
    READLONG 1, sizex
    READLONG 1, sizey

   CLOSEFILE 1

  ENDIF

ENDFUNCTION

// Save data
FUNCTION save_data: filename$

OPENFILE (1,filename$,FALSE)

  WRITELONG 1, number
  WRITELONG 1, sizex
  WRITELONG 1, sizey

CLOSEFILE 1

ENDFUNCTION

Title: Re: GLBasic equivilent of ReadInt?
Post by: Kitty Hello on 2009-Apr-01
the READLONG and READSHORT, READYBYTE functions take an integer as the last parameter now, thus.

READLONG 1, mynumber%

or:
LOCAL mynumber%
READLONG 1, mynumber

or ( bestmost(c)(tm) )
LOCAL mynumber%
READLONG 1, mynumber%

Title: Re: GLBasic equivilent of ReadInt?
Post by: Ian Price on 2009-Apr-01
I thought I'd tried that. Obviously not. D'oh!

Cheers Gernot. :)