types and functions

Blitz3D Forums/Blitz3D Beginners Area/types and functions

Dax Trajero(Posted 2004) [#1]
is there any way to pass any object\variable to a function ?
eg.

;-define type---------------

type car
field accelerate_timer
field decelerate_timer
end type

global blue.car = new car

;-main-program--------------

set_timer(blue\accelerate_timer)
set_timer(blue\decelerate_timer)
end

;-function------------------

function set_timer (object\variable)
object\variable = milliecs()
end function

;---------------------------


Ross C(Posted 2004) [#2]
you need to pass across

set_timer(blue.car)

I'm sure.


soja(Posted 2004) [#3]
Well, you're just passing in an integer (accelerate_timer or decelerate_timer), so in order to pass that in you would have to define the function like this:
function set_timer (thetimer%)
(The % is optional.)

But I see that you want to change the field value of the type, so this is not the way to do it. (The value would be forgotten once you returned from the function.) What you should do is this:

Function set_timer(c.car, decelerate)
    If decelerate then 
        c\decelerate_timer = Millisecs()
    Else
        c\accelerate_timer = Millisecs()
    Endif
End Function

...and to call it:
set_timer(blue, 0) ; Accelerate
set_timer(blue, 1) ; Decelerate


Something like that. There are numerous ways to end up doing what you want. This is the one closest (probably) to what you were thinking.


Dax Trajero(Posted 2004) [#4]
thanks all

Incidentally, I'm reaching a stage in my program where its probably best I make a decision NOW as to how I'm going to reference my type object. eg. in my main loops is it best to do this method...


For loop.car = Each car (I have 4 different types of car)
loop.car\field = whatever
Next



or, is it better to put them into an array like this:



car(1)\field = whatever
car(2)\field = whatever



I'm sure everyone of you have been in this position before, so your experience would be invaluable


soja(Posted 2004) [#5]
If you're going to be doing the same thing to each one, by all means, do it the first way.

Generally you would put custom type pointers into an array if you wanted them indexed, so that you could reference a single, particular one in O(1) time. But if you're doing something to all of them, then both ways are O(n).

And in fact, 4 cars is virtually nothing. Even if you wanted to reference a single car, to loop through 4 to find that car is practically the same.

So in this case, I would say to make it easy on yourself, the programmer.


Dax Trajero(Posted 2004) [#6]
I started with for next

but now, I'm tempted to use arrays as I've reached the sprite to sprite collision routine I have to write, so I find myself with a problem

using the for next loop method, I can't compare blue.car\xpos to red.car\xpos (I just have loop.car\xpos)

if I switched to an array I could easily do this:

if car(1)\xpos = car(2)\xpos

so I think I'll switch


soja(Posted 2004) [#7]
or,

if blue\xpos = red\xpos


Dax Trajero(Posted 2004) [#8]
bear in mind I need to sort the sprites draw order based on their y position on the screen (a sort of faux 3D)

so being able to sort an array would be helpful

I can't see any downsides to using an array in fact ?


soja(Posted 2004) [#9]
No, it's just another level of abstraction. There's nothing wrong with it. You could do the same thing with the type instances (since they're in a linked list, and swapping order would be quite fast) using Before, After, Insert, etc.

Whatever is easiest for you as the programmer.