Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help with J2ME RS  (Read 643 times)
0 Members and 1 Guest are viewing this topic.
Offline psycho_dad123

Junior Newbie





« Posted 2007-11-02 01:07:56 »

Hi all, new here and only been doing java for about a month. I am writing a blackjack game and it's almost done but I want to use a recordstore for the stats and am having a problem... Here is my code to read/write the RS

    private boolean readRS() {
        RecordStore rs;
        try {
            rs = RecordStore.openRecordStore("BlackJack", true);
            if (rs.getNumRecords() == 1) {
                System.out.println("loading settings");
      ByteArrayInputStream buffer = new ByteArrayInputStream(rs.getRecord(1));
      DataInputStream stream = new DataInputStream(buffer);
            
      long sv = stream.readLong();
      if (sv != this.SETTINGSVERSION) { //previous settings format is diferrent
                    rs.closeRecordStore();
                    wins = 0;
                    losses = 0;
                    pushes = 0;
                    blackJacks = 0;
                    bankRoll = 100000;
                    soundOn = true;
                    return writeRS(); //create new format;
                     //othewise it may cause a crash if we read different format
      }
            
      wins = stream.readInt();
                losses = stream.readInt();
                pushes = stream.readInt();
                blackJacks = stream.readInt();
                bankRoll = stream.readLong();
                soundOn = stream.readBoolean();
                rs.closeRecordStore();   
                System.out.println("Settings read...");
                return true;
            } else {
                wins = 0;
                losses = 0;
                pushes = 0;
                blackJacks = 0;
                bankRoll = 100000;
                soundOn = true;
                rs.closeRecordStore();               
                return writeRS();
            }   
       
        } catch (Exception e) {
       
            db(e.toString());
        }
        return false;
    }
   
    public boolean writeRS() {
        RecordStore rs;
   try {
            rs = RecordStore.openRecordStore("BlackJack", true);
            ByteArrayOutputStream buffer=new ByteArrayOutputStream();
            DataOutputStream stream=new DataOutputStream(buffer);
         
            stream.writeLong(SETTINGSVERSION);
            stream.writeInt(wins);
            stream.writeInt(losses);
            stream.writeInt(pushes);
            stream.writeInt(blackJacks);
            stream.writeLong(bankRoll);
            stream.writeBoolean(soundOn);
           
            //if (rs.getNumRecords() == 1)
      rs.setRecord(1, buffer.toByteArray(), 0, buffer.size());
       //else
       //   rs.addRecord(buffer.toByteArray(), 0, buffer.size());
         
            rs.closeRecordStore();               
       
            System.out.println("Settings written...");
           
        } catch (Exception e) {
       
            db(e.toString());
            return false;
        }
        return true;
                 
    }



 I am sure there is some basic thing I am missing but, it has been driving me nuts for 2 days Sad

Thanks for any help you can give me

Kevin
Offline psycho_dad123

Junior Newbie





« Reply #1 - Posted 2007-11-02 01:19:27 »

I forgot to say what the actual problem is.... When the program loads, it should read the stats (if they exist) and default to 0's if not. But, for some reason, the stats always start at 0 if they exist or not.

Kevin
Offline SimonH
« Reply #2 - Posted 2007-11-02 04:04:24 »

It's a while since I did any J2ME but I don't remember any input/outputstreams... try setRecord() & getRecord() & save your data as bytes. I think some phones don't like recordIDs which != 1... or was it 0?  Huh

People make games and games make people
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SimonH
« Reply #3 - Posted 2007-11-02 04:16:32 »

Just checked - it's 1.

Here's working code;

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
   /***************************************************************************
    **************************************************************************/

   public void saveGameState()
   {
      byte[] data = new byte[1024];
     
      // insert save data into byte array here, ie data[0]=(byte)(anIntVariable&255);data[1]=(byte)((anIntVariable>>8)&255); &c
     
      try
      {
         RecordStore rs = RecordStore.openRecordStore("myRMS", true);
         try {
            rs.setRecord(1, data, 0, data.length);
         } catch (RecordStoreException ex) {
            rs.addRecord(data, 0, data.length);
         }
         rs.closeRecordStore();
      }
      catch (RecordStoreException ex)   {
      }
   }
   /***************************************************************************
    **************************************************************************/

   public void loadGameState()
   {
      try
      {
         RecordStore rs = RecordStore.openRecordStore("myRMS", true);
         byte[] data = rs.getRecord(1);
         
         // get data from byte array here
       
         rs.closeRecordStore();

      }
      catch (RecordStoreException ex)   {
         
         //set default values here;
       
      }
   }

People make games and games make people
Offline psycho_dad123

Junior Newbie





« Reply #4 - Posted 2007-11-02 05:02:30 »

I'll give it a shot. Thanks a million for the help.

Kevin
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (44 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (62 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (39 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!