Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help with objects

Started by James1984 Apr 7, 2011 at 9:17 PM 4 replies 1.6k views
Original Post
James1984
James1984
Hi there,

Been trying to get my head around initializing and getting data from an object from other classes, im using Java.

Sorry if this explanation isnt the best, i'm still trying to get my head around this but hope you can help me.

If i have a class Enemy with a get method inside to get data say from a hashtable and then in another class called enemy_list i initialize an object for eg

Enemy enemy = new Enemy();

and call the method to get the data

enemy.get()

that will get any data that has been put into enemy. Now my issue is this If i have yet another class say creatures and i want to get data from class enemy (like name,level) to put both datas together i cant initialise another enemy object as that will create a new enemy and wont have the same data as the enemy_list class i want to get an existing enemy. How do i get access to the existing data in the enemy class without having to do another Enemy enemy = new Enemy() in the creatures class.
DanielONeill
DanielONeill
I'm not to familiar with Java, but your problem isn't java, its object orientated programming.

you can do something like this (pseudocode only I'm afraid)

Enemy ogreBrute1=new Enemy();
Enemy ogreBrute2=new Enemy();
ogreBrute1.get();
ogreBrute2.get();


hope that helps, better would be to have an array of ogreBrutes, not sure of the semantics of Java, but try something along these lines....

MaxOgres=100;

Enemy[] ogreEnemy=new Enemy()[MaxOgres];

then when you want to refer to any ogre you can use something like

ogreEnemy[ogreID].get();



hope that helps....


Edit:Even better, is to use a list, but I do not know how to do this in jave
James1984
James1984
Thanks for the quick reply I appreciate it ,

But what if i have a 3rd class and i want to access the data for ogreBrute1? ,

if i do this again in the new class Enemy ogreBrute1 = new Enemy(); , wont that create a new instance of the object with new data in the new class?

If i declare an object , Enemy ogreBrute1 in this 3rd class but dont instantiate it i will get a nullPointerException, so I'm confused how the same object can be accessed from different classes.

Thank You
DanielONeill
DanielONeill
look up inheritence - I think thats what your after....

In C#


class Creature()
{
...code...
}

class Enemy():Creature
{
...code...
}
James1984
James1984
Thank you I have got this working now, I was instantiating the object in each class that i was wanting to use the methods of that object, instead of instantiating the object once in the main method and then accessing them via the constructors.

I got this answer by looking at the Model-View-controller Design.

Thanks for your replies.
DanielONeill
DanielONeill
Not a problem, glad to have been of some help. Good luck with your endeavours....

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.