Faster way of creating tables

Find quick help here to get you started with Hollywood
Post Reply
tolkien
Posts: 195
Joined: Sun Oct 17, 2010 10:40 pm
Location: Spain

Faster way of creating tables

Post 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!
jalih
Posts: 281
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Faster way of creating tables

Post 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.
tolkien
Posts: 195
Joined: Sun Oct 17, 2010 10:40 pm
Location: Spain

Re: Faster way of creating tables

Post by tolkien »

I´ll study it later but thanks for the answer. I´m your padawan! :)
tolkien
Posts: 195
Joined: Sun Oct 17, 2010 10:40 pm
Location: Spain

Re: Faster way of creating tables

Post by tolkien »

Hey! That works excellent. So simple so good! Thanks again.
Post Reply