Documentation
¶
Overview ¶
Package datatablewatch is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package datatablewatch adds hot reload to datatable.Table through a tooling/hotreload.Watcher.
It lives outside datatable so a headless build, such as a dedicated server, imports datatable without NSGo: only the game client links the watcher.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Watch ¶
Watch registers tbl's file with watcher and invokes onReload with the updated rows on the engine goroutine whenever a valid edit is saved.
Invalid edits keep the previous valid rows intact and record the error in tbl.LastError.
Example ¶
Watch registers a table with the development file watcher, so saving an edit reloads the rows into the running game without a restart.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/nsgo-dev/game/pkg/datatable"
"github.com/nsgo-dev/game/pkg/datatable/datatablewatch"
"github.com/nsgo-dev/game/pkg/tooling/hotreload"
"github.com/nsgo-dev/nsgo/core"
)
type enemy struct {
ID string `table:"enemy_id"`
HP int `table:"hp"`
}
// Watch registers a table with the development file watcher, so saving an edit reloads the rows into the running game without a restart.
func main() {
dir, err := os.MkdirTemp("", "datatablewatch")
if err != nil {
fmt.Println(err)
return
}
defer os.RemoveAll(dir)
path := filepath.Join(dir, "enemies.csv")
if err := os.WriteFile(path, []byte("enemy_id,hp\ngoblin,40\n"), 0o644); err != nil {
fmt.Println(err)
return
}
tbl, err := datatable.NewTable[enemy](path)
if err != nil {
fmt.Println(err)
return
}
root := core.NewRoot(core.RootOptions{})
defer root.Dispose()
watcher := hotreload.NewWatcher(root, dir)
defer watcher.Stop()
datatablewatch.Watch(tbl, watcher, func(rows []enemy) {})
fmt.Println(len(tbl.Rows()), tbl.Version())
}
Output: 1 1
Types ¶
This section is empty.