Page 1 of 1

Faster way of creating tables

Posted: Thu Dec 01, 2011 12:35 am
by tolkien
Let´s imagine I want a table mytable={x=0,y=0,fire=0,etc,etc}
If I try to get more tables like that with index to can write
mytable.x=100 (for example)
we have to write
mytable={{x=0,y=0,fire=0,etc,etc},{x=0,y=0,fire=0,etc,etc},{x=0,y=0,fire=0,etc,etc}} and so on...very tedious...

Is there a way to do something similar to STRUCT in C or NEWTYPE in Amiblitz?

Is there a way to do it in a better/faster form? Am I too old to learn? ;)

Thanks!

Re: Faster way of creating tables

Posted: Thu Dec 01, 2011 9:06 am
by jalih
Creation of objects is very much possible with tables.

Code: Select all

Obj = {}

; Return new zeroed obj
Function Obj:new()
	Local o = {}
	o.x = 0
	o.y = 0
	o.fire = False	
	Return(o)
EndFunction


; Return new obj with init values
Function Obj:init(x, y, fire)
	Local o = { x = x, y = y, fire = fire)	
	Return(o)
EndFunction


; Example of usage
Local newObj1 = Obj:new()
Local newObj2 = Obj:init(10, 10, False)
Naturally you can use loop to initialize multiple objects or make a separate object for list of objects.

Re: Faster way of creating tables

Posted: Thu Dec 01, 2011 6:47 pm
by tolkien
I´ll study it later but thanks for the answer. I´m your padawan! :)

Re: Faster way of creating tables

Posted: Tue Dec 06, 2011 12:09 pm
by tolkien
Hey! That works excellent. So simple so good! Thanks again.