methods or functions?

BlitzMax Forums/BlitzMax Programming/methods or functions?

Najdorf(Posted 2005) [#1]
As of now I structured my games this way:

I made a "game" object where I hold the most important variables and objects, then the main functions such as the game loop and most other routines as "methods" of the game object.

For laziness I put as methods also functions that affect very few of the game objects: should I better rewrite them as functions (passing in the appropriate stuff?)

From my understanding functons are faster than methods because they dont "pass in" all the fields of the object, only the necessary ones: is this correct?

[another question: say I want to pass variable in a function (say an integer) as a pointer/reference (so then I can modify it in the function), how would I do? I know that objects are always passed as a reference, what about variables?


deps(Posted 2005) [#2]
variables are not passed as reference, put Var after to make them references:
local a = 42
local b = 10
print a
print b
test(a,b)
print a
print b

function test( a, b Var )
    a = 1024
    b = 768
endfunction



FlameDuck(Posted 2005) [#3]
From my understanding functons are faster than methods because they dont "pass in" all the fields of the object, only the necessary ones: is this correct?
No. The only thing passed to a method, in addition to the parameters, is a reference to the object in question. The method then uses this reference to look things up as nessecary.

In either case, any real or imagined performance hit by using one over the other isn't going to make or break your game. PCs are plenty fast now, why make prograaming harder than nessecary?


Najdorf(Posted 2005) [#4]
Ah, ok. So ultimately using the "game" object+methods approach is ok?


FlameDuck(Posted 2005) [#5]
Yeah. I would code it so it works first, then optimize later, if / when nessecary.