Illegal type conversion error...
Blitz3D Forums/Blitz3D Beginners Area/Illegal type conversion error...
| ||
I get this error when I'm trying to return an int from a function that uses a type, like so:c = FindClosest.point(pointID) Function FindClosest.point(id) For p.point = Each point If p\id = id min = 1000000 close = 0 For p2.point = Each point If p2\taken = 0 If p<>p2 dx = p\x-p2\x dy = p\y-p2\y d# = Sqr#(dx*dx+dy*dy) If d < min min = d close = p2\id EndIf EndIf EndIf Next Return close EndIf Next Return 0 End Function Any help? |
| ||
In the end you are simply returning an Integer, not a Type instance. There is no need to add the ".point" suffix at the function's name; that's what is causing the problem. |
| ||
But I need to add the .point to be able to use a non-global type! |
| ||
No you don't. Remove the .Point from the function declaration AND the function call. That compiles and runs fine, because I just tried it. If it doesn't for you, then you obviously have more code you're not showing, in which case we'll need to see that too. Adding .point after the function name serves no purpose other than declaring your intention to return a .point, whicih is not your intention. |
| ||
....it works....I wonder what was causing the problem so long ago... |
| ||
You're welcome. |
| ||
Thanks. So, what would the .point be needed for? |
| ||
The suffix in a function's name must always be the kind that that function will return. If you don't put any suffix, then the default kind is utilized which is Integer. For all the other kinds of possible Return values, you must type in the precise symbol\type in the suffix. Let's say I want the function to return a String: Function ReturnName$() ;I put the "$" because that symbol indicates that the function returns a string Return "Mark Sibly" End Function Similar thing for floats, where you will be putting a "#". For returning Type instances, you need to put a ".Type" in the suffix. The appropriate type you put in the suffix is the structure that you will be returning instances of (I hope you can understand that). In practice: Function FindEnemyObject.Enemy(ID) ;I'll be returning an instance of the Enemy type. For E.Enemy = Each Enemy If E\ID = ID Then Return E ;Returns the handle to an instance. Next End Function To use this function, you could do something like: Enemy5.Enemy = FindEnemyObject( 5 ) Get it? Since in your code you are returning an integer, you can either use FindClosest() or FindClosest%() (the "%" is the symbol for Integers). |
| ||
Aaaahhhaaaaa.... thanks for clearing it up for me. |