Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Saving Inventory Items  (Read 554 times)
0 Members and 1 Guest are viewing this topic.
Offline samcp12

Junior Member


Medals: 1



« Posted 2013-05-10 02:55:08 »

Hello, I currently creating a small rpg in java, more to see what i can achieve rather than trying to make a serious game, nonetheless I've reached a problem.

I am attempting to save the items (stored in an item array) in my players inventory to a file so that when loaded the same items as before are still there, unfortunately when creating an item it takes a buffered image as the "Icon" when displayed in the inventory and as I was trying to save through serialization, it failed and realized Buffered Images could not be used, is there any other image type I could use that would still allow for the same functionality?. I've done some research but can't find a very strong answer on how to solve this.

Any advice would be great, I am not looking for code to be spoon fed to me just a hint in the right direction would be amazing.

Cheesy
Offline Fsig

Senior Newbie





« Reply #1 - Posted 2013-05-10 03:39:22 »

Why are you trying to save the image to a file?

Just save a value which tells you what image to load.

If you must save the image to a file then first convert it to Base64 and get the string. Save this to a file. When loading, convert the Base64 back to bytes then you can use a input stream from you byte array to load a image.

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #2 - Posted 2013-05-10 03:53:27 »

Just save a value which tells you what image to load.
+1

You don't need to save the actual items as they exist in the game. You just have to save codes/values that you can interpret upon loading so that you can rebuild the inventory the way that it was saved.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #3 - Posted 2013-05-10 03:58:35 »

This might be difficult to do if you don't have your game already programmed this way, but how I see it off the top of my head is that maybe each item is its own class and inside each class is what image should be used for that item.

Then maybe you could assign a code for each item that is also stored inside the class and you save that code to a file when you want to save the inventory, so that when you load the file, all you have to read is a code and then you create an instance of the class that that code represents so that you can rebuild the inventory.

Offline samcp12

Junior Member


Medals: 1



« Reply #4 - Posted 2013-05-10 04:12:22 »

That sounds like it would work, any ideas on how exactly I would be able to load an image based of a value such as an int?
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #5 - Posted 2013-05-10 04:13:40 »

^ What are you using to develop your game? libgdx? lwjgl? java2d? pure java?

tbh, I only know how to do it in libgdx Smiley

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #6 - Posted 2013-05-10 04:19:55 »

for what it's worth, how I do it using libgdx is that I have some code such as this in the constructor of my class:

1  
2  
3  
/* load ball graphic */   
texture = new Texture(Gdx.files.internal("data/ball.png")); // create a texture based on an image source
sprite = new Sprite(texture, texture.getWidth(), texture.getHeight());  // create a Sprite from the created texture


and I have a method inside that class that returns the graphic when called (usually when it needs to be drawn on the screen):

1  
2  
3  
public Sprite getSprite(){ 
       return sprite;
}

Offline samcp12

Junior Member


Medals: 1



« Reply #7 - Posted 2013-05-10 04:42:33 »

I'm using just pure java Cheesy, I managed to assign an individual id to each item now I am just trying to figure out how to function like I need it to :/
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #8 - Posted 2013-05-10 04:46:42 »

Oh man, kudos to you for using pure java, but I can't help you out with regards to how to implement the logic if that's the case Smiley

Best of luck, though.

Offline Fsig

Senior Newbie





« Reply #9 - Posted 2013-05-10 06:42:44 »

I'm using just pure java Cheesy, I managed to assign an individual id to each item now I am just trying to figure out how to function like I need it to :/

So now you could have your items store a image location or you could make your images have the same ID as your items.

If you item stores a image location you would look up your item by it's ID and get the image location.
If you save the image with the same ID then you just load the image using the ID as the file name.

Here is a example of loading a image with the ID as the file name.

ImageLoad.java
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  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.imageio.ImageIO;


public class ImageLoad {
   
   /**
    * Read the item file and load the item image.
    */

   public ImageLoad(){
      //File name (Location is assuming the file is in the same folder)
     String itemFile = "./myitem.txt";
      String imageFileName = "";
     
      //Get the file
     BufferedReader input = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(itemFile)));
      String line;
     
      //Read the file line by line.
     try{
         while((line = input.readLine()) != null){
            imageFileName = line.split(":")[1].trim();
         }
      }catch(Exception e){
         e.printStackTrace();
      }
     
      //Echo the image filename
     System.out.println(imageFileName);
     
      //Load the image
     BufferedImage myItemImage = loadImage("./" + imageFileName + ".png");
     
      //Echo the image
     System.out.println(myItemImage);
   }
   
   /**
    * Method to load a image.
    *
    * @param ref
    * @return BufferedImage
    */

   public BufferedImage loadImage(String ref){
      BufferedImage bimg = null;
     
      try{
         bimg = ImageIO.read(this.getClass().getClassLoader().getResource(ref));
      }catch(Exception e){
         e.printStackTrace();
      }
     
      return bimg;
   }
   
   /**
    * Start.
    *
    * @param args
    */

   public static void main(String args[]){
      new ImageLoad();
   }

}


myItem.txt
1  
Image: 1


1.png
This is just a image made in paint 5x5 pixels with a black background.

Output:
1  
2  
1
BufferedImage@7b19f779: type = 6 ColorModel: #pixelBits = 32 numComponents = 4 color space = java.awt.color.ICC_ColorSpace@65c66812 transparency = 3 has alpha = true isAlphaPre = false ByteInterleavedRaster: width = 5 height = 5 #numDataElements 4 dataOff[0] = 3
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.

E.R. Fleming (10 views)
2014-07-29 03:07:13

E.R. Fleming (4 views)
2014-07-29 03:06:25

pw (38 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!