Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Problem reading files

Author
Message
Juggleboy
11
Years of Service
User Offline
Joined: 17th Oct 2009
Location:
Posted: 15th Dec 2010 03:11
I'm working on a simple 3d game where you are constantly moving forward and have to avoid cubes. Not an original idea at all but I had to choose something within my skill level. I added the ability to save/load a single high score but as I continued working on the game, it wouldn't read the high score file correctly anymore. It reads the first part of the file, which is the name, just fine but not the actual score. The info is stored in strings because that was easiest for me. I checked over my code and I didn't notice anything wrong. I hadn't touched that section of the code at all. It seems to be working on and off. It's not a problem of writing the file, just reading. I can open it up in notepad and it has the correct info. What am I missing?
TheComet
13
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 15th Dec 2010 18:39
Give us da codez Then we will be able to help you out

TheComet

Juggleboy
11
Years of Service
User Offline
Joined: 17th Oct 2009
Location:
Posted: 15th Dec 2010 19:14
Oooops, why didn't i do that in the first place?

I am using this to write it



and this to read it

BN2 Productions
16
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 15th Dec 2010 20:47
Don't forget to close your file before trying to read from it a second time.

Other than that, what ^he^ said

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Latch
14
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 15th Dec 2010 23:17 Edited at: 16th Dec 2010 03:25
Quote: " `convert the high score string into an integer
hscore=val(hscore$)"


There's something that can be a little quirky with VAL(). VAL() always converts a string to a float value. When you assign an integer to a float, the type conversion is supposed to happen automatically, but I've had many cases when using VAL() where the resulting integer is undefined and doesn't return a value or returns zero all of the time. To solve this, I always try and declare integer from VAL() conversions like this:



Another thing to make sure of is that you are managing your directories properly between the time you created and saved the file to the time you load the file. You may end up dealing with 2 different files, or deleting and recreating a file you didn't intend.

Enjoy your day.
Juggleboy
11
Years of Service
User Offline
Joined: 17th Oct 2009
Location:
Posted: 16th Dec 2010 15:52
@ Latch
Well I tried that about the the VAL() command but it didn't solve my problem. I only need convert it to an integer so I can check if the player has beat the previous high score but even when I want to display the high score in the game's menu using the text command and the hscore$ variable it doesn't work. I did a little experimenting and printed hscore and hscore$. Only hscore showed up on the screen and it was 0. I double-checked the contents of the save file and the score was 73.
BN2 Productions
16
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 16th Dec 2010 18:53
Can you output hname$ correctly?

If this all fails, you can use the SAVE ARRAY and LOAD ARRAY commands and do it that way.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Juggleboy
11
Years of Service
User Offline
Joined: 17th Oct 2009
Location:
Posted: 16th Dec 2010 21:08
Yep. hname$ never stopped working. It was only hscore$ that didn't. I am displaying the high score in the menu like this



It looks like this when I try it.

High Score
player's name:
DVader
16
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 17th Dec 2010 17:11
This tutorial I did awhile back should work fine with DB classic. That's where I first started using the method. Since I have used it, I have never gone back to files. Might be worth you trying as you seem to having hassles ( a lot do) with files.
http://forum.thegamecreators.com/?m=forum_view&t=177546&b=1

http://s6.bitefight.org/c.php?uid=103081
Silverman
13
Years of Service
User Offline
Joined: 18th Jan 2007
Location: France
Posted: 17th Dec 2010 17:37
hi,

try these changes:



you get the same thing?

AMD Athlon(tm)XP 3200+ (2.2GHz) / DDR pc3200 (3x1024Mo) / Nvidia 6800GT (driver 197.45 WHQL)/ XP Pro SP3 / DirectX 9.0c (February 2010)/ DBClassic v1.2 / DBPro v7.4
Juggleboy
11
Years of Service
User Offline
Joined: 17th Oct 2009
Location:
Posted: 17th Dec 2010 23:07 Edited at: 18th Dec 2010 00:11
@Silverman
I tried your changes but it gave the same results. Not sure what going on there.

@DVader
Thanks! With the help of your tutorial I rewrote my code to use an array to store the info.

Well it works but occasionally a rectangle will appear at the end of the player's name. I guess that doesn't really have to do with my original question though.
DVader
16
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 18th Dec 2010 00:30
Not sure why you would get the end line symbol tacked on. That's what is sounds like. Excuse my poor syntax lol, I'm sure someone will know the exact terminology I am after. All I can suggest is you check your code where you enter your name. Try entry$ instead of input if you aren't already using it.

http://s6.bitefight.org/c.php?uid=103081
Juggleboy
11
Years of Service
User Offline
Joined: 17th Oct 2009
Location:
Posted: 18th Dec 2010 17:43
Well I figured out what my mistake was that caused the rectangle. I wasn't using the INPUT command because I wanted to have more control . I put the part that checks if the user had pressed return after checking for any key being pressed. That's what showed up on the end of the name. I just cut/pasted that part so it would come first.
Latch
14
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 20th Dec 2010 05:16
In regards to your original question, I can't see a reason why it wouldn't work unless your string value of the score had a leading non numeric character in it similar to what you are saying about the square above. If when your player was typing in name$, it is likely the carriage return was tagged onto the end of that as well. When you save the strings to the file, everything looks okay when you view the file. But, when it is read back, that extra carriage return is read in and throws off the strings read back.



What this code demonstrates is an extra carriage return (chr$(13)) added onto the end of monkey is saved in the file. When this is read back as a string, first 'monkey' gets read in and assigned to a$, then the rogue chr$(13) is read back but it isn't converted to a string in DB it ends up as NULL 0 . Then 5 is read back, then the end of the file as NULL . So, in your original code, I'm guessing there was an extra non printable character at the end of name$ that was causing behavior as described above.

As a test, using your original code read back all of the strings in the file and print them using a loop. I've included a while loop as an example:



In theory, you should see your hscore$ of 73 print out.

Enjoy your day.
Silverman
13
Years of Service
User Offline
Joined: 18th Jan 2007
Location: France
Posted: 24th Dec 2010 14:01 Edited at: 24th Dec 2010 14:03
Juggleboy, what is your version of DarkBasic classic?

try this snippet:


if you can see on the screen:

hname$= azerty
hscore$= 123
hscore= 123

then the problem is not in reading file, but in your code.

AMD Athlon(tm)XP 3200+ (2.2GHz) / DDR pc3200 (3x1024Mo) / Nvidia 6800GT (driver 197.45 WHQL)/ XP Pro SP3 / DirectX 9.0c (February 2010)/ DBClassic v1.2 / DBPro v7.4
Neodelito
15
Years of Service
User Offline
Joined: 29th Jul 2005
Location:
Posted: 30th Dec 2010 16:24
from upgrade 1.7 pass vars in this way don't work..
example.
you store in your file this number "666"
---------------------
...
read string 1,test$
...
number=val(test$)
print number
---------------------
this print "0"

In old version 68 of DarkBasic pro
work good
Print "666"

why?



Login to post a reply

Server time is: 2020-12-04 11:42:52
Your offset time is: 2020-12-04 11:42:52