简体   繁体   中英

How to implement an inventory system for a game?

How would I go about implementing an inventory system for a game in PHP? I've been trying to figure out a way so that items can be dynamically used instead of just sitting in your inventory, eg equipping weapons, using potions, teleporting, etc.

The only solution I can come up with is to store PHP code in the database, and fetch it when the item is used, but that doesn't really seem appropriate. Is there a better way to implement this?

To clarify: It's an mmo browser game, and the inventory of each player needs to persist through the game. Having a class for each item (There may be hundreds of items) seems difficult to maintain if any changes were to be made.

In addition to implementing a 'baseItem' class as suggested, you may also want to considering developing an interface class for outlining the methods that all items should have. Then, each type of item could implement these methods differently.

For example, all items might need to have an 'equip()' method, but implement it differently based on which kind of items it is. You may not need to create a class for each individual item, but you should probably create classes which are capable of creating unique instances of items based on some sort of data structure you provide (which can be stored in the database).

A really general example might be:

class Leather_Armor extends Armor_Base implements Equippable
{
     // these would probably be defined in the base class instead of here
     protected $_itemName;
     protected $_itemDescription;
     protected $_defenseRating;

     public function __construct(params)
     {
         // intialize properties with data from the db
     }


     // the Equippable interface requires us to define this function
     public function equip()
     {
        // call a function from the base class
        $this->recalculateDefenseRating($this->defenseRating)
     }

}

You should probably read up on interfaces and abstract classes to fully get the idea.

Also, be aware that this is a really broad, open-ended question than can be approached a thousand different ways. You might want to ask something more specific, or provide concrete examples.

Using PHP you'll need to set up an inventory class, and classes for each of your items - including a "baseItem" class that items inherit from which includes things common to all items (icon? sound? amount?).

You'll need to store the user's instance of the inventory class in a session variable. If you want it to persist it should be in a database.

Any page that accesses the inventory will need to reference the file that contains the item and inventory classes.

This link shows setting an object to a session and accessing it across pages.

EDIT: typed too fast and mixed up a couple key terms

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM