Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
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  
  How to internally save objects properly (this subject might be misdirecting)  (Read 357 times)
0 Members and 1 Guest are viewing this topic.
Offline Ecen

Junior Newbie

Exp: 2 years

Magic, is science that we don't yet understand.

« Posted 2013-05-16 16:36:09 »


I'm pretty much just starting out with java, but I've learnt most basics by now. I think.

Anyways, I've just started out on a somewhat simple RPG-style game in which the player is supposed to be able to do  rpg-generic things like moving and fighting. Now, I've drafted some kind of basic layout for the program. One part handles drawing and one handles the actual game data. In the part that is supposed to handle the object-object interaction I've run into somewhat of a problem, and it's this problem that forms my slightly confused question:

How do I create objects and save them to be able to paint them later on while still retaining full control of specific objects?

To clarify what I mean, my last project involved a ball and a few lines. I essentially 'saved' internally them like this:
private Ball ball;
private MyLine[] line = new line[10]

public ConstructorOfThisClass(){

ball = new Ball(position, size)
line[0] = new MyLine(x1, y1, x2, y2)
line[1] = new MyLine(x1, y1, x2, y2)
line[2] = ...

To draw this I just had to call the ball's draw method as well as the draw method of line[ i ] in a loop cycling through all lines for every frame.

In this rpg, there must be more than 2 kinds of things. For example I may want a few trees, the player, a house, a couple of stones and an enemy. All of these things can of course subclass some kind of overall WorldObject class and then get their unique properties in their own class files. But how is it advised that I handle them? I kinda feel that manually (or in a loop with random values) creating a whole lot of tree instances and saving them to a list somewhere is not such a good solution. Perhaps some kind of grid?

I feel that I want to be able to refer to any one object at any time as well as being able to handle them all at once, mostly for drawing them.

So, yeah, anyone got any tips or any guides it might be worthwhile for me to look at? If you want me to clarify something, please tell, as it is obvious that I might not have made my point quite clear.

Offline dermetfan
« Reply #1 - Posted 2013-05-16 17:03:51 »

You mostly have to create them all manually, unless you save them in a file or class or generate them. You can add them to some kind of container, though.

A common way to do this is the stage and actor system.
Let's say you have some classes like Ball, Tree, Rat, I don't know. They all extend Actor. Then you can add them to a stage like this:
Stage stage = new Stage();
Ball ball = new Ball(position, size);

This way, you could add all your actors to an ArrayList for example:
public class Stage {
    private ArrayList<Actor> actors = new ArrayList<Actor>();
    public void addActor(Actor actor) {
    public ArrayList<Actor> getActors() {
        return actors;

To draw them you could use an enhanced for loop:
for(Actor actor: stage.getActors())
    actor.draw(); // or whatever way you draw your things

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

bilznatch (27 views)
2015-08-04 11:03:17

SHC (44 views)
2015-08-01 03:58:20

Jesse (25 views)
2015-07-29 04:35:27

Riven (48 views)
2015-07-27 16:38:00

Riven (26 views)
2015-07-27 15:35:20

Riven (28 views)
2015-07-27 12:26:13

Riven (19 views)
2015-07-27 12:23:39

BurntPizza (42 views)
2015-07-25 00:14:37

BurntPizza (56 views)
2015-07-24 22:06:39

BurntPizza (35 views)
2015-07-24 06:06:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!