types - passing as parameter in function
Blitz3D Forums/Blitz3D Beginners Area/types - passing as parameter in function
| ||
I'm having trouble working out how to pass a type instance as a paramter to a function. Prior to doing this, this is what I achieve right now ; I call the function like this start_gearchange_timer() ; this is what the function looks like Function start_gearchange_timer() player(1)\gearchange_timer = Millisecs() End Function ...now I have lots of timers, so I don't want to have lots of functins that essentially do the same thing so I wanted a simple function that could be passed paramters to do the same job eg. ;call the NEW function like this start_timer( paramters eg. player(1)\gearchange_timer ) ; the NEW function would look like this Function start_timer ( paramters ) paramaters = Millisecs() End Function Can anyone help ? Hope I explained that ok. Basically I want to know have to pass a custom type variable and its field as a paramter in a function! |
| ||
You seem to be using an array of types so can't you just pass the index, like so?start_gearchange_timer(1) Function start_gearchange_timer(i%) player(i)\gearchange_timer = Millisecs() End Function |
| ||
Hi big10p no, I want the new function to be an all purpose function that can be passed any timer and start it running. I have lots of timers and don't want to have to right lots of functions to start them I just want to be able to pass the name of the timer to a single fuction to do the job eg. player(1)\gearchange_timer eg. player(4)\brake_timer eg. player(2)\spin_timer ;call the function start_timer ( player(1)\gearchange_timer ) ; the single all-purpose function to handle any timer function start_timer( paramaters ) parameters = millisecs() end function sorry if I'm not making myself clear, doh! |
| ||
actually looking at it, its probably easier not to even call a function at all - just simply put a line of code in like player(1)\gearchange_timer = millisecs() its late, and my brain hurts! so much for my attempt at elegant code, doh! |
| ||
*chuckle* I was going to suggest that myself but I assumed you wanted to have other code in the function too, not just set the timer. :) |
| ||
if you want to pass the type then do function start_timer( p.yourtype ) ; p is acting as a pointer and is declared as yourtype p\gearchange_timer = Millisecs() end function call it with start_timer( player(1) ) |
| ||
dmaz out of interest is the any way to include the type's field in the function's parameter ? |
| ||
Yes. Its late but I'll try this. It goes something like this. Best to put it in a dll. in the dll.... Type timer field ID$ field starttimer ect... end type ;call the function in your game loop start_timer( "paramters eg. player(1)\timer",starttimer,ect ) function start_timer.timer(ID$,starttimer,ect..) ticktock.timer = new timer ticktock\ID = ID ticktock\starttimer = timer ticktock\ect... = ect... return ticktock end function ;return the timer instance given its ID function timer_getID.timer(ID$) For this.timer = each timer if this\ID = ID then return this next return null end function ;one more function for your timer Function time() for this.timer = each timer ticktock\timer = Millisecs() next End Function Hope it helps. If it did not then goto the source. Look in blitz coder and in the article section find the article Reusable Code: portable moduals in blitz by Morduun. |
| ||
thanks |