Adding an NPC that gives you an item - pret/pokecrystal GitHub Wiki
This tutorial is for adding an NPC that gives you an item.
Contents
- Adding the required sprite to the overworld
- Creating the script for the NPC
- Event Flag to prevent farming
1. Adding the required sprite to the overworld
In this tutorial, we will add an NPC that gives you an item if you have met a given requirement. Here, we will settle on a the number of Pokémon caught by the player, replicating Oak's Aide from Generation 1.
We will do this at the gate between Ecruteak City and Route 42, though any area will also follow the same steps.
Start by editing maps/Route42EcruteakGate.asm
:
object_const_def
const ROUTE42ECRUTEAKGATE_OFFICER
+const ROUTE42ECRUTEAKGATE_ELMS_AIDE
...
def_object_events
object_event 5, 2, SPRITE_OFFICER, SPRITEMOVEDATA_STANDING_DOWN, 0, 0, -1, -1, PAL_NPC_RED, OBJECTTYPE_SCRIPT, 0, Route42EcruteakGateOfficerScript, -1
+object_event 2, 4, SPRITE_SCIENTIST, SPRITEMOVEDATA_SPINRANDOM_SLOW, 0, 0, -1, -1, PAL_NPC_BLUE, OBJECTTYPE_SCRIPT, 0, Route42EcruteakAideDexCheckScript, -1
This will initialize the scientist sprite used from Elm's Lab into the GateHouse.
2. Creating the script for the NPC
Next, we need to provide contents for Route42EcruteakAideDexCheckScript
, which is the script this NPC is assigned. We will use VAR_DEXCAUGHT
to query the current Pokédex caught value, and if above 20 gift the player the specified item.
+Route42EcruteakAideDexCheckScript:
+ faceplayer
+ opentext
+ readvar VAR_DEXCAUGHT
+ ifgreater 20, .Aide20Caught
+ writetext AideTextFailure
+ waitbutton
+ closetext
+ end
+.Aide20Caught
+ writetext AideTextSuccess
+ giveitem PRZCUREBERRY
+ closetext
+ end
+AideTextFailure:
+ text "Hmm… You don't have"
+ line "enough #MON."
+ para "No BERRY for you."
+ done
+AideTextSuccess:
+ text "Oh! You do have"
+ line "enough #MON."
+ para "Here's a BERRY!"
+ done
Great! We now have an NPC that will give you a PRZCUREBERRY if you have more that 20 Pokémon in your Pokédex, and will give you nothing if not. However, speaking to them again will give you another berry, and another for each time you speak to them. To combat this, we need to provide an EVENT_FLAG.
3. Setting an Event Flag to prevent farming
For this next step, add a new entry to constants/event_flags.asm
.
...
; New to Crystal
+const EVENT_ECRUTEAK_GATE_AIDE
...
We then need to go back to the Aide NPC, and add this event to their script. We also want this NPC to not appear anymore once they have gifted the player an item, which can be achived by replacing the -1
at the end of their object_event
data. Once the event has been set, the sprite will be set to not appear.
We will also take the time to add a check to see if the player's pack is full or not. This is handled by .NoRoom
, which you can edit to either consist of a single end
, printing a default message, or customize to use unique dialogue (which we will do here).
Edit: maps/Route42EcruteakGate.asm
.
...
Route42EcruteakAideDexCheckScript:
faceplayer
opentext
+ checkevent EVENT_ECRUTEAK_GATE_AIDE
+ iftrue .AlreadyGotItem
readvar VAR_DEXCAUGHT
ifgreater 20, .Aide20Caught
writetext AideTextFailure
waitbutton
closetext
end
+.AlreadyGotItem:
+ writetext AideGotText
+ waitbutton
+ closetext
+ end
+.Aide20Caught
+ writetext AideTextSuccess
+ giveitem PRZCUREBERRY
+ iffalse .NoRoom
+ setevent EVENT_ECRUTEAK_GATE_AIDE
+ waitbutton
+ closetext
+ end
+.NoRoom:
+ writetext AideNoRoom
+ waitbutton
+ closetext
+ end
AideTextFailure:
text "Hmm… You don't have"
line "enough #MON."
para "No BERRY for you."
done
AideTextSuccess:
text "Oh! You do have"
line "enough #MON."
para "Here's a BERRY!"
done
+AideNoRoom:
+ text "Your PACK is full"
+ line "it looks like."
+ done
+AideGotText:
+ text "I will head back"
+ line "to the lab soon."
+
+ para "Good luck on your"
+ line "journey"
+ done
...
-object_event 2, 4, SPRITE_SCIENTIST, SPRITEMOVEDATA_SPINRANDOM_SLOW, 0, 0, -1, -1, PAL_NPC_BLUE, OBJECTTYPE_SCRIPT, 0, Route42EcruteakAideDexCheckScript, -1
+object_event 2, 4, SPRITE_SCIENTIST, SPRITEMOVEDATA_SPINRANDOM_SLOW, 0, 0, -1, -1, PAL_NPC_BLUE, OBJECTTYPE_SCRIPT, 0, Route42EcruteakAideDexCheckScript, EVENT_ECRUTEAK_GATE_AIDE
Now we are set! The NPC will check if the event flag has been set or not. If not, it will check your Pokédex, and if you have enough to satisfy the condition (in this case 20 Pokémon) you will be given the item. The NPC will then change their message when you speak with them, and disappear from the gatehouse once the area is reloaded.