ConsoleFunction and syntaxes
by Stefan Lundmark · in Torque Game Engine · 10/14/2004 (5:36 pm) · 13 replies
Alright, simple question yet hard for me.
I'm trying to make a console function that you can pass a number to, and then that number is calculated and added with 12. I've been trying different methods but none seem to work :P
I've been trying to change to const char, char, const, and whatever but it's just pure guessing from my part, and none of that worked out.
Help :)
I'm trying to make a console function that you can pass a number to, and then that number is calculated and added with 12. I've been trying different methods but none seem to work :P
ConsoleFunction(getKey, S32, 2, 0, "")
{
S32 coded = (12 + (argv[1]));
return (coded);
}It seems that if I add a * character infront of the =, the code compiles but gives a totally different return than I expected. In my case it returned 49 when the answer was meant to be 13.Quote:Is what I get if I let the code be, like posted above.
error C2440: 'initializing' : cannot convert from 'const char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
I've been trying to change to const char, char, const, and whatever but it's just pure guessing from my part, and none of that worked out.
Help :)
About the author
#2
Is this documented somewhere? I've been unable to find it if it is.
10/15/2004 (9:11 am)
Am I correct in saying that the int conversion (dAtoi) just can handle a pretty low amount of digits? How do you convert to double (or whatever the handle is that can handle a big amount of numbers)?Is this documented somewhere? I've been unable to find it if it is.
#3
dAtoi converts a string to an int. On most PC's this is the same size as a long (4 bytes). Numbers can range from -2,147,483,648 to 2,147,483,647.
if int is two bytes on your machine then the number range is -32,768 to 32,767.
dAtof converts a string to a float. The number range is 3.40E + 38 to 3.40E + 38.
both these functions and dAtob are found in platform.h and winstrings.cc.
note: dAtoi just does a standard atoi and dAtof does a standard atof. There is plently of help on the web for these two standard functions.
Also, if you looked at some of the exiting ConsoleFunctions that use numbers of as parameters you would of seen the conversion happening.
10/15/2004 (9:57 am)
StefandAtoi converts a string to an int. On most PC's this is the same size as a long (4 bytes). Numbers can range from -2,147,483,648 to 2,147,483,647.
if int is two bytes on your machine then the number range is -32,768 to 32,767.
dAtof converts a string to a float. The number range is 3.40E + 38 to 3.40E + 38.
both these functions and dAtob are found in platform.h and winstrings.cc.
note: dAtoi just does a standard atoi and dAtof does a standard atof. There is plently of help on the web for these two standard functions.
Also, if you looked at some of the exiting ConsoleFunctions that use numbers of as parameters you would of seen the conversion happening.
#4
Looking in the existing code is a given.
Actually I was all trough the consolefunctions.cc and terraformer.cc files for examples, and I found dAtob - but that is a bool thing, and dAtof is a float.
The int conversion I used went nuts with +e23453-1 and whatnot when I printed it out in the console. I'll poke around with this more then and see what I come up with.
Cheers
10/15/2004 (10:31 am)
@Simon Duggan:Looking in the existing code is a given.
Actually I was all trough the consolefunctions.cc and terraformer.cc files for examples, and I found dAtob - but that is a bool thing, and dAtof is a float.
The int conversion I used went nuts with +e23453-1 and whatnot when I printed it out in the console. I'll poke around with this more then and see what I come up with.
Cheers
#5
Sorry, was not implying anything. Just find that most of the answers can be found from looking though the code.
Anyway..
I used the fixed example above, ran some numbers and got some interesting results.
from console I used the following commands
I found that if I used random numbers from 0 to 2,147,483,000 I would get the number + 12 echoed as expected.
BUT,
if I used 0 to -2,147,483,000 I would get strange results. Any number lower than around -1,000,000,000 returned incorrect results.
Not sure how you got +e23453-1 though.
10/15/2004 (11:03 am)
StefanSorry, was not implying anything. Just find that most of the answers can be found from looking though the code.
Anyway..
I used the fixed example above, ran some numbers and got some interesting results.
from console I used the following commands
echo( getKey( 10 ) );;
I found that if I used random numbers from 0 to 2,147,483,000 I would get the number + 12 echoed as expected.
BUT,
if I used 0 to -2,147,483,000 I would get strange results. Any number lower than around -1,000,000,000 returned incorrect results.
Not sure how you got +e23453-1 though.
#6
But, multiplying gets the odd result we spoke about so, I'm pretty much into an dead end :/
it works with 8 numbers I think.. but when I move above that, it get's like you described, odd numbers that have nothing to do with what I "thought" that I wrote in code :)
10/15/2004 (11:17 am)
Got it once when I exagerated my example in the code with alot of numbers. What I'm trying to do is scramble text sent to the server so that it's not as easy to sniff or spoof what is being sent. Making the text into numbers, then multiplying it with 2, or adding a few digits here and there.But, multiplying gets the odd result we spoke about so, I'm pretty much into an dead end :/
it works with 8 numbers I think.. but when I move above that, it get's like you described, odd numbers that have nothing to do with what I "thought" that I wrote in code :)
#7
What I did, was created a DESToString and StringToDES Consolefunctions for when I send text to and from clients.
Oh I did a google on DES and found a couple of simple c functions out there that I modified.
10/15/2004 (11:29 am)
Ah, ok, I see what your trying to do.What I did, was created a DESToString and StringToDES Consolefunctions for when I send text to and from clients.
Oh I did a google on DES and found a couple of simple c functions out there that I modified.
#8
10/15/2004 (11:37 am)
Thanks Simon, I'll try that.
#9
10/15/2004 (11:38 am)
Perhaps you could just encode the text. Instead of turning it into a numeric, just turn all "a"s into "z"s or something. You could also make some random way to calculate the codec(so every line of text is encoded differently).
#10
[code]
%name = strreplace(%name, "a", "120");
%name = strreplace(%name, "b", "112");
%name = strreplace(%name, "c", "132");
%name = strreplace(%name, "d", "140");
%name = strreplace(%name, "e", "108");
%name = strreplace(%name, "f", "103");
%name = strreplace(%name, "g", "101");
%name = strreplace(%name, "h", "102");
%name = strreplace(%name, "i", "149");
%name = strreplace(%name, "j", "132");
%name = strreplace(%name, "k", "165");
%name = strreplace(%name, "l", "119");
%name = strreplace(%name, "m", "166");
%name = strreplace(%name, "n", "190");
%name = strreplace(%name, "o", "187");
%name = strreplace(%name, "p", "165");
%name = strreplace(%name, "q", "134");
%name = strreplace(%name, "r", "111");
%name = strreplace(%name, "s", "144");
%name = strreplace(%name, "t", "138");
%name = strreplace(%name, "u", "122");
%name = strreplace(%name, "v", "171");
%name = strreplace(%name, "w", "188");
%name = strreplace(%name, "x", "105");
%name = strreplace(%name, "y", "107");
%name = strreplace(%name, "z", "116");
%name = strreplace(%name, "
10/15/2004 (11:49 am)
What I was initially trying, just as a simple scrambler..[code]
%name = strreplace(%name, "a", "120");
%name = strreplace(%name, "b", "112");
%name = strreplace(%name, "c", "132");
%name = strreplace(%name, "d", "140");
%name = strreplace(%name, "e", "108");
%name = strreplace(%name, "f", "103");
%name = strreplace(%name, "g", "101");
%name = strreplace(%name, "h", "102");
%name = strreplace(%name, "i", "149");
%name = strreplace(%name, "j", "132");
%name = strreplace(%name, "k", "165");
%name = strreplace(%name, "l", "119");
%name = strreplace(%name, "m", "166");
%name = strreplace(%name, "n", "190");
%name = strreplace(%name, "o", "187");
%name = strreplace(%name, "p", "165");
%name = strreplace(%name, "q", "134");
%name = strreplace(%name, "r", "111");
%name = strreplace(%name, "s", "144");
%name = strreplace(%name, "t", "138");
%name = strreplace(%name, "u", "122");
%name = strreplace(%name, "v", "171");
%name = strreplace(%name, "w", "188");
%name = strreplace(%name, "x", "105");
%name = strreplace(%name, "y", "107");
%name = strreplace(%name, "z", "116");
%name = strreplace(%name, "
#11
So the string "abc" (3 characters) would be "120112132" (9 characters) You could make something like this work if you never converted a number (such as 1) So if you had:
%name = strreplace(%name, "1", "100");
And had "abc" you would end up with:
"120112132" before the %name = strreplace(%name, "1", "100");
and
"10020100100210032" after the %name = strreplace(%name, "1", "100");
Make sense?
10/15/2004 (5:22 pm)
You are replacing 1 character with 3. So the string "abc" (3 characters) would be "120112132" (9 characters) You could make something like this work if you never converted a number (such as 1) So if you had:
%name = strreplace(%name, "1", "100");
And had "abc" you would end up with:
"120112132" before the %name = strreplace(%name, "1", "100");
and
"10020100100210032" after the %name = strreplace(%name, "1", "100");
Make sense?
#12
I'm not sure I got your point.
Replacing 1 character with 3 shouldn't mean any problems, right?
On the server, I just grab the first three digits in the key, then take the next three and convert them back to letters.
This works fine for me anyway, the problem is that if I use more than 4 letters to convert, the key get's negative. :)
Example!
"test" = 138108144138
"testi" = 138108144138149
All is fine! But when you double these (%string * 2) the second example above won't work.
10/17/2004 (2:04 am)
@Dan - Forgotten Soul:I'm not sure I got your point.
Replacing 1 character with 3 shouldn't mean any problems, right?
On the server, I just grab the first three digits in the key, then take the next three and convert them back to letters.
This works fine for me anyway, the problem is that if I use more than 4 letters to convert, the key get's negative. :)
Example!
"test" = 138108144138
"testi" = 138108144138149
All is fine! But when you double these (%string * 2) the second example above won't work.
#13
I finally understood what this was called in C++ though.
Overflow.
I searched the net everywhere and got to a solution, kinda.
This is my previous code. The number it returns is -870183749. Which is wrong.
The tutorial I was reading said that you could pass this overflow with using unsigned integers. I tried that, but got the same problem.
This is the change that the tutorial I read was proposing, that would solve the overflow.
Maybe this doesn't work in TGE's codebase?
10/22/2004 (4:46 pm)
I still got this problem :)I finally understood what this was called in C++ though.
Overflow.
I searched the net everywhere and got to a solution, kinda.
This is my previous code. The number it returns is -870183749. Which is wrong.
The tutorial I was reading said that you could pass this overflow with using unsigned integers. I tried that, but got the same problem.
ConsoleFunction(getKey, S32, 2, 0, "")
{
int callback = 3424783547;
return (callback);
}This is the change that the tutorial I read was proposing, that would solve the overflow.
ConsoleFunction(getKey, S32, 2, 0, "")
{
unsigned int callback = 3424783547;
return (callback);
}Maybe this doesn't work in TGE's codebase?
Torque Owner Dan -
Try:
ConsoleFunction(getKey, S32, 2, 0, "") { S32 coded = (12 + (dAtoi(argv[1]))); return (coded); }