123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|422|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Blitz -> question about functions and types...

Sun, 23 Mar 2008, 17:53
Orion Pax
Ok, I know how to use types and functions in blitz3d. But until recently I have never seen a function declared like a type. Here is what I am talking about...



Initplayer.Playerdata? A function declared like that. what would be the reason and benefit of doing this?
Sun, 23 Mar 2008, 18:54
Jayenkai
OK, imagine you make a type called Person.


If you want to print out that data, you can either do it inside your loop..

or
you could..



That way, not only is that function reusable elsewhere, for different things, but the actual for loop looks a little neater. And there's also the added bonus of being able to use Blitz's right hand bar to jump right to that function when need be.

Now, that's sending people INTO the function, but if you did something like this..


All nice and compact.
It's all about making things easier to "group" fix a bug, or make your actual code a little bit smaller..

It also helps if you suddenly decide to change the data in your type.. you can easily tweak a function to reformat things, whereas if your "New Person" stuff is all over your code, it'll mean plenty of changes.

It's good coding practice, and something you should probably get used to doing.

----

For the record, I rarely remember to do this, and it bites me in the ass later on in a project.. But then I never stick with a project that long, anyway!


-=-=-
''Load, Next List!''
Sun, 23 Mar 2008, 19:03
Orion Pax
That sounds like a great idea. Just one question. I notice that in that last function you start off with ANew as the pointer in the type. And when calling the function you use THIS as the pointer. Does it matter? Or is the ANew local to the function only and it will use what ever pointer you decide to use?
Sun, 23 Mar 2008, 19:11
Jayenkai
You can switch between using one name and another, although in all honesty it's probably best to use local names inside functions incase it all gets messy!
To be honest, I've never checked..
(*goes to check)


Blitz doesn't seem to mind if you make up names inside functions..
But, it's best to play things safe, and specify them as being local, anyway, just incase


-=-=-
''Load, Next List!''
Sun, 23 Mar 2008, 19:13
Orion Pax
Thanks! You been a big help.
Mon, 24 Mar 2008, 01:43
JL235
'this' and 'anew' in Jay's example are two completely separate variables. One is a local variable inside the function, the other is a local variable to the program.

However they both hold the same value: the reference or pointer to the Person Type that was made in the function and then passed back. They are just two different variables holding the same value. So no, having different names for them does not matter.