11

I was wondering if there were some good resources for the concepts of a FPS inventory. My current idea to to just have a JSON file with every item having a "name" "type" "weight" and "count" propertys. I was thinking this would have the added benefit of using a single list that can be used for multiple menus(ie: healthpacks menu, ammo menu, etc)

Are they different approaches or resources for FPS inventorys?

you are viewing a single comment's thread
view the rest of the comments
[-] Redkey@programming.dev 2 points 8 months ago

Ah, OK! Well, even if you're writing your game in JavaScript, there's still no need to work with the data directly as a JSON object. JS has built-in functions for serializing stored objects into JSON format (After all, the "JS" in "JSON" stands for "JavaScript"). If you aren't using JS, working with your data in a JSON representation seems like unnecessary overhead.

I think that one very common approach would be to make a simple array of objects for each "container" (this includes the player character and NPCs). Exactly what those objects are would vary by your language, platform, and personal tastes. You could have objects which contain all the information associated with themselves, such as name, description, weight, etc., or they could be as simple as an ID code that is an index into another table, and a count of how many of that item are held (this can reduce the likelihood of bugs).

The objects could also have their own methods such as reporting the weight or value of the item, which may simplify things if one property of an item (e.g. resale value) can sometimes be affected by another (e.g. level of wear), but not always. Or alternatively you could have functions that query the item objects and do all the necessary calculation and adjustment externally.

That's if your objects are mostly copies of the same things (e.g. health packs, ammo). If your objects are mostly unique (e.g. key, to room 102, Michael's pencil), many people instead make a single array which contains every object in the game/level, and each object includes a "location" attribute, with values such as in the player's inventory, in a particular NPC's inventory, or at a spot in the game world (perhaps specified in another object attribute). This can reduce the chances of accidentally creating multiple copies of "unique" items.

With regards to saving, if you're using JS then you can just automagically serialize your top-level array(s) to JSON and save that to a file. If you're using another language/environment, you'll need to figure out a file format that suits you, or look for a library. Making your own file format shouldn't be too hard, because a file is just a big array of byte values, and presumably you don't need to worry about interoperability.

this post was submitted on 03 Mar 2024
11 points (100.0% liked)

GameDev

2 readers
1 users here now

A community about game development.

Rules:

More rules might follow if they become necessary; general rule is don't be a pain in the butt. Have fun! ♥

GameDev Telegram chat.

founded 2 years ago
MODERATORS