this post was submitted on 03 Mar 2024
11 points (100.0% liked)
GameDev
2776 readers
1 users here now
A community about game development.
Rules:
- Adhere to the general lemmy.blahaj.zone rules (#1 being no homophobia, transphobia, racism or other exclusionary content)
- Self-promotion is fine as long as it's not spammy - share your progress, insights, techniques and mishaps! If you recently posted, update the previous post instead of filling the frontpage with your project
- Hide NSFW/NSFL content behind a clear warning, for example: [NSFW Nudity]
More rules might follow if they become necessary; general rule is don't be a pain in the butt. Have fun! ♥
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
"Inventory" usually refers to the list of items that the player character is carrying while the game is running. Why would you store that in a file and not memory? Are you talking about a game save file?
Sorry for the unclear-ness in my post! I'm currently using JavaScript in school working on databases and websites so my brain kinda just went to JSON as a way to track the players inventory. I was thinking of having the inventory start as an empty JSON object and have items added to it as the player collects thing, adding the benefit of being able to dump itself to a file for saves. While also being able to display itself in different menus: menu "health items" would just loop over the items with the class "health" and display them. I made this post wondering what types of ways is common for people to store/track inventory info
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.