Variable number of parameters for a function?
Blitz3D Forums/Blitz3D Beginners Area/Variable number of parameters for a function?
| ||
Is it possible with blitz 3d to have a variable number of parameters for my function. I thought that I saw somewhere some instructions for it, but I can't find it now. |
| ||
Yes, you can make a parameter optional by specifying a default value for it. Eg: Function MyFunction(a,b=5) |
| ||
Kind of. Basically, in your function definition, you can give some variable default parameters, like this: Function PrintLines(Message$, howmany% = 1) ... Notice the " = 1" assignment in the function definition. This means that if you call the function without specifying a second parameter, it will use the value 1 as the second parameter. If you do specify it, it will be whatever you sent. Here's a working code example: PrintLines("Hello") PrintLines("Echo", 5) Function PrintLines(Message$, howmany% = 1) For i% = 1 To Abs(howmany) Print Message Next End Function So, as you can see, it's kind of a "variable" number of parameters, though it's not truly, since all parameters are specified one way or another, just not necessarily by you. Note that if you have a function with default parameters, every parameter after the first default parameter must also have a default parameter. That is to say, this will not work: Function PrintLines(Message$="", howmany%) ; <--- WON'T WORK ... Dig it? |
| ||
I was thinking more along the lines of: Function LoadSomeBitmaps(a, ...) where ... is any number of bitmaps. Not so much making a parameter optional, but making it possible to have an unlimited number. |
| ||
No, I don't think that's possible. |
| ||
Yeah, you can do it in other languages, but not Blitz. Of course there are all kinds of ways to pass in a variable amount of items into a function, they just can't be separate parameters. You could, say, put them into an array, and just keep loading bitmaps until the array value is 0 or something. But that might be just as hard as what you're probably doing now (calling the function multiple times, probably). |
| ||
Thanks folks. Guess I'll find another solution. |
| ||
well no you can't do it that way, but you could do: type bitmaplist field a.bitmap end type type bitmap field bitmapfile$ end type dim bitmaplist(1) b (0).bitmaplist=new bitmaplist b (0)\ a = new bitmap b (0)\a\bitmapfile$="World" b (0)\ a = new bitmap b (0)\a\bitmapfile$="grass" Loadbitmaps b Function Loadbitmaps(b.bitmaplist) For each b(0)\a\ bitmap filex=b(0)\a\bitmapfile$ file=b(0)\a\bitmapfile$+".dat" filex=Loadimage(file) next end function note- not sure if i got everythign right here, but i don't have time to test it but it should work, just figure out how to run types within types, and you can use 1 identifier. I dimmed the array with the (0) just in case, but its not neccesary. Good luck, maybe someone knows how to do this better then i do. |
| ||
thats prolly the best bet. but if you want to do something super dynamic, like using variants, you could always use a string or an array, and parse/use each array item function DOWORK(myarray []) where each item in myarray is a value of a different type to add a twist you could make the first item in the array how the function should handle it function dowork(myarray[]) select myarray(1) case "DELETE PLAYER" delete myarray(2) case "loadplayer" playertype = new playertype playertype\name = myarray(2) playertype\height = myarray(3) playertype\weight = myarray(4) end function |