Program structure

Started by Sinjin, September 20, 2023, 18:43:54

Previous topic - Next topic

Sinjin

If you have you game variables in a type
type tgame
  field objs:tobj 'those are what you see in game
  field GlobalAndGameRealtedVars...'colors for example
endtype
and all the objects like
type tobj
  field severalthings...
endtype
I hear ppl like: make everything LOCAL, but how? I need variables in my obj-types from the tgame-type. So far I do this by having a GLOBAL var.
global game:tgame=new tgame
type tobj
  method draw()
    setcolor game.r,game.g,game.b
  endmethod
endtype
What is the best approach here? Like so?
type tobj
  method draw(gamevars:tgame)
    setcolor gamevar.r,gamevar.g,gamevar.b
  endmethod
endtype

Also I kinda have all objects loaded but in a global variable too, and when I actually put it in my game, I make a new instance from that obj and just copy all the variables to the new instance which then does comes visible/available with all the necessiary on-time calculations. (the obj in the back, of course those just sit there ready to be copied, which I think is fine so far, I would switch to actually load them from disk if I would have 1000 objects of course.)
type tobj
  global allobjs:tobj[]

  method newfromobj(obj:tobj)
  endmethod

  method draw() 'etc
  endmethod
'other methods here
endtype

type tplayer extends tobj
  field individualvars...

  method newfromobj(obj:tobj) 'find the player/obj in tobj.allobjs, find? since I dont know where its located in the allobj thing?
    'copy the individual vars here
  endmethod
endtype
Is that the best structure already or how you do it?