For Type Pros

BlitzPlus Forums/BlitzPlus Programming/For Type Pros

robot(Posted 2006) [#1]
Okay, the thing is I have a few questions about types:
- Say you have a group of enemy types, is there a way to acsess a certain specified enemy in the list
-I have looked through the code in one of my font libraires .bb libraries, and they have used a function that I cannot find in any of the B+ docs.Object. What does this funciton do?

And if theres any other thing i might need to know about types, please tell me, thanks! :)


Andres(Posted 2006) [#2]
you'll need to add an ID number to every enemy and then just

for this.enemy = each enemy
  if this\id% = enemyid% then
    ...
  endif
next



Grey Alien(Posted 2006) [#3]
I prefer to create an array of Types, then every time I make a new type, add it into the array. Then I can reference the type by accessing an array element instead od having to use a for loop :-) Of course you need to manage adding/deleting from your array carefully...


robot(Posted 2006) [#4]
okay, thats a good idea Grey, but wouldn't you have to make a set number for the array? then could you give me and example of the new function and deleteing. thanks! ;3)


Grey Alien(Posted 2006) [#5]
yes, as Blitz PLus can't resize arrays, you WOULD have to make a fixed size array, but if you know a theoretical maximum number if types it's no problem.

When you add to the array, you ought to keep a counter which is the current next available empty slot, when filled, increment the counter by one. Unless you need to insert into the array and shuffle all the other slots up by one. Deleting something from the middle of the array will need shuffling and also a reduction of the counter by one. It's a bit late for me to be posting code right now, but I used this technique for my games and it's cool.


aab(Posted 2006) [#6]
well, at any level, resizing of an array commonly involves complete reallocation as theres little garantee that anything after the array is free to be appended. So having a second array dimmed to one, and then making a function to dim b to the size of a, dim a to the new size and copy the values back wouldnt be any bother..except it'd have to be done for every desired array..and be slow..So woulod only really be usefull when say having a pause already due to loading an area or something.

Generally, i suppose the eg.enemy you would want would be discovered during a loop for each enemy eg:
for this.ene blabla
	If closeToPlayer(...)
		If playerAttacking(...) 
			;here, this is obviously the type yod want.
		EndIf
	EndIf
next

But if you wanted at this moment to pick some out from the list, or on creation set some so that you could add soemthing else onto what happens to them/treat thm differently, rather than having variables, or storing them with arrays and storing off which indices you wanted to access, you would sometimes be better off having a seperate list of types, which each have one of those types a s a field, and allow you to iterate through a constriction on those types rather than through all of the types as blitz forces.
eg:

However with new hurt_enemy_types being made and freed alot, that can be both messy and mess about with memory. But for some situations it can be usefull to be able to only check a particular condition once, and process the results on a constricted group of the types later (withought having to check any values on that group).
If you only wanted one type out of the loo after detection...Just having another type :
local closest.my_type
For ...
   if something
       if IsCloserThanOthersSoFar(...)
           closest=this;
       endif
   endif
Next



Handle() allows you to store a value relating to a types address into an integer. Object() allows you to get it back again, eg:
Type me_type
field name$
end type
local me.me_type=new me_type
me\name$="aab"
local handleToMe% = Handle( me )
. . .
local someone.me_type = Object.me_type ( handleToMe )
print someone\name$ ;prints "aab"
; i *think* lol

I think its almost useless. Types are already an address-type of thing(referenced through functions..can be null), and much safer.
Even if two types are exactly the same, blitz internal storage methods (which allows the for each thing) means they cannot be intechanged through handle() and object()
You can make a library though, and only return handles so that your users dont have to worry about the types name when passuing it within their code etc: In other words it would be like saying "we dont want this types values to be changed other than through our own functions"...Not that it forces such a thing, but it makes it obvious that thats the intent.


CS_TBL(Posted 2006) [#7]
You can store type instances in banks, banks can be resized without loosing the content.


aab(Posted 2006) [#8]
aaaaahhhh yes thats what handles usefull for.
I forgot that somehow...thanks.