Store info about two objects' relationship

BlitzMax Forums/BlitzMax Programming/Store info about two objects' relationship

JoshK(Posted 2007) [#1]
I want to store some info about two objects. Specifically, I want to store the state between a light and an entity, so I only have to re-check visibility when something moves.

Previously I have done this by creating a type that stored the info, and storing it in lists in both the entity and light. I was wondering if there was a faster simpler way to do this, something like TMap that stored a value for two objects.

This is the idea I am getting at:
Info = GetValue(entity,light)?


My thinking from my terrain system is carrying over to this. I could just create a big "texture", and just use the entity handle as the x or y position to retrieve info. It's the same idea as reading data from a pixmap.

Of course, this would be somewhat redundant, since they could be linked a-b or b-a (either entity could be x or y) so the graph would be twice as big as is necessary.

Any ideas?


JoshK(Posted 2007) [#2]
I am still thinking about this, and I have come to a point where it is necessary.

I want something like this:

SetObjectLink(o1:Object,o2:Object,link:Object)
GetObjectLink:Object(o1:Object,o2:Object)

I am thinking a TMap with an intermediate type might be the way to go, but I don't know how I would sort the type.


Gabriel(Posted 2007) [#3]
Since you're essentially using a 2d lookup table, why not just us an array? Index your lights and entities numerically and then it's a straight lookup, no time spent hunting for things.


JoshK(Posted 2007) [#4]
An array won't work because you have to be able to create and destroy objects on the fly.

Check this out. You can store an object for any two objects.

Can Mark or someone who understand this better tell me whether this is great or stupid? What do I need to do to clean up memory for this?

Global ObjectMap:TMap=New TMap

Type TParternship
	Field o1:Object
	Field o2:Object
	Field h1:Int
	Field h2:Int
	
	Method Compare:Int( with:Object )
		p2:TParternship=TParternship(with)
		If h1>p2.h1 Return 1
		If h1<p2.h1 Return -1
		If h2>p2.h2 Return 1
		If h2<p2.h2 Return -1		
		Return 0
	End Method
	
	Method Delete()
		Release h1
		Release h2
		o1=Null
		o2=Null
	EndMethod
	
	Function Create:TParternship(o1:Object,o2:Object)
		p:TParternship=New TParternship
		p.o1=o1
		p.o2=o2
		p.h1=HandleFromObject(o1)
		p.h2=HandleFromObject(o2)
		If p.h1>p.h2
			h3=p.h2
			p.h2=p.h1
			p.h1=h3
			o3:Object=p.o2
			p.o2=p.o1
			p.o1=o3
		EndIf
		Return p
	EndFunction
	
EndType

Function SetObjectPartnership(o1:Object,o2:Object,link:Object)
	p:TParternship=TParternship.Create(o1,o2)
	If link=Null
		ObjectMap.Remove p
	Else
		ObjectMap.Insert p,link
	EndIf
EndFunction

Function GetObjectPartnership:Object(o1:Object,o2:Object)
	p:TParternship=TParternship.Create(o1,o2)
	Return ObjectMap.ValueForKey(p)
EndFunction

Function ClearObjectPartnerShips(o:Object)
	For p:TParternship=EachIn ObjectMap.Values()
		If p.o1=o Or p.o2=o ObjectMap.Remove p
	Next
EndFunction

'===================================================================
'Test
'===================================================================

Type TA
EndType

a:TA=New TA
b:TA=New TA
c:TA=New TA

SetObjectPartnership(a,b,"Hello")
SetObjectPartnership(a,c,"Goodbye")

s$=String(GetObjectPartnership(a,b))
Notify s

s$=String(GetObjectPartnership(a,c))
Notify s