Jump to content
  • Advertisement
  • entries
  • comments
  • views

Shared: The Object

Sign in to follow this  


Workin' on 'Shared' now.

Shared is a base class (probably better described as a pattern) that I came up with quite a while ago for the 'new' games (eventually released as the Cosmi game pack). They were originally designed as a friendly abstraction for networked games, but it turns out that they were frightfully useful for separating the GUI from the game logic. It was certainly an example of an idea that outgrew its original vision, as I ended up pulling the network functionality out of the games, but I still used shared.

Come to think of it, I probably ought to think of a better name than 'Shared'. Doesn't do it justice.

How it originally worked was a 'shared object'. What it was was an object that was shared between multiple computers. If a computer wanted to change member data in the object, it simply had to change the data and call Update(), which would automatically stream the changes to the other computers, change their member data, and call the WasUpdated() function, informing them that they had changed. The games themselves built little token-passing mechanisms on top of this, so that there weren't lost updates.

Anyway, here's the public interface. It's based on the old version, but it's not much different.

class Shared



Shared(ui8 ObjIndex);


void Update(void);

virtual void WasUpdated(void)=0;

virtual void Exchange()=0;

void ExchangeItem(i8 &rItem);

void ExchangeItem(ui8 &rItem);

void ExchangeItem(i16 &rItem);

void ExchangeItem(ui16 &rItem);

... // all primitive data types will have an associated Exchange();

ui8 Index(void) { return MyIndex; }

// note: the following are stubbed out for now. They should be set

for networked apps

bool Networked() { return false; }

bool Primary() { return true; }


What was neat thing I found, though, was that by sharing an object with

itself (which was the plan for single player games), this makes a dandy class for state machines.

A sample state machine is the following. Gameinfo is derived from Shared,

and contains game-specific info. This is adapted from Wari, which is a simple two-player game:

void GameInfo::WasUpdated(void)

{ // this should look at the object and figure out what's going on

ui8 CommandOrg = Command;





if (MyTurn()) // see if you are player 1 or 2 (always 1 if not networked)

{ // set up the move

[tell UI that it's now valid for the user to click the screen to

make a move]

[if user clicks a cell, UI must set Command to CMD_1MOVED and call



else if (!Networked())

{ // perform a computer move

Wait(); // show the wait cursor

[perform the math on the board to find the best move, storing it in LastMoveMade]

Wait(FALSE); // hide the wait cursor

Command = CMD_2MOVED; // signal that the computer's made his move



case CMD_1MOVED:

case CMD_2MOVED:

{ // apply the move to the board

MainWindow().MakeMove(LastMoveMade); // display the move just made

on the UI

if (!MainWindow().GameOverProcessing()) // check for a game over condition

{ // if no game over, tell the next player it's his turn

if (Command == CMD_1MOVED)

Command = CMD_2TOMOVE; // next player's turn


Command = CMD_1TOMOVE;






MainWindow().NewGame(); // have the GUI set up a new game

if (Primary())

{ // flip a coin to see who goes first

if (Application::Random(2))

Command = CMD_1TOMOVE; // you won the toss!


Command = CMD_2TOMOVE; // lost the toss, computer goes first





if (Command != CommandOrg) // there is a new command, signal it



The Exchange() function looks like this. . .

void GameInfo::Exchange(void)





When you call Update(), it'll use this function to build a packet out of the items given. When the object receives a packet, it uses the same function to see what the items in the packet look like. In the old version, I had two different functions to stream the data in and out. I realized, though, that they were always the same except for the direction of the streaming. In the new version, I'll just put the object into 'send' or 'receive' mode depending on what's happening and use this function to make a packet from the data or to extract from the packet to the data..

Betcha didn't know that so much thought went into discount-rack games :)
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!