PDA

View Full Version : rpg npc



mordormaster
January 17th, 2008, 09:32 AM
now everyone here has probably heard it a thousand times but i have a problem with npcs, i have tried coding them myself and failed, i then tried some tutorials but i have been using a backgorund that maves as aposed to a character so they dont help. so basicly could anybody code form me or point me in the direction of a tutorial that cna show me how to code a npc that can have its poision moved around and still work. thank you :)

gregmax
January 17th, 2008, 11:13 AM
What exactly are looking for your npc to do? I use Tonypa's tutorials to help me for my game programming - they are really good. He has one about npcs too right here: http://www.tonypa.pri.ee/tbw/tut09.html

mordormaster
January 17th, 2008, 12:19 PM
sorry for not being specific, i meant sort of you can talk to them and they can talk back, for example you can get close to him press space and then options would pop up at the bottom of the screen and you could select what to say.

gregmax
January 17th, 2008, 12:38 PM
In Tonypa's tuts he does a collision detection with the enemies. Instead of having the enemy killing the hero, you just have a:


if(Key.isDown(Key.SPACE)) {
trace("get npc's id and do the rest of the stuff");
}


Does that make sense? Then you can have it open up a movieclip with some button options etc.

Or am I still vague? Or are you asking for a complete code example on how to do this? That would take some time for me to do...

mordormaster
January 17th, 2008, 12:46 PM
dont worry i dont want a complete coded secsion that woudl take alot of your time and thank you for the help :) i am just trying to get the hit test to work now which aint going so well but i will see if tonya's tutorial has anything that i coudl use. if you have a example could you post that please, if not thank you very very much = ]
you have helped my game along i will put you in the credits

Marz
January 17th, 2008, 01:56 PM
There is another waqy that you can use. It's directional related.

Instead of taking up computer processes by doing hitTest's on every single NPC you can do simple Math instead. I'll call it Relational Distance. You see, hitTesting tests out bounding boxes or up to 4 points testing out 1 point on the main object. Whereas, relational distancing only tests out the x and y distances and that's it.

So, to make it easier on you, say the location of your NPC is at 150x and 200y and say you want to be able to talk to him when you are near him... So let's say, 25 pixels away from any direction. Instead of making this a circular area, we will make our lives easier and just give him a boxed area.

http://www.visualelementproductions.com/icons/distance.jpg

Okay, now, you just have a simple check statement that's found inside of your main game loop.



distanceX = Math.abs(playerMC._x - npcMC._x);
distanceY = Math.abs(playerMC._y - npcMC._y);

if(distanceX < 26 AND distanceY < 26)
{
// Have NPC MC do something to signify that the player can talk to him!
}


And that's pretty much it. Other than that, you can even store your NPC locations in one array for a certain given map and then just loop through the array as well.

Hope this helps you.

Santa Clause
January 17th, 2008, 02:46 PM
Hey Marz,

I logged on AIM, you online?

tarwin
January 17th, 2008, 07:29 PM
I think it's a good idea to have an "interact distance" and a "de-interact distance" for NPCs. This way you can walk up to them, into their interact zone, then for example press space to interact. Then when you want to leave you just walk away past the de-interact distance. Makes gameplay less complex sometimes.

Also, you might want to work on some kind of onKeyDownOnce code. That is, keep track of keys pressed and only fire events once for a character. This means won't have to keep track of if you've started speaking quite the same way.

mordormaster
January 18th, 2008, 06:55 PM
i am sorry to bother you guys again but i am having a bit of trouble. I tried to use the method that was surgested but fail. i was wondering if somebody could check it otu and maybe tweek the file so it works = ]
thank you = ]

gregmax
January 18th, 2008, 11:46 PM
First off, you need to give your movie clips an instance name, THAT would really help ;)

secondly, don't do a collision detection or a distance calculation on a "onClipEvent(load)" ... it will only do it once the clip is loaded, you need to do it in a loop. ie, onClipEvent(enterframe)

I attached the working version

