Its just because its frustrating to see so many people assume they are all that, and assume that selling a game is very easy, and assume that he doesn’t have to show code because he’s going to be a big shot developer. Genius Coder, we’ve already seen all the code you’ve written. Not yours specifically, but I can bet you everyone on this thread has been programming for longer. Thinking that we can’t see your code because you’re going to sell it is ignorant and offensive.
Point taken, but I don’t think that warrants a “You’re not a genius and you won’t ever be”. And it is ignorant and offensive to think something like that, so he needs to learn that. I just hate when people tell somebody they can’t do something that they totally can. That is ignorant and offensive as well.
I agree, that’s totally not warranted, but being ignorant is very annoying. I don’t care who you are, not researching before you get into something makes you look foolish.
Off topic:
I don’t think he is trolling. I saw the name and chuckled to myself, but lighten up guys, he’s just like every other person on this forum, a programmer, who happens to be asking for suggestions/help.
I don’t think his name warrants the amount of abuse he has copped from certain members here.
So I say let’s stay on topic, stop the flaming, and try and help out, without being condescending.
On topic:
I, myself am no expert on loading/saving…in fact, I’ve just popped back onto the forums are a while of no programming, and first thing I have asked about is saving/loading
While I am not great with the syntax of saving and loading, the concept behind it is pretty straight forward. Just like storing food in the fridge so you can eat it later.
Just like everyone else has said so bluntly here, you haven’t posted the core rendering code that may present the problem, and I understand it may be a problem for you if you wish to sell your game. But remember, most of the people who are able to provide you with help, are people who actually have made games themselves, or are more experienced programmers who have been programming for years on end, refining and expanding their own programming knowledge…one way to help learn programming is to share your existing knowledge with others, hence why forums like this exist. So my point is, you probably shouldn’t worry too much about people stealing your code…generally people who have to steal your code, have no idea how to use it anyway
You don’t have to post the entire source, but in this case, your methods for rendering.
It sounds like you are loading the data into the game, object positioning and names etc are showing when called from within a List (I’m guessing that is what you are using to keep track of your entities?), but the objects themselves are not visually being rendered.
This tells me that the objects obviously exist in your world, but the information loaded, is not being used to determine what textures to load in the location of your objects.
You say that they work before saving, but not after loading.
So it’s a matter of follow the yellow brick road and see where the bump is that takes things off course. I would say that your object information is being loaded into a List or ArrayList, but the code that normally determines what textures to load where those objects are, is not using that information at runtime.
It really is hard to tell without some sort of code snippet to show what is going wrong, and where. It sounds like something ridiculously simple, but it could also be ridiculously complicated and difficult if your code is poorly structured.
http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/tutorial.html
http://docs.oracle.com/javase/tutorial/jndi/objects/serial.html
http://www.tutorialspoint.com/java/java_serialization.htm
http://www.tutorialspoint.com/java/java_serialization.htm
Hope that helps.
A possible debugging check would be to write a “dump” method that displays ALL the information held in the Buildings class. Then, compare before and after the saving/loading process.
Or use a step-debugger and inspection to verify this, by putting breakpoints before and after the load.
It is plausible that an inexperienced programmer might add a “transient” keyword on the suggestion of an IDE, if there were any unserializable objects held in the class being saved/loaded. If “transient” was used for any variables, the data held in them will not be restored in the load.
what are you on about not researched it, I have otherwise how else did i know how to write up the save and load method, i came here because it wasnt working properly not becaujse i wanted loads of source code!
I get an error here: for (int i = 0; i < AmountOfBuildings; i++) Object_Control.Buildings.add((Building)save.readObject());
The system says:
java.lang.ClassCastException: java.lang.Integer cannot be cast to items.Building
My Building class for anyone that still wants to help:
public class Building extends Entity implements Serializable{
public Building(String name, int x, int y) {
super(x, y);
this.name = name;
}
public String name;
public Image currentImage;
//new Structures
private Image ConcreteShack;
private Image Tent;
private Image BarbedWire;
private Image Barricade;
private Image Wire_Fence;
private Image Wood;
private Image Wasteland;
private Image Plasma_Core;
private Image base_platfrom;
public static int velX = 0;
public static int velY = 0;
World W;
public void update() {
y += velY;
x += velX;
}
public void draw(Graphics2D g2d) {
Image();
g2d.drawImage(currentImage, x, y, null);
if (Game.debugMode) { g2d.draw(getBounds()); }
}
public void Image() {
ImageIcon CS = new ImageIcon(this.getClass().getResource("/buildings/CH1.png"));
ConcreteShack = CS.getImage();
ImageIcon T = new ImageIcon(this.getClass().getResource("/placable_Items/tent.png"));
Tent = T.getImage();
ImageIcon BW = new ImageIcon(this.getClass().getResource("/placable_Items/Barbed_Wire.png"));
BarbedWire = BW.getImage();
ImageIcon Bar = new ImageIcon(this.getClass().getResource("/placable_Items/baracade2.png"));
Barricade = Bar.getImage();
ImageIcon WF = new ImageIcon(this.getClass().getResource("/placable_Items/Wire Fence.png"));
Wire_Fence = WF.getImage();
ImageIcon W = new ImageIcon(this.getClass().getResource("/placable_Items/wood.png"));
Wood = W.getImage();
ImageIcon WASTE = new ImageIcon(this.getClass().getResource("/buildings/wasteland.png"));
Wasteland = WASTE.getImage();
ImageIcon PC = new ImageIcon(this.getClass().getResource("/buildings/plasma core.png"));
Plasma_Core = PC.getImage();
ImageIcon BP = new ImageIcon(this.getClass().getResource("/backgrounds/NO TEXTURE.png"));
base_platfrom = BP.getImage();
if (name == "") { currentImage = base_platfrom;}
if (name == "Concrete Shack") { currentImage = ConcreteShack;}
if (name == "Tent") { currentImage = Tent;}
if (name == "Barbed Wire") { currentImage = BarbedWire;}
if (name == "Barricade") { currentImage = Barricade;}
if (name == "Wire Fence") {currentImage = Wire_Fence;}
if (name == "Wood") {currentImage = Wood;}
if (name == "Plasma Core") {currentImage = Plasma_Core;}
if (name == "Wasteland") {currentImage = Wasteland;}
if (name == "Base Platform") {currentImage = base_platfrom;}
}
public Rectangle getBounds() {
if (name == "") { return new Rectangle(x ,y , 0, 0);}
if (name == "") { return new Rectangle(x ,y , 0, 0);}
if (name == "") { return new Rectangle(x ,y , 0, 0);}
if (name == "Plasma Core") { return new Rectangle(x ,y , 150, 250);}
if (name == "Wasteland") { return new Rectangle(x ,y , 80, 80);}
if (name == "Wood") { return new Rectangle(x ,y , 25, 30);}
if (name == "Wire Fence") { return new Rectangle(x ,y , 150, 20);}
if (name == "Concrete Shack") { return new Rectangle(x ,y , 225, 205);}
if (name == "Tent") { return new Rectangle(x ,y , 250, 200);}
if (name == "Barbed Wire") { return new Rectangle(x ,y , 400, 10);}
if (name == "Barricade") { return new Rectangle(x ,y, 100, 135 - 90);} else
return new Rectangle(x ,y , 0, 0);
}
}
Okay.
A few things:
-
You’re reloading the image every single time draw() is called. Waste of CPU and memory. Just cache it and refer to the cached instance when you want to draw.
-
Why are you loading resources for EVERY building in the game in Image() ? You should only load the resources you NEED.
-
You can’t compare strings with == , use .equals( … )
-
Please use a better naming style, for example use aVariable instead of A_Variable.
-
What the hell is this?
if (name == "") { return new Rectangle(x ,y , 0, 0);}
if (name == "") { return new Rectangle(x ,y , 0, 0);}
if (name == "") { return new Rectangle(x ,y , 0, 0);}
And if you want help with the Load/Save, you’ll need to post your Load/Save code.
Stuff I would work on:
- You’re loading all the images and then checking which one to use. You should make a class that contains all the images and loads them upon program start-up. When you want to check which image to use, don’t load images. Check the name of the building and set it’s image to one of the images that you loaded upon start-up.
- You shouldn’t create buildings “by name”. You should make a class for every building that extends building. For example,
a house would be:
public class House extends Building {
public House(int x, int y) {
super(x,y);
this.renderedImage = Images.houseImage;
}
}
This what a tent would like like or something…
public class Tent extends Building {
public House(int x, int y) {
super(x,y);
this.renderedImage = Images.tentImage;
}
}
If you want a quick performance boost, don’t call Image() from draw() method. Call Image() from constructor.
- How could i cache it?
- I need them all
- I usually do
- I usually do
- they’re temporary for when i decide to add some more buildings
My friend you did it, you helped me the solve the issue! All it was was the “==” thing used on Strings. Ive never had problems with ity in the pasyt but I just found out thats what was stopping the loading from working, so I thankyou!
Thanks to all those people who helped with the problem and Fuck you to all the people who were here just tp give me shit!
This is what I do in my games:
- Create a singleton called ImageManager (or whatever you want to call it)
- Create a Hashmap to hold the filename of the image, and the actual image itself.
- Provide an accessor method called getImage(String filename) which will look to see if the image is already loaded and returns it, or loads it, stores it in your hashmap and returns the image
- Have all your entities, etc grab their images from the ImageManager.
So:
public class ImageManager
{
public static ImageManager INSTANCE = new ImageManager();
private final Map<String, Texture> textures;
private ImageManager()
{
textures = new HashMap<String, Texture>();
}
public Texture loadTexture(final String filename)
{
Texture texture = textures.get(filename);
if (texture == null)
{
final String type = filename.endsWith("png") ? "PNG" : "JPG";
texture = TextureLoader.getTexture(type, getClass().getResourceAsStream(filename));
textures.put(filename, texture);
}
return texture;
}
}
Now your textures and images are stored in a single accessible place.
And with your Entity:
public class Entity
{
private transient Texture texture;
private String textureFilename;
public void init()
{
texture = TextureManager.INSTANCE.loadTexture(textureFilename);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
in.defaultReadObject();
init();
}
}
When you create your entity, give it a textureFilename and call init(). This will load the texture (once) and return it for use. The advantage here is that when you deserialise the entity (after loading a game back up) the init() will be called and the texture will automatically be loaded and reapplied.
This code isn’t 100% accurate, and something I’ve put together by quickly hacking up what I do myself, but it should give you the idea and point you in the right direction. You might want to add methods to the TextureManager to delete textures, etc.
Haha I don’t think that that’s what Dxu1994 meant:
I think he wonders why you have exactly the same statement 3 times in a row. What’s the reason for it?
This should work just the same:
if (name.equals("")) return new Rectangle(x, y, 0, 0);
On other notes, please mind the last post from trollwarrior1 on this thread. He makes a good point. You could easily make this a better OOP designed code:
public class Building extends Entity implements Serializable {
private Image img;
private Rectangle bounds;
private int velX = 0;
private int velY = 0;
public Building(int x, int y, Image img, Rectangle bounds) {
super(x, y);
this.img = img;
this.bounds = bounds;
}
public void update() {
x += velX;
y += velY;
}
public void draw(Graphics2D g2d) {
g2d.drawImage(img, x, y, null);
if (Game.debugMode)
g2d.draw(getBounds());
}
}
(Building.java)
public class ImageLoader {
private HashMap<String, Image> images = new Hashmap<>();
public ImageLoader() {
images.put("Base", loadImage("/backgrounds/NO TEXTURE.png"));
images.put("ConcreteShack", loadImage("/buildings/CH1.png"));
images.put("Tent", loadImage("/placable_Items/tent.png"));
images.put("BarbedWire", loadImage("/placable_Items/Barbed_Wire.png"));
// And so on ...
}
public Image get(String name) {
return images.get(name);
}
public static Image loadImage(String path) {
return new ImageIcon(this.getClass().getResource(path)).getImage();
}
}
(ImageLoader.java)
// Now you can easily create a new Building type like this:
public class Base extends Building {
public Base(int x, int y, ImageLoader loader) {
super(x, y, loader.get("Base"), new Rectangle(x ,y , 0, 0));
}
}
(Base.java)
or
public class ConcreteShack extends Building {
public ConcreteShack(int x, int y, ImageLoader loader) {
super(x, y, loader.get("ConcreteShack"), new Rectangle(x ,y , 225, 205));
}
}
(ConcreteShack.java)
And so on.
Finally, to make this work with ImageLoader, somewhere in your initialization code, use this:
// in initialization, where the Buildings are created:
ImageLoader loader = new ImageLoader();
Building whateverbuilding = new Base(0, 0, loader);
Building anotherBuilding = new ConcreteShack(0, 0, loader);
// It's important to reuse the 'loader' once you have created one :)
Hope this helps improving code readability
So while I typed this monster, another reply also did something like that. I’ll post mine anyways as some kind of addition and / or alternative to what StephenJSweeney posted.