Documentation ¶
Overview ¶
GoWorld is a distributed game server engine. GoWorld adopts a Space-Entity framework for game server programming. Entities can migrate between spaces by calling `EnterSpace`. Entities can call each other using EntityID which is a global unique identifier for each entity. Entites can be used to represent game objects like players, monsters, NPCs, etc.
Multiprocessing ¶
GoWorld server contains multiple processes. There should be at least 3 processes: 1 dispatcher + 1 gate + 1 game. The gate process is responsable for handling game client connections. Currently, gate supports multiple transmission protocol including TCP, KCP or WebSocket. It also support data compression and encryption. The game process is where game logic actually runs. A Space will always reside in one game process where it is created. Entities can migrate between multiple game processes by entering spaces on other game processes. GoWorld server can scale arbitrarily by running more process.
Package goworld ¶
goworld package is dedicated to provide GoWorld game engine APIs for developers. Most of time developers should use functions exported by goworld package to manipulate spaces and entities. Developers can also use public methods of Space and Entity.
Run game ¶
GoWorld does not provide a game executable. Developers have to build their own game program. A common game program looks like bellow:
import "goworld" func main() { goworld.RegisterSpace(&MySpace{}) // Register a custom Space type // Register service entity types goworld.RegisterService("OnlineService", &OnlineService{}) goworld.RegisterService("SpaceService", &SpaceService{}) // Register Account entity type goworld.RegisterEntity("Account", &Account{}) // Register Monster entity type goworld.RegisterEntity("Monster", &Monster{}) // Register Player entity type goworld.RegisterEntity("Player", &Player{}) // Run the game server goworld.Run() }
Basically, you need to register space type, service types and entity types and then start the endless loop of game logic.
Creating Spaces ¶
Use goworld.CreateSpace* functions to create spaces.
Creating Entities ¶
Use goworld.CreateEntity* functions to create entities.
Loading Entities ¶
Use goworld.LoadEntity* functions to load entities from database.
Entity RPC ¶
Use goworld.Call* functions to do RPC among entities
Entity storage and attributes ¶
Each entity type should override function DescribeEntityType to declare its expected behavior and all attributes, just like bellow.
func (a *Avatar) DescribeEntityType(desc *entity.EntityTypeDesc) { desc.SetPersistent(true).SetUseAOI(true, 100) desc.DefineAttr("name", "AllClients", "Persistent") desc.DefineAttr("exp", "Client", "Persistent") desc.DefineAttr("lastMailID", "Persistent") desc.DefineAttr("testListField", "AllClients") desc.DefineAttr("enteringNilSpace") }
Function SetPersistent can be used to make entities persistent. Persistent entities' attributes will be marshalled and saved on Entity Storage (e.g. MongoDB) every configurable minutes.
Entities use attributes to store related data. Attributes can be synchronized to clients automatically. An entity's "AllClient" attributes will be synchronized to all clients of entities where this entity is in its AOI range. "Client" attributes wil be synchronized to own clients of entities. "Persistent" attributes will be saved on entity storage when entities are saved periodically. When entity is migrated from one game process to another, all attributes are marshalled and sent to the target game where the entity will be reconstructed using attribute data.
Configuration ¶
GoWorld uses `goworld.ini` as the default config file. Use '-configfile <path>' to use specified config file for processes.
Index ¶
- func AddCallback(d time.Duration, callback func())
- func AddTimer(d time.Duration, callback func())
- func Call(id EntityID, method string, args ...interface{})
- func CallNilSpaces(method string, args ...interface{})
- func CallService(serviceName string, method string, args ...interface{})
- func Entities() entity.EntityMap
- func Exists(typeName string, entityID EntityID, callback storage.ExistsCallbackFunc)
- func GetGameID() uint16
- func GetKVDB(key string, callback kvdb.KVDBGetCallback)
- func GetOnlineGames() common.Uint16Set
- func GetOrPutKVDB(key string, val string, callback kvdb.KVDBGetOrPutCallback)
- func GetServiceEntityID(serviceName string) common.EntityID
- func HGetKVDB(name string, key string, callback kvdb.KVDBGetCallback)
- func HPutKVDB(name string, key string, val string, callback kvdb.KVDBPutCallback)
- func ListAttr() *entity.ListAttr
- func ListEntityIDs(typeName string, callback storage.ListCallbackFunc)
- func LoadEntityAnywhere(typeName string, entityID EntityID, loadEntityID EntityID)
- func LoadEntityLocally(typeName string, entityID EntityID, loadEntityID EntityID)
- func LoadEntityOnGame(typeName string, entityID EntityID, gameid uint16, loadEntityID EntityID)
- func MapAttr() *entity.MapAttr
- func Post(callback post.PostCallback)
- func PutKVDB(key string, val string, callback kvdb.KVDBPutCallback)
- func RegisterCrontab(minute, hour, day, month, dayofweek int, cb func())
- func RegisterEntity(typeName string, entityPtr entity.IEntity) *entity.EntityTypeDesc
- func RegisterService(typeName string, entityPtr entity.IEntity)
- func RegisterSpace(spacePtr entity.ISpace)
- func Run()
- type Entity
- type EntityID
- type Space
- type Vector3
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddCallback ¶
AddTimer adds a timer to be executed after specified duration
func CallNilSpaces ¶
func CallNilSpaces(method string, args ...interface{})
CallNilSpaces calls methods of all nil spaces on all games
func CallService ¶
CallService calls a service entity
func Exists ¶
func Exists(typeName string, entityID EntityID, callback storage.ExistsCallbackFunc)
Exists checks if entityID exists in entity storage
returns result in callback
func GetGameID ¶
func GetGameID() uint16
GetGameID gets the local server ID
server ID is a uint16 number starts from 1, which should be different for each servers server ID is also in the game config section name of goworld.ini
func GetKVDB ¶
func GetKVDB(key string, callback kvdb.KVDBGetCallback)
GetKVDB gets value of key from KVDB
func GetOnlineGames ¶
GetOnlineGames returns all online game IDs
func GetOrPutKVDB ¶
func GetOrPutKVDB(key string, val string, callback kvdb.KVDBGetOrPutCallback)
GetOrPut gets value of key from KVDB, if val not exists or is "", put key-value to KVDB.
func GetServiceEntityID ¶
GetServiceEntityID returns the entityid of the service
func HGetKVDB ¶
func HGetKVDB(name string, key string, callback kvdb.KVDBGetCallback)
HGetKVDB gets value of key from KVDB
func HPutKVDB ¶
func HPutKVDB(name string, key string, val string, callback kvdb.KVDBPutCallback)
HPutKVDB puts key-value to KVDB
func ListEntityIDs ¶
func ListEntityIDs(typeName string, callback storage.ListCallbackFunc)
ListEntityIDs gets all saved entity ids in storage, may take long time and block the main routine
returns result in callback
func LoadEntityAnywhere ¶
LoadEntityAnywhere loads the specified entity from entity storage
func LoadEntityLocally ¶
LoadEntityLocally load entity in the local game If the entity already exists on any server, LoadEntityLocally will do nothing
func LoadEntityOnGame ¶
LoadEntityOnGame loads entity in the specified game If the entity already exists on any server, LoadEntityOnGame will do nothing
func Post ¶
func Post(callback post.PostCallback)
Post posts a callback to be executed It is almost same as AddCallback(0, callback)
func PutKVDB ¶
func PutKVDB(key string, val string, callback kvdb.KVDBPutCallback)
PutKVDB puts key-value to KVDB
func RegisterCrontab ¶
func RegisterCrontab(minute, hour, day, month, dayofweek int, cb func())
RegisterCrontab a callack which will be executed when time condition is satisfied
param minute: time condition satisfied on the specified minute, or every -minute if minute is negative param hour: time condition satisfied on the specified hour, or every -hour when hour is negative param day: time condition satisfied on the specified day, or every -day when day is negative param month: time condition satisfied on the specified month, or every -month when month is negative param dayofweek: time condition satisfied on the specified week day, or every -dayofweek when dayofweek is negative param cb: callback function to be executed when time is satisfied
func RegisterEntity ¶
func RegisterEntity(typeName string, entityPtr entity.IEntity) *entity.EntityTypeDesc
RegisterEntity registers the entity type so that entities can be created or loaded
returns the entity type description object which can be used to define more properties of entity type
func RegisterService ¶
RegisterService registeres an service type After registeration, the service entity will be created automatically on some game
func RegisterSpace ¶
RegisterSpace registers the space entity type.
All spaces will be created as an instance of this type
Types ¶
type Entity ¶
Entity type is the type of any entity in game
func CreateEntityLocally ¶
CreateEntityLocally creates a entity on the local server
returns EntityID
type EntityID ¶
EntityID is a global unique ID for entities and spaces. EntityID is unique in the whole game server, and also unique across multiple games.
func CreateEntityAnywhere ¶
CreateEntitySomewhere creates a entity on any server
func CreateEntityOnGame ¶
func CreateSpaceAnywhere ¶
CreateSpaceAnywhere creates a space with specified kind in any game server
func CreateSpaceOnGame ¶
CreateSpaceOnGame creates a space with specified kind on the specified game
returns the space EntityID
func GetNilSpaceID ¶
GetNilSpaceID returns the Entity ID of nil space on the specified game
type Space ¶
Space is the type of spaces
func CreateSpaceLocally ¶
CreateSpaceLocally creates a space with specified kind in the local game server
returns the space EntityID
func GetNilSpace ¶
func GetNilSpace() *Space
GetNilSpace returns the nil space on this game Nil space is a special space with Kind = 0. Nil space is the default space for all created entities. Each game has one nil space with fixed EntityID for each game, which can be acquired by calling `GetNilSpaceID`
Since nil game exists on each game with fixed EntityID, an entity can migrate to target game by calling `e.EnterSpace(GetNilSpaceID(gameid), Vector3{})`
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
components
|
|
engine
|
|
lib/gwsnappy
Package snappy implements the snappy block-based compression format.
|
Package snappy implements the snappy block-based compression format. |
ext
|
|