now a beginer TYPE question

BlitzMax Forums/BlitzMax Beginners Area/now a beginer TYPE question

mudcat(Posted 2005) [#1]
In this type,there is a global
type score
  global list:tlist=creatlist()
end type


Will list creat a list everytime I create an object?Or is it a one time deal?Just wondering about the global and if it has an effect on it.
And if I don't create an object...will there be a created list?

Thanks,
mudcat


Perturbatio(Posted 2005) [#2]
globals in a type declaration are only initialized once.


mudcat(Posted 2005) [#3]
o.k.,
Then why does all the codes samples (well, not all)show this in a create function?
If list=Null list=CreateList()


Couldn't you just use my first example?And be done with it?


Perturbatio(Posted 2005) [#4]
there was a point where you couldn't initialize a global variable like that, so the code was written in a different manner.


bradford6(Posted 2005) [#5]
real question is. Why are you initializing a Global list inside a type declaration.



Type score
  Field list:tlist=CreateList()

  	Method add_score(score:String)
		ListAddLast(list,score)


	End Method
	
	Method show()
		Print "Score Table"
		For Local i$ = EachIn list
			Print i
		Next
	End Method


End Type



foo:Score = New score
foo.add_score("100")
foo.add_score("200")

foo.show()



taxlerendiosk(Posted 2005) [#6]
real question is. Why are you initializing a Global list inside a type declaration.

I can think of reasons. You might want to hold a list of all instances of that type, emulating Blitz Plus/3D's type lists.


klepto2(Posted 2005) [#7]
Another Example for Global lists in types would be this:

Type Tparticle
	Global particle_list:TList
	
	Field x,y
	Field Emitter
	...
	
	Function create()
	Particle_List.Addlast(Particle)
	End Function
	
	Function Render()
	For Local p:Tparticle =  EachIn particle_list
	p.Update()
	Next
	End Function
	
	Method Update()
	...
	End Method
	
End Type

While Not KeyHit(Key_Escape)

TParticle.Render()

Wend




mudcat(Posted 2005) [#8]

real question is. Why are you initializing a Global list inside a type declaration.



well,maybe I not getting a hand on OOP programming.I was thinking so I can use
score.list

without an instance.Or is that bad programming?


tonyg(Posted 2005) [#9]
Not saying I used good programming techniques either but that's why I do it and I follow the method posted by Klepto2.


bradford6(Posted 2005) [#10]
I am certainly not saying that my way is better, good or even 'right'

I just don't understand why you would want to use a GLobal.

I was under the impression that Locals are more efficient.

anyway. all of the methods work. That's the beauty of programming with Max, there is more than one way to do it!


Perturbatio(Posted 2005) [#11]
I was under the impression that Locals are more efficient.

A list will not work well as a local anyway since it's going to be too big to be held in registers.

A global list in the type is useful for keeping things neat and tidy.


Arowx(Posted 2005) [#12]
I've been wracking my brain cells (all 3 ;o) to work out why I wouldn't do it this way as I don't and I guess I need to justify why I don't!

So here goes my thoughts...

1. You can only ever have one container class.

2. You could find that extending the class to deal with new features may be more complex than needed, for example Sorting the list, ToString.

3. Encapsulating changes is a good way to work with OO this approach might get in the way of this (see 2).

Apart from that I kind of like it as it obeys the K.I.S.S. rule "Keep It Simple Stupid" which for some bizarre reason I often forget!


Perturbatio(Posted 2005) [#13]
1. You can only ever have one container class.

What do you mean by this?