Help with array of types

BlitzPlus Forums/BlitzPlus Programming/Help with array of types

Timjo(Posted 2007) [#1]
Hi....I Need some help with arrays of types:

I know that you can create an array within a type's field(with square brackets) like this(I think):

eg: Type thing
Field stuff[10]
End Type

This is easy to figure out, but I've recently found out that you can make an array of the whole type

eg: using the above example - Dim things.thing(50)


I'm not sure how to address each array element and field etc - what's the format for this?

Also - can you create a different number of New objects in each array element
ie: Array element no. 1 has 10 thing objects
Array element no. 2 has 15 thing objects
etc......

I've just begun to get my head around types in general - this extra dimension stuff is very useful but can be a bit confusing at first -and there's not much about it in the docs yet.
Any help with this and tips for using types in general will be of great help. Thanks.


Moore(Posted 2007) [#2]
Each array element can hold only one object. BUT that object could be a part of a link list. Then you could have an virtually unlimited number of object "pointed" to by each array element. If you are unfamiliar with link lists I would be happy to post a demo. ( The below code worked for addressing individual array element, object fields.)

Type Thing
 Field x
 Field prv.Thing
 Field nxt.Thing
End Type

Dim things.Thing(50)

Print "Array type test..."
things(0) = New Thing
things(0)\x = 10
Print things(0)\x
WaitKey()



Timjo(Posted 2007) [#3]
Thanks Moore - that sorts 'things' out a bit for me.
I'm supposing the fields prv.Thing and nxt.Thing are to create the linked list so you know the order of all your objects - actually - a demo would be quite handy.
Anyway, thanks for the help with addressing objects/fields.


Moore(Posted 2007) [#4]
Hope this helps! :)
; This demo shows the use of double or bi-directional LINK LISTS and CONTAINER CLASSES
; A LINK LIST is a common way of using pointers to string objects together
; each object will contain a pointer to the next object in the list and the previous object in the list
; The pointer to the first object in the list is often called te HEAD
; The previous pointer of the first object in the list will be NULL
; The next pointer of the the last object in the list will be NULL
; If the HEAD is the only object in the list both the previous and next pointers will be NULL
; If there are NO objects in the list then the HEAD pointer will be NULL

; A container class is a class that points to the object that we are working with as well as the next
; and previous objects in the LINK LIST. This allows and object to be part of many LINK LISTS and not 
; just one, which is what would result if the DATA OBJECT (that is the main object that we are
; working with that contains the real data) contained the nxt/prv links.

; Check out the wikipedia.org artical on link lists for more info
; By Naphtali Moore, email me at NRMStudios at my yahoo.com account

Type Guild
	Field Name$, Description$
	Field head.gMember
End Type

Type gMember ; This is our container class
	Field c.character
	Field prv.gMember
	Field nxt.gMember
End Type

Type character ; 
 Field Name$, HP, Def
End Type

curGuild.guild = new_Guild("Super Heros", "A group of powerful heros.") ; Create a Guild
; Create characters
p1.character = new_Character("Super Man", 100, 100)
p2.character = new_Character("Spider-Man", 40, 60)
p3.character = new_Character("Batman", 50, 75)

; Add characters to our guild
add_gMember(curGuild, p1)
add_gMember(curGuild, p2)
add_gMember(curGuild, p3)

; list the members
listMembers(curGuild)

;remove a member and list again
remove_gMember(curGuild, p2)
Print "": Print "Spider-Man has been removed." : Print ""
listMembers(curGuild)

;remove a member and list again
remove_gMember(curGuild, p1)
Print "": Print "Super Man has been removed." : Print ""
listMembers(curGuild)

;remove a member and list again
remove_gMember(curGuild, p3)
Print "": Print "Batman has been removed." : Print ""
listMembers(curGuild)

Print " "
Input("Please hit [ENTER] to end program.") ; wait for return to end
End

Function listMembers(g.Guild)
	If g = Null Return
	temp.gMember = g\head
	Print "Guild Name: " + g\name
	Print "========= MEMBER  LIST ========="
	If temp = Null Then Print "This guild has NO members."
	While temp <> Null
	    i = i + 1
		Print i + ") " + temp\c\name + " HP:" + temp\c\hp + " DEF:" + temp\c\def 
		temp = temp\nxt
	Wend
End Function

Function new_character.character(name$, hp, def)
	c.character = New character
	c\name = name
	c\hp = hp
	c\def = def
	Return c
End Function

Function new_Guild.Guild(name$, descrpt$)
	g.Guild = New Guild
	g\name = name
	g\description = descrpt
	Return g
End Function

Function add_gMember(g.guild, c.character)
	If g = Null Or c = Null Then Return False ; This protects against errors
	gM.gMember = New gMember ; create a new container object
	gM\c = c ; assign character to container object
	
	If g\head = Null Then ; if member list is empty
		g\head = gM ; assign new object to first object of the list
		Return True ; return possitive result
	EndIf
		
	temp.gMember = g\head ; get first object in list
	While temp\nxt <> Null ; continue through list till you reach the last object
		temp = temp\nxt ; move to next object
	Wend

	temp\nxt = gM ; point the last object to the new object 
	gM\prv = temp ; point the new object to the last object
	Return True	  ; return positive result	
End Function

Function remove_gMember(g.guild, c.character)
	If g = Null If c = Null Then Return False ; This protects against errors
	If g\head = Null Return False
	temp.gMember = g\head
	
	If g\head\c = c Then ; character is in first object of list
		If g\head\nxt <> Null Then ; character is not only object in list
			g\head = g\head\nxt ; make second object in list the first object in list
		Else ; character is only object in list
			g\head = Null 
		EndIf
	Else
		While temp\c <> c ; while object does not contain the character we are removing
			temp = temp\nxt ; move to next object
			If temp = Null Then Return False ; object not in list return negitive
		Wend	
		If temp\nxt <> Null Then ; character is in middle of list
			temp\prv\nxt = temp\nxt ; point the prv object to the nxt object
			temp\nxt\prv = temp\prv ; point the nxt object to the prv object
		Else ; character is in last object in list
			temp\prv\nxt = Null ; make prv object last object in list
		EndIf
	EndIf
	
	temp\c = Null ; remove pointer to our character
	Delete temp ; delete the object containing our character
	Return True	; return a positive result	
End Function



Timjo(Posted 2007) [#5]
Many Thanks, Moore. I shall pick my way through this and see if I can get my head 'round it. Cheers.