• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Rick281

How to make an object usable in multiple classes

10 posts in this topic

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!

0

Share this post


Link to post
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
0

Share this post


Link to post
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.

0

Share this post


Link to post
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
0

Share this post


Link to post
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
0

Share this post


Link to post
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.

0

Share this post


Link to post
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.
0

Share this post


Link to post
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.

0

Share this post


Link to post
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!

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0