Using an Inventory Item on NPC Character but having Player walk to NPC first.

Started by jamesreg, Sat 23/06/2018 05:15:38

Previous topic - Next topic

jamesreg

I am trying to figure out how to use an inventory Item on a NPC Character but have the player character have to first walk to the location of the NPC before interaction is completed.

I know how to use an inventory Item on a character just confused on how to get it to walk to the character before that part.

Cassiebsg

In the use_inventory function you just add a walk to command. ;)

Something like:
Code: ags

player.Walk(cNPC.X+1, cNPC.Y, eBlock, eWalkableareas); // You can of course, adjust the relative coordinates so your player faces the NPC in the exact spot you want him to be, by adding + or - to the NPC coords.
player.FaceCharacter(cNPC);
// Add remaining of give item code here.


Hope it helps. (nod)
There are those who believe that life here began out there...

jamesreg

This worked perfect thank you so much.

I have many interactions where this will be needed this was perfect.

Cassiebsg

You can also make a new function for it, so you don't need to type all that over and over...
Code: ags

void TalkToNPC (Character *walktoNPC, int walkX, int walkY) // defines name of character, and x + y coords to walk to
{
    player.Walk(walkX, walkY, eBlock, eWalkableareas);
    player.FaceCharacter(walktoNPC);
}

//Place the above code around the top of Global Script, and then place the next line in the Header.
import void TalkToNPC (Character *walktoNPC, int walkX, int walkY);



Now you can just type when coding:
TalkToNPC(cNPC, cNPC.X+1, cNPC.Y);
There are those who believe that life here began out there...

ManicMatt

what, just for that one NPC Cassie? It doesn't take me very long to type that, plus the more you type manually, the more you remember code and how to type it. :P

jamesreg

Well actually this could come in handy as their will be multiple characters ill be doing this with a really large amount actually.
What I used the above code for was a cow. The player used a milk bucket on it and walked over it the cow and milked it.

I have a large scrolling screen with many such farm animals roaming around on it and the player will need to basically walk around and do various farming actions like this on the animals.

So any kinda code that simplifies the process is welcomed thanks. 

Crimson Wizard

Quote from: ManicMatt on Sun 24/06/2018 17:19:40
what, just for that one NPC Cassie? It doesn't take me very long to type that, plus the more you type manually, the more you remember code and how to type it. :P

This depends on the situation (and the size of the repeated code chunk), but usually putting one in a function gives following benefits:
- Less chance to make a typo in one of the copies, which you will have a hard time finding.
- Easier to change or expand this interaction. Imagine you want to slightly change the way player walks to NPC, and now you need to change code in 20 places. With a function you change it in one place (the function body).

jamesreg

Thanks for the added comments on this subject as this is going to be a repeated action on multiple characters this gives me a better look at how to handle it before getting to deep into it and realizing I could have done it differently or saved code, time and headaches later.

Slasher

As Crimson said,

Changing many things in just one place is a godsend....its like a master sheet controlling minor sheets...

This is the way to go and once you get the hang of it you'll never be the same...(nod)

ManicMatt


SMF spam blocked by CleanTalk