some pointers

BlitzMax Forums/BlitzMax Beginners Area/some pointers

cloned(Posted 2006) [#1]
can you give me some pointers to a newbie to BMAX, i just want some pointers on what things avoid in BMAX and what i should do to keep memory consumption down


Regular K(Posted 2006) [#2]
http://blitzbasic.com/Community/posts.php?topic=42519


SculptureOfSoul(Posted 2006) [#3]
Well, I'm a newbie as well - to programming (for the most part) and to BMAX.

Here's a lone pointer: Objects (any declared Type) are passed by reference to a function, while the standard variable types (int, float, etc) are not.

So if you want to directly modify parameters of a function that are of the standard types, you must include "var" after them so that they are treated as a reference.

E.G.



cloned(Posted 2006) [#4]
well i already knew that from programming in a different language


tonyg(Posted 2006) [#5]
In that case tell us EVERYTHING else you know and we'll see what's missing.


H&K(Posted 2006) [#6]
So if you want to directly modify parameters of a function that are of the standard types, you must include "var" after them so that they are treated as a reference.
You could of course just pass the whole object, and then not worry either way. (Because the object is by refference it is still just a pointer being passed)

In fact (Broken record time), where ever possible the function/method should be called via the object, and not simply passed a param of the object


Brendane(Posted 2006) [#7]
"the function/method should be called via the object, and not simply passed a param of the object"

I have no idea what this means.


tonyg(Posted 2006) [#8]
I *think*...
Type TSum
	Field a:Int
	Function create:TSum(num:Int)
		mynew:TSum = New TSum
		mynew.a = num
		Return mynew
	End Function
 	Function double_a_func(myobject:TSUM)
		Return myobject.a * 2
	End Function
	Method double_a_method()
		Return a * 2
	End Method
End Type
mynew:TSum = TSum.create(5)
Print TSum.double_a_func(mynew)
Print mynew.double_a_method()   ' preferred.

Is that right?


Brendane(Posted 2006) [#9]
I still have no idea what H&K was getting at in response to the post above - what SculptureOfSoul was saying is perfectly correct and has nothing to do with passing an object's own data as parameters into it's own methods.


ziggy(Posted 2006) [#10]
@Brendane: well, everytime you pass a parameter without var, a new variable is alocated and after the function, destroyed. So passing pointers (types or variables by reference) is quicker and reduces memory usage and data conversions. I think that is what H&K was saing.


Brendane(Posted 2006) [#11]
Passing base types (int, float, byte etc.) into a function pushes their values onto the stack to be used by the function. There is nothing gained by using Var unless you want your function to modify the actual variable passed in.

'Objects' (user Types) are *always* passed by reference (that is to say, a pointer to the object is pushed onto the stack, no copy is made). Nothing new is created and the use of Var here is actually redundant.

Strings are handled differently it seems. If you pass a string into a function it always passes by reference, *HOWEVER*, unless you specify Var you cannot modify the string.


H&K(Posted 2006) [#12]
What I ment was exactly what tonyg posted, so I didnt think to post a explination.

If I have a chose between

Function doit(Object.Field)
or
Object.doit

Then I would alway pick the second one.

Ie I wouldnt pass a Field, I would call the function from the object ;)

As a more sencible statement I was saying any function that is going to use data from "Only One Type of Object", should always be a function/Method of that Object.

(Sorry for not being clear, but TonyG didprint exactly what I ment, so I couldnt have been that unclear)
(Mind, as I always say "Stick it in an object", know matter what the question, TonyG might have just guessed what I ment)

I would recomend that everyone downloads the minib3d by simon. http://www.blitzbasic.com/Community/posts.php?topic=61515
Even If you do not intend helping him, the way it shows the object layout against the Function layout is instructive. (And because it trying to do something we know, the code is easy to follow)


Defoc8(Posted 2006) [#13]
var1=var2=var3 - assigns the result of var2=var3 to var
..that is - whether or not theyre equal..
as opposed to assigning vars 1 and 2 the
same value as var3...this was pointed out
to me..after i made a post about assembly
output...my brain is still stuck in c/c++ mode.



var:+1 equates to var=var+1
var:/2 equates to var=var/2 etc etc

print "hello"+..
"world"

".." this operator allows you split text lines up..the above
print statement spans two lines..perhaps i shouldnt call
it an operator, too late now :p ;)

type namespace
global var,var,var,var
function
function
function
endtype

- global functions/vars/consts are perhaps best stored within a type.
this allows you to treat them as if within a c++ namespace, - lessening the chances of clashes between
modules...

special methods new() and delete() can be added to your
type - allowing your objects to perform initialisation, and
cleanup ops - contructor/destructor.

there are loads of other things that might be useful to
know..those are the only ones that spring to mind at the
moment..


SculptureOfSoul(Posted 2006) [#14]
Is Delete() currently called when an object gets destroyed?

And also, I remember reading that Delete was currently a reserved keyword. Is it wise to utilize delete() (assuming it is called) as a destructor...I'm worried that it's functionality will change in a future Bmax release.


SculptureOfSoul(Posted 2006) [#15]
Probably a silly question but, since I'm away from a comp w/ Bmax at the moment and I'll probably forget to test this if I don't ask now...
Do exceptions work like they do in C++ where they continue to propagate upward until they are either caught or terminate the program...or do the thrown exceptions have a local scope?


cloned(Posted 2006) [#16]
i thought delete() didn't exist BMAX like it did in BB

i made a sword fighting animation for a guy but i have no images to test it with(i worry a lot if my math is right or wrong)

also what functions would be best for calling images, i already perfer .PNG as my image format. i really need some info on the pixmap modules


SculptureOfSoul(Posted 2006) [#17]
LoadAnimImage is ideal for loading animated images, where LoadImage() is for single frame images. These both work on .png's.

Pixmaps are stored in system memory, and need to be transferred over to video memory before being drawn, and are therefore considerably slower than images (TImage objects).

The only time it is recommended to use a pixmap over an image is if you need to modify the image directly in some manner or another (i.e. plotting pixels over it, combining two images, doing some kind of custom pixel replacement, etc. )


Dreamora(Posted 2006) [#18]
Delete() is a default method on objects (destructor) thats called when an object is collected after it went out of scope.
If you implement it, its called instead of the core object ones (:Object) and you can define the stuff that should happen when the specific object is cleaned.


tonyg(Posted 2006) [#19]
To add to Dreamora's comment...
Don't mix up the 'delete' command/keyword with the delete() method.


SculptureOfSoul(Posted 2006) [#20]
Thanks for clarifying that tony!

Another question - is there any command that is similar to "inline" in C/C++?


Brendane(Posted 2006) [#21]
@SculptureOfSoul : Yes, exception work just like in C++, they propagate until a handler catches it.


Defoc8(Posted 2006) [#22]
..as far as i know, no inline or macro support has been
added to bmax - this may well be an effort to avoid hard
to read/confusing code..well with macros anyway..
some form of code expansion would be nice though..


skidracer(Posted 2006) [#23]
@Brendane: well, everytime you pass a parameter without var, a new variable is alocated and after the function, destroyed. So passing pointers (types or variables by reference) is quicker and reduces memory usage and data conversions.


Using var will in many situations keep the variable alive longer and force it to be stored in memory both effects possibly resulting in slower performance.