Sending a type to a function

Blitz3D Forums/Blitz3D Beginners Area/Sending a type to a function

coach(Posted 2005) [#1]
I want to send a type to a function and change some values and then send the type back to be caught by the same type

Ex.

type1 = function(type1)

I am getting an "Illegal Type Conversion" error. Can you not send types to functions.


DJWoodgate(Posted 2005) [#2]
Yes, define the function as

Function myfun.mytype(instance.mytype)

for instance.


WolRon(Posted 2005) [#3]
And don't forget to

Return instance.mytype

after you've modified it.


NewtSoup(Posted 2005) [#4]
umm if you have

a.mytype= new mytype

funtion do_some_work(atype.mytype)
...
...
...
end function

do_some_work(a)

then your reference to "a" after the point where you modified the values will have the modified data in it because blitz passes everything by reference

try this

Type number
field value
end type

function add_one( num.number)
     num\value=num\value+1
end function

a.number=new number
a\value=10

add_one(a)

print a\value



this is something you have to be very careful of when designing your program. Do you really want to modify the data inside the type or do you want to do work using the values (multiplying etc) but leave the original values alone.

Usualy you need only return a type if you are finding/seletcing one from a collection.

If you want a new type object based on the values of the old one you need to do something like this:

Type number
field value
end type

function add_one( num.number)
     r.number=new number
     r\value=num\value+1
     return r
end function

a.number=new number
a\value=10

b.number=add_one(a)

print a\value
print b\value



You could be forgiven for thinking that you created 3 instances of the type here but you didnt.. new was only called twice, once to create "a" and once to creat "r" the return value in the function. b.number only declares b to be a variable of type "number"

However once again there is a possible bug bear. Due to odd things that can happen with variable "scope" it's considered bad form (In "C" at least, not sure about Blitz precisely) to return a type created inside a function as variables created inside funcitons are local and it's conceivable that it might go out of scope and end up NULL

so again if you need to return a new instance of a type based on an original one without modifying the original a better function might be

Type number
    field value
end type

Function add_one (source.number, newvalue.number)
    tmp=source\value
    tmp=tmp+1
    newvalue\value=tmp
end function

a.number=new number
a\value=10
b.number= new number

add_one (a,b)

print a\value
print b\value


Some people will say that my last example is code intensive and for this tiny simple example they'd be dead right. However seperating the data from the types into temp variables then working on the temp variables and then copying the data back last thing before the function exits is again good form. Far better to use 10 extra lines now and get your function working than use a short cut which "should work fine" and then spend the next 2 days going through 30 or 40 thousand lines of code trying to work out why the AI jumps a mob 40 feet in the air under certain conditions. Besides a good compiler will strip out uneccesary code during block optimisation. (Isn't that right Mark ;) ) having said that do feel free to streamline after once your program is doing what you intended it to do rather than what you told it to do (subtle difference, one that is never lost on me each time I start debuggin)

Ok Ive drifted off topic a bit here but its all stuff worth remembering :) because without careful plannign things can go badly wrong when you start flinging types around between functions.