#Design for a modular update system for a game engine
5 messages · Page 1 of 1 (latest)
This is my current code ```golang
//Scene
type Scene struct {
Objs []Obj
}
func NewScene() Scene {
return Scene{}
}
func (scene *Scene) Update() {
for i := range scene.Objs {
scene.Objs[i].Update()
}
}
func (scene *Scene) Instance(obj Obj) {
scene.Create(obj.Mods...)
}
func (scene *Scene) Create(mods ...Mod) Obj {
scene.Objs = append(scene.Objs, Obj{ID: int32(len(scene.Objs)), Mods: mods})
return scene.Objs[len(scene.Objs)-1]
}
func (scene *Scene) Delete(id int32) {
for i, obj := range scene.Objs {
if obj.ID == id {
scene.Objs = append(scene.Objs[:i], scene.Objs[i+1:]...)
return
}
}
}
// Object
type Obj struct {
ID int32
Mods []Mod
}
func NewObject(mods ...Mod) Obj {
return Obj{Mods: mods}
}
func (obj *Obj) Update() {
for _, mod := range obj.Mods {
mod.Update()
}
}
func (obj *Obj) AddModules(mods ...Mod) {
obj.Mods = append(obj.Mods, mods...)
}
func (obj *Obj) RemoveModules(comps ...Mod) {
for _, compToRemove := range comps {
for i, comp := range obj.Mods {
if comp == compToRemove {
obj.Mods = append(obj.Mods[:i], obj.Mods[i+1:]...)
break
}
}
}
}
// Module
type Mod interface {
Update()
}
My goal is to make so you can create a object witch is like a "prefab" that I can later use in the scene.Instance() function witch basicaly clones the "prefab" and adds a ID to it, my problem is that I cant acces the modules from the object, but this system might just be terrible but I cant come up with any other system design...
you probably need to update Mod to have some way to either create them with an object, or to add 2 methods to the interface, SetObject(o *Obj) and Object() *Obj to get back that information