Need help with Type

BlitzMax Forums/BlitzMax Beginners Area/Need help with Type

RustyKristi(Posted 2016) [#1]
I'm currently translating my work to BMX, with the definitions I'm getting this error and was wondering what is the correct type declaration/translation

Type Foo
Field x
Field y
Field z
Field a.Foo
End Type

I'm still not used to Max conventions and can't seem to find an advanced example quickly using Types so I'm getting error using the a.Foo field where this works with B3D.

I'm guessing the correct usage is a:Foo but I have not fully tested it yet.


Floyd(Posted 2016) [#2]
BlitzMax uses : for the type tag.

You can use n% and x# for Int and Float but written out in full they are n:Int and x:Float.

Likewise a variable of user defined type would look like t:whatever.

You will see many examples with names beginning with T, in your case TFoo. That's not enforced by the language but is a generally accepted BlitzMax convention.

Also, the . is used instead of the old Blitz3D \ style. Here is a trivial example.

Type TFoo
	Field n:Int
	Field x:Float
	Field f:TFoo
End Type

a:TFoo = New TFoo

a.n = 7

Print
Print a.n

a.f = New TFoo
a.f.x = 1234.5

Print a.f.x



RustyKristi(Posted 2016) [#3]
Thanks Floyd, I got this now but getting to more complicated code, how do you create new type inside it's own function? I'm getting identifier not found under Strict.

Type TFoo
  Field a
  Function b:TFoo(z)
    c:TFoo = New TFoo
  End Function
End Type



BlitzMan(Posted 2016) [#4]
Do you need a z field for type TFoo.Just a guess Types do my swede in aswell.


Brucey(Posted 2016) [#5]
SuperStrict

Local t:TFoo = TFoo.b(100)

Type TFoo
  Field a:Int
  Function b:TFoo(z:Int)
    Local c:TFoo = New TFoo
    c.a = z
    Return c 
  End Function
End Type

The code can be also improved by using more verbose (like proper words) variable names.


BlitzMan(Posted 2016) [#6]
I know Brucey is the master,but this has still blown my mind.

I know you are setting up 100 TFoo`s
With a field a
and you are calling a function of 100 TFoo`s
but you have c and z .Am i missing something simple or are they dummy vars.


RustyKristi(Posted 2016) [#7]
@Blitzman

Yes, I'm doing a migration from Blitz so that will be needed.

@Brucey

Thanks, the Local keyword fixed my problem. I agree, sorry I just did this for this quick example.

My next step would be properly converting a ForEach and List of that type inside a function which is a bit tricky for me as a bmx noob.

So my question is, how do simply replicate the foreach routine using a type inside a function of that particular type?

This is the blitz code equivalent

        

Type Foo
        Field a
End Type

...

Function b.Foo(z)

        c.Foo = New Foo
       
        ...

        c\a = z
        cnt = 0
        For i.Foo = Each Foo
                If i\a = c\a Then cnt = cnt + 1
        Next
      
        ...


My setup would be inside that b:TFoo function as an example


Henri(Posted 2016) [#8]
Hi,

Blitzmax doesn't store object instances internally like BlitzD3 or Blitz+ I presume. Instead you store them inside a list (TList type).


Example 1:
Type Foo
	Field a:Int
	
	Global list:TList
	
	Function CreateFoo(param:Int)
	
		Rem
		List is just like any custom object which can be stored anywhere,
		but in this example we are storing it inside our type.
		EndRem
		
		If Not list Then list = New TList
		
		Local f:Foo = New Foo
		f.a = param
		
		'Add the new instance to the end of our list.
		list.addlast(f)
		
	EndFunction
	
	Function IterateFoo()
		
		'Just to be on the sure side. It's always good to check before accessing potential null object.
		If Not list Then Return
		
		For Local f:Foo = EachIn list
			Print f.a
		Next
	EndFunction
End Type


'Create some Foo's
For Local i:Int = 1 To 10
	Foo.CreateFoo(i)
Next

'Iterate our Foo's
Foo.IterateFoo() 


-Henri


RustyKristi(Posted 2016) [#9]
Thanks Henri, I have encountered this before and it seems to be the most confusing part. I understand now that it does not store instances and so, the process would be to put every function related to list inside its type and just make the call?


Henri(Posted 2016) [#10]
No you don't have to. Often though it is convenient to keep everything related to any particular type inside that type.

I usually have 2 brands of types: Functional type which operates with it self (has Methods and functions), and second is a data type that essentially is a data storage (temporary or permanent)


Example of data storage:
Strict

Local list:TList = GetInformation()
If list
	Print "Got some info..."
	
	For Local p:TPoint = EachIn list
		Print p.x + " | " + p.y + " | " + p.z
	Next
EndIf


Type TPoint
	Field x:Int, y:Int, z:Int
EndType


Function GetInformation:TList()
	
	Local list:TList = New TList
	
	Local p:TPoint
	
	For Local i:Int = 1 To 20
		
		If i Mod 2
			
			p = New TPoint
			p.x = i
			p.y = i * 2
			p.z = i * 3
			
			list.addlast(p)
		EndIf
	Next
	
	Return list
EndFunction


-Henri


TomToad(Posted 2016) [#11]
Check out this tutorial by John J.

PDF file on Brucey's site
http://brucey.net/programming/blitz/misc/library/BlitzMax_OOP_Tutorial.pdf

Original thread
http://www.blitzbasic.com/Community/posts.php?topic=59233#662843


Cocopino(Posted 2016) [#12]
@Blitzman

I know you are setting up 100 TFoo`s With a field a


Actually, that's not what's happening in post #5.

A variable named "t" is set to a custom made variable type TFoo.
The value for "t" is the returned value of static function "b" with parameter "z" set to 100

In function "b", a new TFoo is made ("c") with field "a" set to 100 and returned.
So in the end, t.a equals 100


Using more proper names, this is a nice way to create a new Type having some predefined field values.

Maybe this is more readable:

SuperStrict

Type THuman
	
	Field FirstName:String
	Field LastName:String
	Field AgeInYears:Int

	Function Create:THuman(fn:String, ln:String, age:Int)
		Local human:THuman = New THuman
		human.FirstName = fn
		human.LastName = ln
		human.AgeInYears = age
		
		Return human
	End Function

End Type

Local person:THuman = Thuman.Create("Foo", "Bar", 30)
Print "Personal data: " + person.FirstName + " " + person.LastName + ", age " + String(person.AgeInYears)



RustyKristi(Posted 2016) [#13]
Thanks guys for the info and tips. I could definitely do some use case to better understand how the BMX list/foreach works.


Cocopino(Posted 2016) [#14]
@RustyKristi
Some OOPy code using types in a list:



Note that when using lists, this will not work:
Local rect:TRectangle = list.ValueAtIndex(3)


Since a list can contain all kinds of variables including custom ones, you'll need to cast items found in the list to TRectangle first:
Local rect:TRectangle = TRectangle(list.ValueAtIndex(3)) 



RustyKristi(Posted 2016) [#15]
thanks! ill check it out :)