Page 1 of 2 12 LastLast
Results 1 to 20 of 25
Like Tree5Likes

Thread: JAVA storing objects

  1. #1
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default JAVA storing objects

    So is Java Serialization the best way to store huge amounts of objects? I mean like >1000000 objects.

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: JAVA storing objects

    My problem is that i want to be able to save "maps" for my game and one map is 1000x1000 tile objects + extra objects inside .... and these maps should be loaded at the start up of the game so I have to figure out a space efficient way to store them so they can be "stored with the game" and recreated at start up of the game ... Thanks for your time =)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: JAVA storing objects

    The objects are all the same ... the only thing that differs is the information. Ex points to different images , contains a sub object eg. a flower, stone ... etc

    ideas?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JAVA storing objects

    First off, I'm no expert in any of this, and so am only giving thoughts, not advice, but having said that, if the tiles were of limited type, i.e., grass, forest, rock, water, then you could store the map as an array of ints or something similar. If they can hold objects and only a minimum of them hold objects, then a sparse array (using collections) of some type could work. If all is complex then a database, and why not, since Java works quite well with databases.

  7. #7
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: JAVA storing objects

    The problem is they all have different attribues ... like walkable (boolean) ...spawn point (boolean) ... Im not sure how much information they need to have because i'm in a very early stage of the game ... By database do you mean mysql? because my goal is for this game to become an applet. (Currently working on a mapeditor to create the maps for the real game)

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: JAVA storing objects

    Check out ObjectDB. It's a commercial product, but their licensing terms are pretty lenient for educational or even small commercial projects. I think you can have up to 10 JPA @Entity classes and (presumably) unlimited @Embeddable classes with the free license.
    Get in the habit of using standard Java naming conventions!

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JAVA storing objects

    Another option is the Derby DB which is now part of Java and is free. It may now be called Java DB or something similar.

  10. #10
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: JAVA storing objects

    Thank you guys ... I'm still trying to figure out the best approach and your ideas are very helpful. If i would take the database approach ... how would that work with an applet. The server holds the database and the applet loads from it? Thanks again.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: JAVA storing objects

    I have no experience in the line, but search around for applet-servlet communication.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: JAVA storing objects

    Quote Originally Posted by santa View Post
    The problem is they all have different attribues ... like walkable (boolean) ...spawn point (boolean) ... Im not sure how much information they need to have because i'm in a very early stage of the game ... By database do you mean mysql? because my goal is for this game to become an applet. (Currently working on a mapeditor to create the maps for the real game)
    A lot of that sounds like something that belongs as general rules for the game.
    You only need to save the things that are not general rules, or Thing attributes.

    For example, it should be possible to recreate the 'walkable' flag simply from the type of terrain. Therefore there is no need to store that flag, just an id indicating the terrain type.
    I suspect spawn points are different, so you'll probably want to store that.
    Essentially pare it down to the minimum needed to allow the existing game structure to recreate the map, then see how big that is and then decide how would be best to store it.

    Assuming a tile is not all that complex a handful of bytes would do, mostly flags.
    Fubarable and kjkrum like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: JAVA storing objects

    Thanks for the advice !

    As it is now the "tile objects" only have one image the current background . But i want to be able to place stuff over it and then flatten it to one image ... ex each tile object contains an array of images first we have the background at [0] then if i want to place an object over it (ex a flower) then i place the flower in [1] and flatten the images to one image.

    I'm starting to think that this is the wrong approach because if a have a tree for example then it would stretch over multiple tiles ... so maybe its better to have all objects (trees,stones,leafs) on a different coordinate system and draw them as separate objects having nothing to do with the tiles. This way I can move behind trees and in front of them ...

    Anyway I checked gamedev.net and a lot of people there are using XML to store the maps ... Is that beneficial?

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: JAVA storing objects

    But the tree is centred on a single square.

    Think in terms of your squares, not images.
    You do not want to store the actual image (there's no game I can think of that does that) for part of a map.
    You want to store data that describes what is in that square.

    This goes back to your other thread on drawing.
    You have a background consisting of stuff that is underneath the stuff that moves...this probably include your flowers and rocks etc.
    You then have stuff that effects how your characters appear, in other words things like trees.
    IIRC older Settlers games would have two level trees, so the trunk and then the larger canopy.
    The trunk hid people behind them (ie higher up the screen), and the canopy hid things underneath it's spread.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: JAVA storing objects

    Quote Originally Posted by Tolls View Post
    But the tree is centred on a single square.

    Think in terms of your squares, not images.
    You do not want to store the actual image (there's no game I can think of that does that) for part of a map.
    You want to store data that describes what is in that square.

    This goes back to your other thread on drawing.
    You have a background consisting of stuff that is underneath the stuff that moves...this probably include your flowers and rocks etc.
    You then have stuff that effects how your characters appear, in other words things like trees.
    IIRC older Settlers games would have two level trees, so the trunk and then the larger canopy.
    The trunk hid people behind them (ie higher up the screen), and the canopy hid things underneath it's spread.
    But im thinking the advantages of having the objects on a coordinate system is that I can place them where ever I want (ex between 2 squares) . I'm not a good java programmer I just want to create a game for fun and basically your post is confusing for me. I'm not trying to store the image =P im trying to reference how to build it.

    So you are saying that I should have 2 different types of objects? objects that will be painted (flatten) with the background, and objects that affect the game should also be kept inside and have a internal position inside the tile object? so first i paint everything that can be walked over ... then i draw the trees etc?

    Sense I draw everything upside down and the "hero" is centered it will cover the hero if the hero is higher than the object .. (not including background) .. Thanks for you input =)

    Do you think that the XML would be better than other ways to store the map?

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: JAVA storing objects

    OK.
    I see now.
    It was the mention of tiles, which implies (in map work) essentially squares. The contents of the tile are often then defined in a Tile object, listing all the things in the tile itself. And that would be the thing that is saved.

    Anyway, if it's not tiled, and you're just painting some stuff wherever as an overlay say in order to make things look less regular and tiled, then I don't really know.

    The choice of how to save it will all depend on how you are building this thing. What it looks like, what it contains.

    Going back to the tiled idea, say you pick XML (which isn't a bad choice, but can be bulky, though easy to read), you'd have an XML structure for the Map containing Tile entries in it for each of the Tiles, which contains the data for that Tile. Now if there is an overlay (ie things that are not part of a tile, but are painted onto the map) then that would be saved as a section after the Tiles, consisting of XY coordinates and what the thing is.

    eg.
    Java Code:
    <Map>
       <Tile> ... tile data here or as attributes of Tile, up to you... </Tile>
       <Tile>...</Tile>
        etc
       <Overlay>
          <Thing>
             <Flower x=1 y=200/>
          </Thing>
          <Thing>
             <Pebble x=10 y=20/>
          </Thing>
          etc
    </Map>
    Note I have no idea what your game is or what your map has in it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: JAVA storing objects

    Well you are correct the game is "Tiled" . Basically then each tile object would contain a "list" of other objects and their offset within the tile? so when i draw the object i just draw everything that's in that list? This caused the cpu power to go from ~4 to ~10% =P

    Thanks for your help =) I will research a bit more about the storing part..

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: JAVA storing objects

    That's one way, but as I said I have no idea what you are doing beyond the most basic concept of a tiled map.
    I'm thinking in terms of Settlers, Civ, that sort of thing (ie fairly basic maps).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #19
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: JAVA storing objects

    Quote Originally Posted by Tolls View Post
    That's one way, but as I said I have no idea what you are doing beyond the most basic concept of a tiled map.
    I'm thinking in terms of Settlers, Civ, that sort of thing (ie fairly basic maps).
    Well that's what im doing =) Sense i don't know how many "objects" are going to be in each tile ... I'm guessing I should use an ArrayList. But if i remember correctly that does allocate more memory? Will that cause a problem when i do it in 1000000 objects?

    Thanks again

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: JAVA storing objects

    The more I read your posts, the more I feel that you are mixing the game state -- the model -- and its visual representation -- the view. Try to achieve a separation of the two, even if you don't go for full MVC.

    Then you only have to store the state, the model, which should be orders of magnitude smaller than storing the entire view + state. The view will walk the model and build itself.

    db
    Tolls and Fubarable like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 11-06-2011, 08:19 PM
  2. Replies: 6
    Last Post: 11-02-2011, 04:38 AM
  3. Storing objects in an array list?
    By Moedig in forum New To Java
    Replies: 3
    Last Post: 10-27-2011, 07:39 PM
  4. Storing objects
    By paul1024 in forum New To Java
    Replies: 5
    Last Post: 04-30-2011, 07:12 AM
  5. Storing objects directly with db4o
    By german in forum JDBC
    Replies: 0
    Last Post: 05-12-2009, 09:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •