   How do I make the NPCs "know" the map?
  How do I make the NPCs "know" the map, without passing a copy of the entire map to each one? I'm working in Java. Honestly I'm thoroughly confused with the class structure I'm supposed to use.As of now, I have a "Renderer" class which extends frame, and inside this class I store the map and an arraylist of "NPC" (which extend characters) objects which have their own tick() methods.Ideally, I want my "Renderer" to be merely a camera that I can focus on a "Character" object, and I want to be able to have some sort of a "Simulator" class control the game.How do I go about doing this? Sorry if you find my explanation confusing   
  HiMake Nodes, points on map and move the NPCs between the points, so they will not know the entire map, only the points between they will move.   ;Quote:Original post by streamerHiMake Nodes, points on map and move the NPCs between the points, so they will not know the entire map, only the points between they will move.but i would like to have the npc's move onto any square, as it is a dynamic rpg/simthanks though   ;  I assume your problem with passing the whole map is that the dependency issues. If so, could you just create an interface (or maybe even a base class) for the map containing the methods/properties you want to use?   ;Quote:Original post by SpodiI assume your problem with passing the whole map is that the dependency issues. If so, could you just create an interface (or maybe even a base class) for the map containing the methods/properties you want to use?yes I have that, but the only way I could access the map from the NPCs without passing would be if the map object was static. I would like to keep the ability to create separate games... semi static sort of.   ;  Java passes objects by-reference (or what some people prefer to call passing references by-value), so passing the map object around is actually dirt cheap since you are not passing a copy but a reference to the actual object (if that's what you're worried about). Additional caveat because of that: if your NPC modifies the map object, you've modified the original, so watch out. For getting info from the map without passing a reference of the map to the NPC, you could either use an event manager or a polling station, but I think those kind of solutions are more than you need at this point.A better question might be, what kind of tasks do you want to accomplish? Let's start from there. Maybe it's not the NPCs that need this data in the first place.   ;  Well, you've got some not insignificant problems here. Your Renderer class owns your map and the NPCs - how does that make sense? Surely a Renderer would merely keep graphical objects. Sure, the map might be graphically represented, but IMO it should own a graphical object, not be one. Same for the NPCs.Once you've got this sorted out, it should be a little clearer. With the logical map and logical NPCs separated from the graphical representations thereof, we see that it's OK for an NPC to "know about" the map object. After all, they're both just parts of the game. To go about doing this, there's two general methods:A) Let the NPCs retain a reference to the map and query it for relevant information. For example, an NPC might call the map's FindRoute function to get the path it must travel to reach a destination. Or it might call the FindEnemiesInRange function which will have the map search for all enemy NPCs within a given radius and return those to the NPC.B) Let the map inform the NPCs of important information. When the map gets something interesting, it tells the NPC. For example, it might say that an enemy has gotten within some radius of the NPC.Method A is probably the more logical way to go. I'm sure there are other methods I haven't thought of/heard about that would be useful here.Note: in my post, the "map object" I talked about could be the Simulator that you mention. Or perhaps the Simulator owns a map object (in this case, the NPCs might query the Simulator instead of the map).   ;Quote:Original post by EzbezWell, you've got some not insignificant problems here. Your Renderer class owns your map and the NPCs - how does that make sense? Surely a Renderer would merely keep graphical objects. Sure, the map might be graphically represented, but IMO it should own a graphical object, not be one. Same for the NPCs.Once you've got this sorted out, it should be a little clearer. With the logical map and logical NPCs separated from the graphical representations thereof, we see that it's OK for an NPC to "know about" the map object. After all, they're both just parts of the game. To go about doing this, there's two general methods:A) Let the NPCs retain a reference to the map and query it for relevant information. For example, an NPC might call the map's FindRoute function to get the path it must travel to reach a destination. Or it might call the FindEnemiesInRange function which will have the map search for all enemy NPCs within a given radius and return those to the NPC.B) Let the map inform the NPCs of important information. When the map gets something interesting, it tells the NPC. For example, it might say that an enemy has gotten within some radius of the NPC.Method A is probably the more logical way to go. I'm sure there are other methods I haven't thought of/heard about that would be useful here.Note: in my post, the "map object" I talked about could be the Simulator that you mention. Or perhaps the Simulator owns a map object (in this case, the NPCs might query the Simulator instead of the map).What do you mean by retain a reference?They also have to let the game know that the cell they are moving into is no longer walkable, so they must modify the map aswell.I only want one copy of the map for everything to access without it being static I guess.;Quote:Original post by lightbringerJava passes objects by-reference (or what some people prefer to call passing references by-value), so passing the map object around is actually dirt cheap since you are not passing a copy but a reference to the actual object (if that's what you're worried about). Additional caveat because of that: if your NPC modifies the map object, you've modified the original, so watch out. For getting info from the map without passing a reference of the map to the NPC, you could either use an event manager or a polling station, but I think those kind of solutions are more than you need at this point.A better question might be, what kind of tasks do you want to accomplish? Let's start from there. Maybe it's not the NPCs that need this data in the first place.This helps alot. But is it the best way to do it? Passing the entire huge 3 dimensional matrix? And I believe changing something in it does not change the original?   ;Quote:Original post by overeasyThis helps alot. But is it the best way to do it? Passing the entire huge 3 dimensional matrix? And I believe changing something in it does not change the original?If the map is an object, then you're not really "passing the entire huge 3d matrix" . As Lightbringer said, you're just passing the object's address. And changing something in it *does* change the original.You know if you do this, it doesn't copy the object, right? There's still only one objectMyClass a = new Myclass();MyClass b = a;//there is still only one object; if I do something to b, it changes a.In my game, each character has a reference to the map, and can call methods like isSquareFree(x,y) and isPathFree(x1,y1,x2,y2) to figure out if a move is possible. (Actually each char has a reference to a pathFinder object that calls these, but you get the idea.)