hatu
January 19th, 2008, 07:58 AM
There is another waqy that you can use. It's directional related.

Instead of taking up computer processes by doing hitTest's on every single NPC you can do simple Math instead. I'll call it Relational Distance. You see, hitTesting tests out bounding boxes or up to 4 points testing out 1 point on the main object. Whereas, relational distancing only tests out the x and y distances and that's it.

So, to make it easier on you, say the location of your NPC is at 150x and 200y and say you want to be able to talk to him when you are near him... So let's say, 25 pixels away from any direction. Instead of making this a circular area, we will make our lives easier and just give him a boxed area.

http://www.visualelementproductions.com/icons/distance.jpg

Okay, now, you just have a simple check statement that's found inside of your main game loop.

ActionScript Code:

distanceX = Math.abs(playerMC._x - npcMC._x);
distanceY = Math.abs(playerMC._y - npcMC._y);

if(distanceX < 26 AND distanceY < 26)
{
// Have NPC MC do something to signify that the player can talk to him!
}





And that's pretty much it. Other than that, you can even store your NPC locations in one array for a certain given map and then just loop through the array as well.

Hope this helps you.
Also as RPGs are (almost) always tile based, make use of that and as you know the coordinates that you're seeing at any given time, only run the checks on the NPCs that are between those bounds.

Marz
January 19th, 2008, 03:13 PM
Another fine point hatu ;)

mordormaster
January 20th, 2008, 08:20 AM
thank you all very very much = ] i finally have people running around my oblivion world, its pretty huge so this was a massive help to have some sort of interaction :) now not to push my luck as you have all helped me loads but i am working on ai that move around on the background as aposed to around the screen, which is really hard to explain, does anybody have any example or tutorials on ai from rpgs please
thank you all very very much = ]

tarwin
January 20th, 2008, 09:00 AM
Really depends on what you want them to do. It might be a good idea to just sit and watch some RPGs such as Neverwinter Nights. Maybe also check out their programming (some can be seen user side).

Most RPGs they'll just walk aimlessly from point X > Y > Z and return. When you talk they'll have a conversation ready depending on some variable showing certain messages.

mordormaster
January 20th, 2008, 09:13 AM
well ive had a look at most rpgs i can find, they are generally moving around the screen, i would like my enemies to come to the character, but have them move around the background as aposed to the screen, if that makes any sence.

Marz
January 20th, 2008, 12:09 PM
So you are working on the enemies ai?

This is where it gets a little more difficult depending on how you want their "behavior". How do you specifically want them to behave, do you want them to have short term AI where they only kick in when you get close and they just attack you?

I need a little more details when it comes to A.I. before I can really help you with that. Basically, how will the enemy react in whole?

mordormaster
January 20th, 2008, 12:18 PM
i would like them to have a sort of go towards and hit the enemy behavoir, and if possible the idea that if they are under to much threat they would move away and attack less, i tried but it walked straight down and left, so i scraped it being totally confused

therobot
January 20th, 2008, 06:22 PM
I'll reiterate gregmax's recommendation to scope out tonypa's tutorials on tile based games.

I knew relatively little about programming a.i. for games, but if you incorporate a few ideas he explains, such as pathfinding, or the tutorials on "stupid enemy", you can get a better idea on how to program your enemies.

LINK TO TONYPA'S TUTORIALS. (http://www.tonypa.pri.ee/tbw/index.html)

mordormaster
January 21st, 2008, 07:12 PM
my game is artistic based and the tiles are the size of the maximum window that cs3 can take, its a stupid sort of idea but me and my friend have found a way to create a very interactive game with very little bites, the only drawback is that i cant use any of these tuorials, could anybody help me

mordormaster
January 23rd, 2008, 01:20 PM
hey i added scrolling to the npcs and it seems to have just killed there usefulness :( they dont work could anybody tinker with them to see if they can fix them again

gregmax
January 23rd, 2008, 02:10 PM
I changed the structure a bit (sorry) ... but this way "should" make more sense and is more in one place instead all over the place :)