Best way to "move my character"

Blitz3D Forums/Blitz3D Beginners Area/Best way to "move my character"

Types ARE linked lists. Just use First, Before, After, and Last.


Subtly incorrect. Blitz automatically creates a single, partially-accessible linked list for each defined type. If you implement your own linked lists, you can have the benefits of customizable linked lists, such as multiple lists per type and objects belonging to multiple lists.

thisNode = First Node


See? Only one list per type. If you want your animated objects to follow more than one path, you'll need a different solution.

The conventional approach to "custom" linked lists is to add next (and usually prev) pointer fields to your type so that you can chain objects into lists, setting the pointers at ends of lists to NULL. You need only store a pointer to the first object to be able to access all the elements in its associated list.

Type node
	Field x#, y#, z#
	Field nextNode.node
End Type

Function new_node.node(prevNode.node, x#, y#, z#)
	n.node = New node
	n\x = x : n\y = y : n\z = z
	If prevNode <> Null Then prevNode\nextNode = n ; update the node before us
	Return n
End Function

a1.node = new_node(Null, 1, 1, 1) ; new list
a2.node = new_node(a1,   2, 2, 2) ; chain a1 to this node
a3.node = new_node(a2,   2, 3, 3) ; ...
b1.node = new_node(Null, 3, 2, 3) ; new list
b2.node = new_node(b1,   2, 1, 2) ; chain b1 to this node
b3.node = new_node(b2,   0, 0, 1) ; ...

a.node = a1
b.node = b1

display_node_list(b)

Function display_node_list(firstNode.node)
	currentNode.node = firstNode
	While currentNode <> Null
		Print currentNode\x + ", " + currentNode\y + ", " + currentNode\z
		currentNode = currentNode\nextNode ; advance to the next node
	Wend
End Function


You'll need a function to remove a node without breaking the list. You may end up adding functions to rearrange elements, treat your list as a queue or a stack, and other fun things.

Although this is a fantastic solution, some of us are prone to over-engingeering. The next step is to separate the linked list logic from your type so you can reuse it with other types. Since there's no polymorphism in Blitz Basic, you'll need to copy your linked list functions anytime you need lists of a new type. I got tired of this and devised a different solution.