Help with how to access player inventory from script?

Coras

10 Month Old Code Baby
Member
Joined
Mar 11, 2016
Messages
8
Reaction score
1
First Language
English
Primarily Uses
N/A
I'm writing a plugin that requires a test to see if a certain item of ID 002 in the database is in the player's inventory. How do I do that?
Also, I'm a bit of a noob, so could someone explain to me how to use things like the $dataItems and $gamePlayer calls, because I can't seem to find a way to use them just by reading the code, for example where would I see in the code that I can use $gameParty.members()[ID] to refer to a party member? I don't see that anywhere.

Thanks
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,454
Reaction score
12,015
First Language
English
Primarily Uses
RMMV
$dataItems is just the array that you set up in the Items database. That just holds a description of each item.

Here's an easy way to find out stuff like this ... find an event command that does it, then look at the code to see what the event command does. rpg_objects.js and Game_Interpreter right towards the bottom, is the place to go for such a search.

For this example, the event page has a condition to make that page active if the party has a certain item. If you look at the Game_Event code you'll see a meetsConditions function, and the part that does the item check looks like this:
Code:
var item = $dataItems[c.itemId];
if (!$gameParty.hasItem(item)) {
  return false;
}
So it's using an item id to retrieve the element from the $dataItems array with that id. So the item var is an actual item, not an item id. Then it simply calls the $gameParty.hasItem function, passing in the item you're testing for.

Note that the Game_Party.hasItem function has two arguments. The second, includeEquip, defaults to false if not passed in. For items, that's not a problem, as you normally can't equip items. But if you were interested in armor or weapons, you'd need to pass in true as a second argument if you wanted it to consider equipment as well.

There is also a test in the Conditional Branch event command to see if the party has a certain item in their inventory, and Control Variables also puts the number being carried into a variable. For both of these, you could look at Game_Interpreter to see what happens behind the scenes - that's the class that processes all the event commands.


For your other question ... $gameParty.members() returns an array of members. [ID] is just looking at a certain element in that array. 0 is the first element in that array, which is your party leader (the ID is not the actor id, it is the index, or order, of actors in your party - so ID is probably not a good variable name to use in this case). If you want to refer to an actor id, and you don't care where they are in the party, you would be after $gameActors.actor(id)

There is no simple way to learn all of this quickly. It's a matter of looking in the most likely place for each situation, and gradually, over time, you'll get better and faster at it, and you'll remember things and not need to look them up. There really is no shortcut.
 

Coras

10 Month Old Code Baby
Member
Joined
Mar 11, 2016
Messages
8
Reaction score
1
First Language
English
Primarily Uses
N/A
Thanks! This was very helpful.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

How many Vocalist is on this forums?
Stream will be live shortly with some Witcher 3 followed by Haunting Ground! Feel free to drop by!
I have at least three ongoing projects and I have no idea what to do with them.

Gotta love game dev.
I keep seeing the 'file tear' icon when I'm on some post that has images. I'm not sure if it's just me or it's the post, based on the replies I see.
Me: anime is okay. There are a couple good ones and a lot of really bad ones but the good are worth it. Brother: if you bring anime anywhere near me I'll break your nico nico kneecaps

Forum statistics

Threads
94,277
Messages
919,590
Members
124,001
Latest member
Howzarion01
Top