Pointers in types??

Blitz3D Forums/Blitz3D Programming/Pointers in types??

PsychicParrot(Posted 2005) [#1]
Ello again, my Blitz-addicted friends!!

Eres one I just can't figure ... can you put a pointer to another type IN a type?

Heres the situation .. I have a shed-load (200+) of waypoints stored in types. Rather than iterate through them all looking for an ID, since they're always read in sequence (start to finish!) it would be great if it was possible to put a pointer to the the current waypoint in the player's type. That way, I could just use AFTER to move along to the next waypoint... in theory!??!!

Anyone tried this or know if it'll work? I started out with this test code to see if it worked in principle, but my head is frazzled and I'll have to go away and think about it all a bit before carrying on!!

Type norbert

	Field number

End Type

Type hismate

	Field norb.norbert

End Type

For i=1 To 100

	a.norbert=New norbert
	a\number=I

Next

For i=1 To 4

	b.hismate=New hismate
	
	b\norb=First norbert
	
Next

c=1
d.hismate=First hismate

For i=1 To Rnd(10)
d\norb=After d\norb
Next

	For i=1 To 4
			Print i+".."+d\norb\number
			d=After d
	Next

While Not KeyHit(1)
Wend



Jeppe Nielsen(Posted 2005) [#2]
I think this is what you're after:


Graphics 800,600,16,2

Type waypoint

	Field number
	Field x#,y#
	
End Type

Type player
	
	Field waypoint.waypoint

End Type


For n=1 To 10
	
	w.waypoint=New waypoint
	w\number=n
	w\x=Rnd(780)
	w\y=Rnd(580)

Next

player.player=New player
player\waypoint=First waypoint


Repeat
Cls

Text 400,20,"Press space to go to next waypoint",True
Text 400,40,"Current waypoint : "+player\waypoint\number,True

For w.waypoint=Each waypoint
	Oval w\x,w\y,18,18,False
	Text w\x+9,w\y+9,w\number,True,True
Next

If Sin(Float(MilliSecs())*10.0)>0

	Oval player\waypoint\x+6,player\waypoint\y+6,6,6

Else

	Oval player\waypoint\x-3,player\waypoint\y-3,24,24,False
		
EndIf

If KeyHit(57)

	player\waypoint=After player\waypoint
	If player\waypoint=Null
		player\waypoint=First waypoint
	EndIf

EndIf


Flip

Until KeyDown(1)
End	



PsychicParrot(Posted 2005) [#3]
Jeppe you are a star... your code is definately some of the most useful stuff I have on my hdd - THANK YOU!!!

:D Sooo glad it actually works, because TBH I didn't expect it to .. I was just waiting for someone to say 'dont be insane that'll never work because ...' ;)

Thanks again - brilliant stuff!


PsychicParrot(Posted 2005) [#4]
My head just popped.


PsychicParrot(Posted 2005) [#5]
Yeah ... next, the task is to try and get my head around doing multiple sets of waypoints!! ARGH!! :D :D


PsychicParrot(Posted 2005) [#6]
Ok. Got a plan ... I load the waypoints in to a field in the player type, then have a waypoint pointer (also in the player type) that points to those waypoints in the player waypoint field.

Then my head explodes and they donate my body to medical science ;)


Jeppe Nielsen(Posted 2005) [#7]
Glad you could use it, I have however modified it to include mutiply paths with any set of waypoints each:

Graphics 800,600,16,2

;create a new blue path
path1.path=PathNew(0,0,255)

;assign 4 waypoints to it
For n=1 To 4
	WaypointNew( path1,Rnd(780),Rnd(580) )
Next

;create a new yellow path
path2.path=PathNew(255,255,0)

;assign 7 waypoints to it
For n=1 To 7
	WaypointNew( path2,Rnd(780),Rnd(580) )
Next

Type player
	
	Field waypoint.waypoint

End Type

;create player 1 and let it follow path1
player1.player=New player
player1\waypoint=PathFirstWaypoint(path1)

;create player 1 and let it follow path2
player2.player=New player
player2\waypoint=PathFirstWaypoint(path2)

Repeat
Cls
Color 255,255,255
Text 400,20,"Press 1 to let player 1 go to next waypoint in path 1 (blue)",True
Text 400,40,"Press 2 to let player 2 go to next waypoint in path 2 (yellow)",True

;draw waypoints
For w.waypoint=Each waypoint
	Color w\path\r,w\path\g,w\path\b
	Oval w\x,w\y,18,18,False
	Text w\x+9,w\y+9,w\number,True,True
Next

Color 0,0,255
If Sin(Float(MilliSecs())*10.0)>0

	Oval player1\waypoint\x+6,player1\waypoint\y+6,6,6

Else

	Oval player1\waypoint\x-3,player1\waypoint\y-3,24,24,False
		
EndIf

Color 255,255,0
If Sin(Float(MilliSecs())*10.0)>0

	Oval player2\waypoint\x+6,player2\waypoint\y+6,6,6

Else

	Oval player2\waypoint\x-3,player2\waypoint\y-3,24,24,False
		
EndIf

If KeyHit(2)

	player1\waypoint=WaypointNext(player1\waypoint)

EndIf

If KeyHit(3)

	player2\waypoint=WaypointNext(player2\waypoint)

EndIf


Flip

Until KeyDown(1)
End	


Type path

	Field r,g,b
	Field f.waypoint ;first waypoint
	Field l.waypoint ;last waypoint
	
End Type

Function PathNew.path(r,g,b)

	p.path=New path
	p\r=r
	p\g=g
	p\b=b
	Return p

End Function

Function PathFirstWaypoint.waypoint(p.path)
	Return p\f
End Function

Type waypoint

	Field path.path
	Field number
	Field x#,y#
	Field p.waypoint ;previous waypoint
	Field n.waypoint ;next waypoint
	
End Type

Function WaypointNew.waypoint(p.path,x#,y#)

	w.waypoint=New waypoint
	
	w\path=p
	w\x=x
	w\y=y
	
	;check if path's first waypoint is empty
	If p\f=Null
		p\f=w
	EndIf
	;check if path's last waypoint is empty
	If p\l=Null
		p\l=w
	EndIf
	
	;assign path's last waypoint to the current waypoint's previous waypoint link
	w\p=p\l	
	
	w\p\n=w
	
	p\l=w
	
	w\number=w\p\number+1
	
	Return w
End Function

Function WaypointNext.waypoint(w.waypoint)

	If w\n=Null
		Return w\path\f
	EndIf
	
	Return w\n

End Function

Function WaypointPrev.waypoint(w.waypoint)

	If w\p=Null
		Return w\path\l
	EndIf
	
	Return w\p

End Function



Picklesworth(Posted 2005) [#8]
Oh no now it'll be more than his head!


PsychicParrot(Posted 2005) [#9]
BOOOOOM!!!! ;)

That's it then, you are the ruler of the universe and I am but a pleb.

EXCELLENT! You totally beat me to it!

Thanks x10billion! Now I just got to figure out how you did it so well ... !


DH(Posted 2005) [#10]
I try not to keep things so global personally. Just a different approach.

type SomeList
	Field ID
	Field otherfields
End Type

;=====Main Loop
	MyThing = createsomething()
	ModifySomething(MyThing)

;====End Main Loop


Function CreateSomething()
	local T.SomeList
	T.SomeList = new SomeList
	T\ID = handle(T.SomeList)
	return T\ID
End Function

Function ModifySomething(ID)
	local T.SomeList
	T=object.SomeList(ID)
	;Just in case i deleted it somewhere and my codes isn't clean, check to make
	;-sure it is still in the list
	if T <> null then
		;modify things here
	Endif
End Function


The handle of that list (the ID) is always unique, so there is no way to get the same one twice.


PsychicParrot(Posted 2005) [#11]
Thanks, Mr. Half :D

I should write an article - "how to get half way through doing something really cool that melts your brain so that you have to go away and lie down for a bit".!!!

Great help as always, guys. Much appreciated :)


Jeppe Nielsen(Posted 2005) [#12]
You're welcome :-)
You can add as many paths as you like with any number of waypoints each. Look in the WaypointNew() function to see where the waypoints gets connected, using their previous and next pointers. (p.waypoint and n.waypoint)


Jeppe Nielsen(Posted 2005) [#13]
And you will need these to delete paths:
Function PathDelete(p.path)

	w.waypoint=p\f
	While w<>Null
		ww.waypoint=w\n
		WaypointDelete w
		w=ww
	Wend

End Function

Function WaypointDelete(w.waypoint)

	Delete w

End Function