Newbie 'type' questions

Blitz3D Forums/Blitz3D Beginners Area/Newbie 'type' questions

Farflame(Posted 2003) [#1]
Ok, just started using types. I've come from the dark world of arrays, so types are confusing me a bit :)

Is there a command to tell me how many items are alive in my type? E.g If I've done a.alien = new alien and b.alien = new alien, is there a command to tell me I now have 2 aliens?

When I delete an item from my type, and then create a new item, would the new item be the 'first' in the list? E.G If I did a.alien = new alien, delete a, b.alien = new alien, would b.alien now be the first in the list? I would assume it is, but it doesn't seem to be in my program. If I create b.alien, it seems to still be the second in the list, even though a is now dead.... ?


Neo Genesis10(Posted 2003) [#2]
There is no counter for types. You can create your own counter if you wish, and in some cases it is recommended. As for deleting, you are perfectly correct. If you delete one object the following objects slide up the list. So 2 will be 1, 3 will be 2 etc.
---
1 - Arnie
2 - Slylvester
3 - Van Damm	> Deleted <
4 - Will Smith
5 - Jackie Chan
---
Becomes
---
1 - Arnie
2 - Sylvester
3 - Will Smith
4 - Jackie Chan
---
To understand why yours isnt behaving you'd have to check your code.


soja(Posted 2003) [#3]
Whenever you use the New command to create an instance of one of your custom types, it is added to the bottom of the linked list for that custom type. Blitz keeps ALL objects of a particular type in a single linked list.

So, if I did this:
Type Actor
    Field Name$
End Type

a.Actor = New Actor
a\Name = "Arnie"

...then the internal linked list of Actor-type objects would look like this:
a, First, Last -> (Arnie)

(Note that a, First, and Last are all referencing the Arnie type object.)

If I then did this:
For i = 1 To 5
    a.Actor = New Actor
    a\Name = "Actor " + Str i
Next

...then I would have something like this:
a, First -> (Arnie)
(Actor 1)
(Actor 2)
(Actor 3)
(Actor 4)
b, Last -> (Actor 5)

I could do
b = Before b
a = After a
Delete After a
Delete b

...and I would have:
First -> (Arnie)
a -> (Actor 1)
(Actor 3)
Last -> (Actor 5)



b -> Null ; The object that 'b' was pointing to (Actor 4) was deleted...
          ; and the pointer 'b' was not reassigned to point to another type object
          ; now the linked list contains only 4 Actor objects, instead of 6


The key is to understand the difference between custom type pointers (variables) and the actual objects themselves (in the linked list).


soja(Posted 2003) [#4]
Ah, and to discover how many instances of a certain type you have (without keeping track manually), you would do this:
For a.Actor = Each Actor : count = count + 1 : Next



Farflame(Posted 2003) [#5]
Thanks. I'm sure I'll get my head round it. Explanations are always helpful :) Looks fairly easy, but my head is refusing to accept it right now. A bit of blundering around in Blitz should help :)


soja(Posted 2003) [#6]
You can also go through the Beginner's Forum and check out all the threads that have "Type" in the title.

Or ask some more questions.

Or blunder around in Blitz. =)

Rembember:
1) The type declaration ("Type... Field... End Type") is like a blueprint for your custom type.
2) When you use the New command, a new object is "built" from the blueprint, and kept track of by Blitz in a linked list.
3) When you create a new variable for a type, it's only a pointer. "variable.MyType" initially points to NULL, until you assign it to one of the objects in the linked list. You can reassign them at will. Not every object created has to have a variable assigned to it, but all can. In fact, any custom type object can have many variables pointing to it.

Clear as mud? =)


Farflame(Posted 2003) [#7]
Oh, I worked out why my type wasn't being deleted. I'm in Windowed mode for debugging, and I was using the 'exit' button to exit the window, rather than hitting escape and my program is set up to look for the escape button to delete the type. Now I just need to find out how to recognise the exit button :)