6 Replies - 1149 Views - Last Post: 05 July 2013 - 05:30 AM

#1 dreaminxna  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 61
  • Joined: 10-June 13

Interaction between npc and player?

Posted 03 July 2013 - 10:20 PM

Hi can anyone guide me on how to have an interaction between npc and player? for example how know if i am facing the npc so that it will talk to me? I have a solution but i dont think its the right one. Ive thought of creating a bigger rectangle aside from the rectangle for collision then detect if there is an npc on that big rectangle. But wat if on that rectangle there are two npc? well thats the problem. and how to get its ID? so that i would know whats the npc name? Can anyone guide me?? thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Interaction between npc and player?

#2 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 190
  • Joined: 26-April 10

Re: Interaction between npc and player?

Posted 04 July 2013 - 08:35 AM

If you wanna give your NPCs a name, you can either give them a property in the NPC class
private string _name;
public string Name
{
 get { return _name; }
 set { _name = value; }
}



Or you can store your NPCs in a dictionary (Instead of a list, I'm assuming you're using a list.. Or at least an array).
It would work something like this..
Dictionary<string, NPC> NPCs = new Dictionary<string, NPC>();



With that, the string would be the key (Where you would assign a name for your NPCs) and then NPC would be your object.
That could help with the name aspect.


But as far as who you're looking at, I see no problem with an extra rectangle. You'd just assign it so you can only recognise 1 NPC at a time, so you don't get multiple speaking to you all at once.
Was This Post Helpful? 1
  • +
  • -

#3 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: Interaction between npc and player?

Posted 04 July 2013 - 12:57 PM

How is your game world set up? Are you using a Tile Map or just placing sprites into the world?? How are you storing the NPC's? What kind of interactions do you want the player to have with NPC's?
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 591
  • View blog
  • Posts: 1,318
  • Joined: 24-April 12

Re: Interaction between npc and player?

Posted 04 July 2013 - 06:44 PM

LiberLogic is asking the right questions. If you have a tile map, its a lot easier than if you don't.

Essentially, I think you want to do something like have the user hit the space bar to interact with the object the player is facing. When that trigger happens you may want to check the grid square in front of the character for the presence of an NPC. If there is one, you may want to have that trigger a dialog.

How do you check if the space in front of the player is occupied by an NPC? Well, if you have a grid you can just check that grid space. If not, you could test with a bounding box in front of the player and see if that collides with an NPC. If the box is just slightly bigger than the NPC's collision box then it would only be able to fully hold one NPC unless you are letting the NPC's completely overlap one another.
Was This Post Helpful? 0
  • +
  • -

#5 dreaminxna  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 61
  • Joined: 10-June 13

Re: Interaction between npc and player?

Posted 04 July 2013 - 08:42 PM

View Postoyyou, on 04 July 2013 - 08:35 AM, said:

But as far as who you're looking at, I see no problem with an extra rectangle. You'd just assign it so you can only recognise 1 NPC at a time, so you don't get multiple speaking to you all at once.


Cool. Thank you very much
Was This Post Helpful? 0
  • +
  • -

#6 dreaminxna  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 61
  • Joined: 10-June 13

Re: Interaction between npc and player?

Posted 04 July 2013 - 08:50 PM

View PostBBeck, on 04 July 2013 - 06:44 PM, said:

LiberLogic is asking the right questions. If you have a tile map, its a lot easier than if you don't.

Essentially, I think you want to do something like have the user hit the space bar to interact with the object the player is facing. When that trigger happens you may want to check the grid square in front of the character for the presence of an NPC. If there is one, you may want to have that trigger a dialog.

How do you check if the space in front of the player is occupied by an NPC? Well, if you have a grid you can just check that grid space. If not, you could test with a bounding box in front of the player and see if that collides with an NPC. If the box is just slightly bigger than the NPC's collision box then it would only be able to fully hold one NPC unless you are letting the NPC's completely overlap one another.


Is there a problem if i use tile map for example? That is exactly on my mind. If i press space the npc would interact with me. Btw is there other way than to use tile map to represent as the environment? Tilemap + bounding collision is causing some lag to my game because of looping on every tiles.
Was This Post Helpful? 0
  • +
  • -

#7 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 591
  • View blog
  • Posts: 1,318
  • Joined: 24-April 12

Re: Interaction between npc and player?

Posted 05 July 2013 - 05:30 AM

There's nothing wrong with using a tile map. In fact, I would almost certainly use one for a 2D RPG or something along those lines.

But if you have a tile map, then you almost certainly have an array to control each tile. NPCs are likely to be in one of those grid spaces. You could check to see if an NPC is in the grid space the PC is facing. Even if your NPCs are positioned by floating point values, you could round to integers to match their current position up with a grid space. It's even easier if they move directly from grid space to grid space.

You could have a second array that overlays the first to manage placeables and NPCs. But if that would be mostly empty space, then a list might be better to keep track of only the grid spaces that are actually occupied. The second array has the advantage of being ordered in grid space order. So you can directly check Placeables[4,7]>=4, for example.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1