Public Group

# How to make an object usable in multiple classes

This topic is 1665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So in my program i have a Game class that contains the game loop and such, a Map class for map objects, and a Player class to create the player object. The player is created in the Game class and I am able to use the player methods just fine in that class. However, in my Map class, I am wanting to use the player.getLocation() method, but it gives me the error that player cannot be resolved. Do I need to create my player object in a different place or somehow make the player object public or something. Thanks for the help!

##### Share on other sites

Usually you should make the Map class hold an attribute of Player type and pass the Player instance in the constructor of Map.

EDIT: by the way, why do you need to call Player inside Map? I sense a bit of code smell...

Edited by Avalander

##### Share on other sites

Thanks for the reply, although the language is a bit advanced for me. As to why the player is called in the map class is because some of the map methods are going to depend on the location of the player (which map the player is in). So i need to call the player.getLocation() method inside the map class. Forgive me if this is a silly way to do this I am very new to programming in general.

##### Share on other sites

Avalander summed up what I was going to say, so I will expand on his reply quick.  You don't state what language you are using, which compiler you are using, or which error code you are receiving, however, it sounds like you don't have an implementation for your routine "getLocation;" or the linker can't find it.  (Under C/C++ and a Microsoft compiler) you generally get a 'can't be resolved' error when you don't have an implementation for the routine, or are not exporting the symbols in the case of a library.

EDIT: I just noticed the "Java" tag.

Edited by ByteTroll

##### Share on other sites

Ok, I'll try to explain. First of all, if you are going to program in Java, you'll need a good understanding of the anatomy of a class. Also, you should understand whan an object is. But don't worry if you find those hard to understand in the beginning, you need to learn to think in a whole new way to program, and that requires time.

I suppose somewhere you call something like

Player player = new Player();


Here you are invoking the constructor of Player and creating a new instance of the class Player, which is stored in the variable player.

Then, somewhere else you probably call

Map map = new Map();


Which invokes the constructor of Map and creates a new instance of the class Map, which is stored in the variable map. If you want to access the player instance from inside your map instance, you need to store a reference to the player instance inside your map instance. How can you do that? Pretty easy, just create a field into the Map class with the type Player, then, make the constructor of Map require an argument with the type Player and store it in its field of Player type.

Because that will be a lot more comprehensive in code than explained, here you are the code for what I'm saying:

public class Map {
// This is the field of type Player. Each instance of the Map class will hold
// an instance of type Player.
private Player player;

// This is the constructor. Now, it requires an argument of Player type and assigns
// it to its "player" field.
public Map(Player player) {
this.player = player;
}

// This is some method
public void myMethod() {
player.doDirtyThings(); // Now you can do dirty things to "player" ;)
}
}


EDIT:

Oh, I forgot, now, of course, when you create the instance of Map you need to pass an instance of the type Player to the constructor:

Player player = new Player();
Map map = new Map(player);


EDIT 2:

I strongly suggest you to follow some basics tutorial in Java, if you are not already doing it. Having some basis will help you a lot.

Edited by Avalander

##### Share on other sites

Maybe it'd be better, when you call your Map function which needs the player location you pass it in.  For example, if you might call

Map.Update(Player.GetLocation());

That way, Map doesn't need to know everything about player, rather, you tell the map what little it needs to know from the players values.

##### Share on other sites

Ok thanks everyone for replying to me. I got some good information and I'll be going back to try to rethink things. Hopefully I can put together some type of solution. If not, I'm sure I'll be back.

##### Share on other sites
If you give us more details about the high level goal you have, we might be able to suggest alternative designs which avoid the map needing to know where the player is.

##### Share on other sites

I will try. I'm programming in Java. I am currently using eclipse to program and compile in. I am wanting to create a console type game that resembles pokemon. I know it is probably a difficult goal for me but I want to challenge myself. So the concept is that the player will start in one map and will have a list of actions to do on each map, such as walk this way, get map information, etc. This is really what I am focussing on right now. I really haven't put much thought into how battles and leveling and such is going to work yet. I am just trying to do one thing at a time. So far I have four classes : Game, Map, Player, Setup. The game class is where the program starts and contains the game loop which is a gamestate while loop. The map class will contain information such as the map name, map type, which creatures are in the map, how big it is, etc. The map class also has a method to give information when the player wants to know the information. The player class contains the location of the player, player name, etc. It has a method where the game loop will ask for an action to be typed in and will do whatever has been entered. The setup class is just there to hold all of the map construction and such. If it is easier to upload my eclipse project I can in a new topic. I'm really new to programming and I know I am probably doing 90% of stuff in a terrible way, but I am just trying to learn some of the concepts for game design. Thanks so much.

##### Share on other sites

Maybe it'd be better, when you call your Map function which needs the player location you pass it in.  For example, if you might call
Map.Update(Player.GetLocation());

That way, Map doesn't need to know everything about player, rather, you tell the map what little it needs to know from the players values.

Big +1 to that.

Unless you need something else than the getLocation from the Player, there's hardly any reason to stash a reference to your Player as you construct your map!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 15
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
634151
• Total Posts
3015823
×