Posted: 14th Jun 2007 11:58
I was reading a thread and a few people said it's good to make functions more command like.

An example:
+ Code Snippet
#constant FreeObject free_object()
newobject = FreeObject


It kind of looked nice, so I change a few more and stumbled on this:
+ Code Snippet
MakeCube 1,100,25,50,10

#constant MakeCube make_cube()
function make_cube(id , size , x , y , z)
   make object cube id , size
   position object id , x , y , z
endfunction


I never knew you could do this with functions, could this be useful in any way or has anyone else used functions like this?
Posted: 14th Jun 2007 13:32
Nice find, I never knew that
Posted: 14th Jun 2007 13:33
Does that actually work? It looks to me like the comiler would read that as:

+ Code Snippet
make_cube() 1,100,25,50,10


and not

+ Code Snippet
make_cube(1,100,25,50,10) 


If it works, then that is pretty cool - just don't see how it can, though.
Posted: 14th Jun 2007 13:41
It seams to work but it has limitations. You can't use anything with brackets.
Posted: 14th Jun 2007 14:06
Compile this...
+ Code Snippet
sync on
MakeCube 1,100,25,50,10

do
	
	sync
loop

#constant MakeCube make_cube()
function make_cube(id , size , x , y , z)
   make object cube id , size
   position object id , x , y , z
endfunction
Posted: 14th Jun 2007 14:15
Wow, great, didn't know that it works.. fantastic. Thanks!
Posted: 14th Jun 2007 15:00
I should give it a name, hmm... something like "Sasuke's Super Commandlike Functions" . I going to play around for awhile, see if there are more limitations and see what else I can find.

Spin Thing:
+ Code Snippet
SetSyncOn 60

   rem MakeCube id , size , positions x,y,z
   MakeCube 1,50,25,5,100

do

   rem SpinCube id , speed , multiplier x,y,z angles
   SpinCube 1,0.25,-1.0,0.0,1.0

   sync
loop

#constant SetSyncOn set_sync_on()
function set_sync_on(syncrate)
   sync on : sync rate syncrate
endfunction

#constant MakeCube make_cube()
function make_cube(id , size , x , y , z)
   make object cube id , size
   position object id , x , y , z
endfunction

global r as float

