Skip to content
This repository was archived by the owner on Jun 12, 2019. It is now read-only.

Handle events

Tnze edited this page Jan 19, 2019 · 6 revisions

Simple Events

After you go g.HandleGame(), you have to get event channel by events := g.GetEvents() and receive Events from events. The events hasn't buffer so if you don't receive Event, game handler will be block.

Generally, you should handle events after you go g.HandleGame() like this:

for e := range events {
	switch e {
	case mb.PlayerSpawnEvent:
		fmt.Println("Player is spawn!")
	case mb.PlayerDeadEvent:
		fmt.Println("Player is dead")
	default:
		fmt.Println("Other event: ", e)
	}
}

Chat Event

Chat message is not sending by previous. However, if you want to handle chat messages, you should set a callback function:

game.SetChatCallBack(func(msg string, pos byte) {
	if pos == 0 {
		fmt.Println(msg)
	}
})

The pos can be 0, 1 or 2. It tells where client should display this message:

  • 0: chat (chat box)
  • 1: system message (chat box)
  • 2: game info (above hotbar)

The msg is a JSON string which format you can found here

Clone this wiki locally