Jump to content

  • Log In with Google      Sign In   
  • Create Account


How to make an object usable in multiple classes

  • You cannot reply to this topic
10 replies to this topic

#1 Rick281   Members   -  Reputation: 146

Like
0Likes
Like

Posted 03 July 2014 - 11:10 AM

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!



Sponsor:

#2 Avalander   Members   -  Reputation: 748

Like
0Likes
Like

Posted 03 July 2014 - 11:35 AM

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, 03 July 2014 - 11:37 AM.


#3 Rick281   Members   -  Reputation: 146

Like
0Likes
Like

Posted 03 July 2014 - 12:39 PM

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.



#4 ByteTroll   Prime Members   -  Reputation: 1223

Like
0Likes
Like

Posted 03 July 2014 - 12:44 PM

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, 03 July 2014 - 12:49 PM.

▬▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬▬▬▬▬▬
I see the future in 1's and 0's
▬▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬▬▬▬▬▬

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

#5 Avalander   Members   -  Reputation: 748

Like
0Likes
Like

Posted 03 July 2014 - 01:18 PM

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, 03 July 2014 - 01:25 PM.


#6 BeerNutts   Crossbones+   -  Reputation: 2716

Like
3Likes
Like

Posted 03 July 2014 - 02:56 PM

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.


My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#7 Rick281   Members   -  Reputation: 146

Like
0Likes
Like

Posted 03 July 2014 - 03:30 PM

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.



#8 rip-off   Moderators   -  Reputation: 7964

Like
0Likes
Like

Posted 03 July 2014 - 04:01 PM

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.

#9 Rick281   Members   -  Reputation: 146

Like
0Likes
Like

Posted 03 July 2014 - 06:39 PM

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.



#10 Orymus3   Crossbones+   -  Reputation: 6938

Like
0Likes
Like

Posted 03 July 2014 - 07:28 PM


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!



#11 Rick281   Members   -  Reputation: 146

Like
0Likes
Like

Posted 03 July 2014 - 08:28 PM

Ok so for those of you that want to see very messed up code, I am putting my four classes on here. Any and all tips are welcome!

 

Here is my game class:

package Game;
import java.util.Scanner;
 
public class Game { 
 
public static void main(String[] args){
boolean running = true;
String gamestate = "menu";
Scanner scanner = new Scanner(System.in);
Player player = new Player();
 
 
while (running == true){
 
switch(gamestate){
case "menu" :
 
 
System.out.println("Menu--------------------------------");
System.out.println("1) Play");
System.out.println("2) Info");
System.out.println("3) Exit");
 
String menuoption = scanner.next();
 
switch(menuoption){
 
case "1" :
gamestate = "introduction";
 
break;
 
case "2" :
gamestate = "info";
 
break;
 
case "3" :
running = false;
 
break;
}//end of menuoption switch
 
break;//end of menu
 
 
case "info" :
 
System.out.println("Developed by Brock Grinstead");
System.out.println("Version: 0.0.3");
System.out.println("Enter anything to return to the menu");
scanner.next();
gamestate = "menu";
 
break;//end of info
 
case "introduction" :
 
Setup gamesetup = new Setup();
gamesetup.setupMap();
 
String playername;
String player_action;
 
System.out.println("Entering the world of consolemon...");
 
System.out.println("What is your name?");
playername = scanner.next();
 
player.setName(playername);
 
System.out.println("Hello " + player.getName());
System.out.println("Welcome to the world of consolemon!!");
System.out.println("This is a very different world from the one that you know.");
System.out.println("You will have to learn how to coexist with many different consolemon and trainers.");
System.out.println("A great adventure awaits you, Goodluck!!");
 
player.setLocation("Sloan Town");
 
 
 
gamestate = "worldmap";
 
 
 
break;//end of introduction
 
case "worldmap" :
 
 
System.out.println("What would you like to do?");
 
player_action = scanner.next();
player.Playeraction(player_action);
 
 
 
 
 
break;// end of world map
}
}// end of game loop
 
 
scanner.close();
}// end of program
 
 
}

Here is my map class

package Game;
 
public class Map {
 
private String mapname;
private String maptype;
 
 
public void setMapname(String nameofmap){
mapname = nameofmap;
}
 
public String getMapname(){
return mapname;
}
 
public void setMaptype(String typeofmap){
maptype = typeofmap;
}
 
public String getMaptype(){
return maptype;
}
 
static void Mapdescription(String currentmap){
 
 
switch(currentmap){
 
case "Sloan Town" :
 
System.out.println("Sloan Town is the town that you first find yourself in the world of consolemon.");
 
break;//End of Sloan Town
 
 
}//End of map description switch 
 
 
}//End of map description method
 
}
 

Here is my player class

package Game;
 
public class Player {
 
private String name;
private String location;
 
public void setName(String enteredname){
name = enteredname;
}
 
public String getName(){
return name;
}
 
public void setLocation(String playerlocation){
location = playerlocation;
}
 
public String getLocation(){
return location;
}
 
 
void Playeraction(String paction){
 
switch(paction){
 
case "Map Info":
 
Map.Mapdescription(location);
 
 
break;//end of map info
 
 
}//end of player action switch
 
 
}// end of player action method
 
}// end of Player class
 

and here is my setup class

package Game;
 
public class Setup {
 
void setupMap(){
 
Map map1 = new Map();
map1.setMapname("Sloan Town");
map1.setMaptype("town");
 
}
 
}

I apologize for any head aches.







PARTNERS