#constant SpinCube spin_cube()
function spin_cube(id , speed#, xa# , xy# , xz#)
   r = wrapvalue(r + speed)
   nxa# = r * xa# : nxy# = r * xy# : nxz# = r * xz#
   rotate object id , nxa# , nxy# , nxz#
endfunction
Posted: 14th Jun 2007 15:17
Well spotted - It's a nice curiosity, but it's not too useful because of the limitation of avoiding all brackets on the line. It's perfect for functions that don't need any arguments though.

Does that actually work? It looks to me like the comiler would read that as:

+ Code Snippet

make_cube() 1,100,25,50,10

That's what it does. You can actually type that in without using a constant an it will compile.
Posted: 14th Jun 2007 16:29
Hah. That's pretty neat . I'm surprised the make cube one works at all!

-Xol
Posted: 14th Jun 2007 17:45
A shame that you can't use any more brackets.. so it's not possible to take other functions or calculations as parameter.. :/
Does the compiler support this feature, or is it changed before by the editor? (I mean, does the editor translate 'MyCommand() x,y,z' to 'MyCommand(x,y,z)' or something?)
Posted: 14th Jun 2007 21:24
I think it would probably be the compiler. As far as I know the compiler handles everything involved in the source-code (constants, #includes, etc.). The editor doesn't do a whole lot of anything these days (since TGC can't update it).

-Xol
Posted: 14th Jun 2007 21:37
Oh that's interesting.. I always thought the editor would "pre-compile" it (execute all commands with '#', so #constant and #include..).
Posted: 15th Jun 2007 10:53
The only way I can think of getting around brackets is to do the working out before passing it to the function.

Something like this:
+ Code Snippet
objectnum = 1
size = 20+(15*2)

MakeCube objectnum , size

wait key
end

#constant MakeCube  makecube()
function makecube(id,size)
    make object cube id,size
endfunction
Posted: 15th Jun 2007 13:03



String works fine, if

+ Code Snippet
sync on: sync rate 0
d3d_init
d3d_font 1, "Arial", 12, 0, 0, 0

s$ = "FPS:" + str$(screen fps())


do

d3d_starttext

DFont = 1: DX = 0: DY=0: DColor = rgb(255,255,255)
d_print s$ : rem only if string do first
d_print "Works"
rem not work 'd_print "FPS:" + str$(screen fps())'  
    
d3d_endtext

sync
loop



global DFont, DX, DY, DColor
#constant D_print D_print()

function D_print(s as string)

    D3D_text DFont, DX, DY, 0, s, DColor
    DY = DY + D3D_GETTEXTHEIGHT(Dfont, s)

endfunction


hate everytime write command(something$)
Posted: 15th Jun 2007 14:03
nice bit of info. Do i get to call this "kyuzumaki's xtra super command like functions"? lol kidding

Take a look if you miss out the first bracket and include it in your constant it works fine. As said earlier the compiler is just replacing the constants with their real values before it's compiled.

+ Code Snippet
`leave the  an open bracket on the constant
#constant MakeCube make_cube(

`close the bracket here
MakeCube 1,20,0,0,0)

wait key
end

function make_cube(id , size , x , y , z)
   make object cube id , size
   position object id , x , y , z
endfunction
Posted: 16th Jun 2007 0:35
It is nice, and there are others like it, but...

I once responded to someone's question about #constant by stating it was a text substitution MACRO-like command. I always use it as a straight up assignment to a value. It can be made to do this type of thing, but...when you confuse the compiler, you are on your own.

The intent of constant seems to be text substitution, almost like define. But it is nothing like define at all. It is preprocessed, but, you cannot really use it like a define. (At least that seems like a side-effect, not a feature.)

The upshot for me is that I only use it in my code as a readability tool for bitmasks, and pi, etc.

You can't for example, rem out the remainder of the line with impunity, it can bite you very quickly.

Anyway, Sasuke...you got mad skills, just keep on doing what you are doing, and thanks.
Cheers.
Posted: 3rd Sep 2007 14:58
I'm wondering what actualy happens...
As far as I'm aware, #constant replaces all occurencies of the named variable with the value at compile time so..
+ Code Snippet
#Constant Pi = 3.1415
X#=Sin(Pi)
Y#=Cos(Pi)
2Pi2# = Sqrt(Pi^2)

would compile as...
+ Code Snippet
X#=Sin(3.1415)
Y#=Cos(3.1415)
2Pi2# = Sqrt(3.1415^2)


Nothing new or odd there, but if you assign the value to a function, won't the compiler automaticaly replace all occurencies with the function instead of just calling it, So..
+ Code Snippet
myCube 1,100,1
myCube 2,100,1
mYcube 3,100,1

#constant myCube = MakeCube()
Function MakeCube(Id,Size,Texture)
 Make Object Cube Id,Size
 If Texture > 0 Then Texture Object Id,Texture
EndFunction

would compile into...
+ Code Snippet
 Make Object Cube 1,100
 Texture Object 1,1
 Make Object Cube 2,100
 Texture Object 2,1
 Make Object Cube 3,100
 Texture Object 3,1

instead of just calling the function, resulting in larger than required programs.

I haven't checked if this is correct, I'd be interested to know.

Go Create...
Posted: 3rd Sep 2007 15:29
As said above #constant is just a preprocessor directive and not a data type so you can use it to shorten whatever you like.
Posted: 3rd Sep 2007 15:36
TinTin,

That code wouldn't create inline functions - it would be translated to this before being sent to the compiler:

+ Code Snippet
MakeCube() 1,100,1
MakeCube() 2,100,1
MakeCube() 3,100,1


And for whatever reason, the compiler accepts that as valid input.
Posted: 14th Sep 2007 12:26
Another function thing.

When I was playing around with function I noticed you could put a point between the the words like:

My.CoolFunction( <something> )

And the compiler excepts this, but why won't it except it if the function returns something:

a = My.CoolFunction( <something> )

It will only read up to the point, whats up with that?