简体   繁体   中英

Use the same data between server and client

I'm designing a network only game. I would like to share game data—stored in C++ classes—between the server side and client side.

This has the benefit of having the data in the same place, simplifying maintenance. However, I have a problem with the class design because:

  1. On the server side only the logic is needed
  2. On the client side you want to draw, render, and such

In other words, I don't want to install the graphics library on the server and vice-versa.

The class may look like this:

class Fire : public Spell {
private:
    /* some data */

public:
    void execute(Player &p)
    {
        // call server actions
    }

    void draw(Player &p)
    {
        // call rendering and such
    }
};

Execute is the function for the server only and draw is for the client. I really don't want to use macros to determine on which side the data is because it tends to be ugly and unmaintainable.

I'm looking for a well-designed way to accomplish this.

What would you do / use?

I think You should reach the conclusion you need to split the classes and make two classes starting from Spell:

//put here all common spell things
class Spell{
};

//add to spell the server logic
class SpellLogic: public Spell{
public:
   virtual void execute(Player &p);
};

//add here the client graphic
class SpellGraphic: public Spell{
public:
   virtual void draw(Player &p);
};

each spell derivation will be splited to two.

clients will use the Graphics classes, server will use the logic.

while I agree with @SHR this probably means you need to separate classes, there are situations where it makes sense to share code between client and servers (especially when involving data representation or when invoking code from the other side models your flow better than messaging)

in that case I would suggest a good RPC library such as google's protocol buffers

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