Can I convert a float to a int int T2D 's scripting?
by Ken · in Torque Game Builder · 07/08/2005 (1:32 am) · 6 replies
In T2D scripting 5/2 == 2.5,but I want to get a int value: 2 = 5/2; how can I do?
#3
(edit)
Oh, btw, I think the math functions are in 'mMathFn.cc', not 'mConsoleFunctions.cc'
07/08/2005 (9:48 am)
But that will only work with odd numbers, no?%value = mFloor( 6 / 2 + 0.5 ); //%value = 3.5I wrote a round() function like this. There totally is probably a better way to do this... I feel like this is kind of a brute force approach.
function mRound( %val ){
if( %val - mFloor( %val ) < 0.5 )
{
return mFloor( %val );
}
else
{
return mCeil( %val );
}
}At least I think that's right ;)(edit)
Oh, btw, I think the math functions are in 'mMathFn.cc', not 'mConsoleFunctions.cc'
#4
The "mFloor(n+0.5)" is a classic way of rounding to the nearest and should work. It'd be a good idea to have a "mRound()" that does this though.
- Melv.
07/08/2005 (10:17 am)
@Phil: Yin was specifically asking about scripting so I pointed to where all the math script-functions are rather than the C++ stuff but yes, the C++ stuff is in "mMathFn.cc".The "mFloor(n+0.5)" is a classic way of rounding to the nearest and should work. It'd be a good idea to have a "mRound()" that does this though.
- Melv.
#5
and doubly so since I was looking in consoleFunctions.cc, not mConsoleFunctions.cc
So the functions in mMathFn.cc are only available in C++? And mConsoleFunctions.cc is what exposes them to script? I was wondering why some of the ones in mMathFn.cc didn't work (in script), so that would make sense if that were so.
Sorry guys, I'm just blabbing away here in my ignorance :)
07/08/2005 (8:02 pm)
See, I knew I was missing something... of course that will work. Haha, now I feel stupid :/and doubly so since I was looking in consoleFunctions.cc, not mConsoleFunctions.cc
So the functions in mMathFn.cc are only available in C++? And mConsoleFunctions.cc is what exposes them to script? I was wondering why some of the ones in mMathFn.cc didn't work (in script), so that would make sense if that were so.
Sorry guys, I'm just blabbing away here in my ignorance :)
#6
There are two macros that expose the C++ stuff to the script. In-fact, they're really easy to use. The first is "ConsoleFunction" which exposes a non-class function (as the math ones are) and the second is "ConsoleMethod" which exposes a class-method.
Searching for these is a good way to see what you can call from C++. If you're ever wondering what a specific class can do then look in the C++ code and search for these macros to tell you exactly what you can do. It can be a little more complex if objects inherit from another but in that case, just look up the C++ code for the parent class and so on.
- Melv.
07/09/2005 (12:38 am)
Quote:So the functions in mMathFn.cc are only available in C++? And mConsoleFunctions.cc is what exposes them to script? I was wondering why some of the ones in mMathFn.cc didn't work (in script), so that would make sense if that were so.Absolutely! :)
There are two macros that expose the C++ stuff to the script. In-fact, they're really easy to use. The first is "ConsoleFunction" which exposes a non-class function (as the math ones are) and the second is "ConsoleMethod" which exposes a class-method.
Searching for these is a good way to see what you can call from C++. If you're ever wondering what a specific class can do then look in the C++ code and search for these macros to tell you exactly what you can do. It can be a little more complex if objects inherit from another but in that case, just look up the C++ code for the parent class and so on.
- Melv.
Associate Melv May
... to round down or...
...to round-up.
You can find more of these in "mConsoleFunctions.cc".
Hope this helps,
- Melv.