Functions as Types?

Blitz3D Forums/Blitz3D Beginners Area/Functions as Types?

MAX711(Posted 2004) [#1]
Why doesn't this work? ('troop' and 'unit' are previously defined Types), it's the '\checked' bit it doesn't seem to like.
;
;
FindTroopLeader(troop)\checked = True
;
;
Function FindTroopLeader.unit(troop.troop)
For this.unit = Each Unit
If this\troop = troop
If this\leader = True
Return this.unit
EndIf
EndIf
Next
End Function

If I use an array it works fine, such as...

claimedNode(x,y)\checked = true

where claimedNode is defined as..

Dim claimedNode.unit(48,48)


semar(Posted 2004) [#2]
2 things.

1)
I would change the function call with:
temp = FindTroopLeader(troop)
temp\checked = true
where temp is declared as temp.troop

2)
I would distinguish between type structure name and type pointer name; in your example, troop is the type structure name, and also the type pointer name, which I find a bit confusing.
So, make your type structure always with t_ suffix, for example:
type t_troop
field...
end type
global troop.t_troop

Hope this helps,
Sergio.


MAX711(Posted 2004) [#3]
Thanks for the reply Sergio. I tried your suggestion 1) and it works great. I was hoping there was a way to avoid creating a 'temp' Type just to check the result of a Function, just seems like redundant code to me, especially when it seems to work fine with Arrays. Thanks again for the help. Also, do you always define your Types as Global? I pass all my Types between Functions and have never needed to make them Global, is there another reason?


semar(Posted 2004) [#4]
No, I declare them global only for code consistence, but it is not needed, a type pointer becomes global the moment you declare it.
So this:

troop.t_troop


has the same effect of:

global troop.t_troop



More, since you return a troop element (this), you can just refer to that, since the for..next loop has exited, that means that 'this' points already to the right element. In other words, you may do like:

FindTroopLeader(troop)
this\checked = true ;'this' points already to the element you want to edit

That's why I think, also, that you can set the 'checked' field of 'this' element, directly in the FindTroopLeader function:

Function FindTroopLeader.unit(troop.troop)
For this.unit = Each Unit
If this\troop = troop
If this\leader = True
;Return this.unit 
this\checked = true
return

EndIf
EndIf
Next
End Function



That means also that the function declaration does not need to return any 'unit' type, so you can declare it as:

Function FindTroopLeader(troop.troop)

Hopefully I didn't confuse you !

Sergio.


MAX711(Posted 2004) [#5]
A little bit confused, but more concerned that all my Type pointers used in Functions survive outside the function, if I understand what you are saying? I also don't want to set the \leader field inside the FindTroopLeader function because I call that function from lots of different places in the code for other reasons. Thanks for the advice and info.


big10p(Posted 2004) [#6]

troop.t_troop

has the same effect of:

global troop.t_troop



I don't think so. :)
If you want a type pointer (or any other type of variable) to be accessible inside functions, you have to specifically declare them as Global, in the main code.

MAX711, you're OK - local function variables are destroyed on exiting the function and so are not accessible outside of functions.