0
\$\begingroup\$

I am looking to program my own little Role Playing Game using...

  • ANSI C or "standard C", (NOT C++) as my programming language
  • The SDL2 development library

I have got a very basic and simple type of game up and going,
(I am learning ANSI C as I develop this hobby game).

I want to implement an item system to the game, so I have been looking through the internet for advice, but it seems like the closest I can get are solutions to C++, Java and other object oriented languages. I can't seem to find a good implementation in C, other than THIS guide, so I thought I could post this question specifically for ANSI C both for me and for others that might be looking to achieve the same goal.

My attack plan is to create a struct to lay out a blue print of what an item is, much like a class in an OOP language. Then, perhaps create an array that defines objects of this type and fills in the details for each item.


This is what I have so far:

#ifndef ITEMS_H_INCLUDED
#define ITEMS_H_INCLUDED

/* Create a structure that represent an item.
   @see https://www.tutorialspoint.com/cprogramming/c_structures.htm */
typedef struct Item {
    //Unique ID to be used as a reference to the item.
    unsigned int id;

    //Name of the item.
    char name[20];

    //Brief description about the item.
    char description[50];

    //Defines the category of the item; equipment, consumable, etc...
    unsigned char type;

    //Used for equipment; defines the strength of the weapon/armor.
    unsigned char stats[4];

    //The X and Y coordinates of the starting point of the sprite.
    unsigned int sprite[2];
}item;

#endif // ITEMS_H_INCLUDED

The id-attribute will be used to spawn the item in different locations, such as...

  • In the player's inventory
  • In a store
  • On the floor to be picked up

I'm looking to hard code the item DATA in the source file, but I'm also open for solutions like reading item information from a file, and such.

\$\endgroup\$
9
  • 2
    \$\begingroup\$ Can you clarify, what is your question? Did you encounter a specific problem in making your system so far that we can help you solve? Is there a specific gap or missing feature in the system as you've written it thus far that you need help implementing? \$\endgroup\$
    – DMGregory
    Sep 9, 2019 at 22:59
  • \$\begingroup\$ I have an issue conceptualizing it and I'm not sure which approach/implementation is the best, most effective way and which is most scalable. Seeing that C is not an OOP language, how would I translate an item system written in OOP languages like C++ or Java, into a language like C? I hope this clarifies my question. \$\endgroup\$ Sep 9, 2019 at 23:18
  • \$\begingroup\$ What specific benefit do you observe in OOP implementations that you need help adapting to C? \$\endgroup\$
    – DMGregory
    Sep 9, 2019 at 23:23
  • \$\begingroup\$ Defining the different items and creating an instance of the item. \$\endgroup\$ Sep 9, 2019 at 23:40
  • \$\begingroup\$ Cool. Now what distinguishes those item types from one another? We see your type field — is that not sufficient for what you need? How have you tried instantiating an item so far? We really need to see what your requirements are and where you've hit a wall chasing them before we can help you find the appropriate door in that wall to get to where you want. \$\endgroup\$
    – DMGregory
    Sep 9, 2019 at 23:42

1 Answer 1

1
\$\begingroup\$

As for the way to store informations regarding items and stuff, there is nothing closer from a class in C than a struct. I would recommend using it.

As for retrieving datas from a file, I advise checking the Stack Overflow part of Stack Exchange considering it is dedicated to programming techniques and issues. A dozen of topics are about using files in C since its a bit tricky and can easily be done wrong (as everything in C I guess).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .