Derive type from console input
by Tim Kreger · in Torque Game Engine · 08/24/2005 (9:40 pm) · 8 replies
Hi all,
is there any way to establish what the types of the argv s are from the console when writing a console method. I'm writing a method which needs to know whether the arg is a string float int 3f 4f 3 i etc.
Failing that does anyone have some ideas for implennting a string analyser for deriving this info from the arg string.
Any pointers ideas etc welcome.
thanks
Tim
is there any way to establish what the types of the argv s are from the console when writing a console method. I'm writing a method which needs to know whether the arg is a string float int 3f 4f 3 i etc.
Failing that does anyone have some ideas for implennting a string analyser for deriving this info from the arg string.
Any pointers ideas etc welcome.
thanks
Tim
About the author
#2
1) Alpha chars? anything not in [0-9] negative, decimal or comma?
Yes - String
No - Numeric, go to step 2
2) Comma(s)?
Yes - How many equals type of point (3F = 2 commas)
No - Float or Int
3) Decimal point?
Yes - Floating point
No - Integer
3)
08/25/2005 (12:23 pm)
I would write some code that analyzes the input with rules like:1) Alpha chars? anything not in [0-9] negative, decimal or comma?
Yes - String
No - Numeric, go to step 2
2) Comma(s)?
Yes - How many equals type of point (3F = 2 commas)
No - Float or Int
3) Decimal point?
Yes - Floating point
No - Integer
3)
#3
I had made the decision to supply type tags at the console, I was pretty sure it was not possible. As you mention Stephen it isn't a trivial task to acertain type from a string . I guess I was just looking for confirmation that my approach was the way to go. (the result just doesn't look as nice as other TS stuff).
Many thanks for your quick replies.
Cheers
Tim
08/25/2005 (4:55 pm)
Thank Stephen and Brett,I had made the decision to supply type tags at the console, I was pretty sure it was not possible. As you mention Stephen it isn't a trivial task to acertain type from a string . I guess I was just looking for confirmation that my approach was the way to go. (the result just doesn't look as nice as other TS stuff).
Many thanks for your quick replies.
Cheers
Tim
#4
I think though that you may (unless you have some -really- specific requirements here) be barking up a non-existant tree: when you write a console method you need to know what the method is for, and what params it is going to expect, and when you call that console method from script, you simply need to send the params of the type that it needs...no real need to explicitly worry about the type itself, just do some data validation on the contents of each parameter if they may be something that could break your method.
In other words, robustness in design is really good, but you can go overboard with it as well: there is a certain level of implicit trust of those calling your console method, in that they need to know how to call it properly.
08/25/2005 (5:10 pm)
If this is critical for some reason to your project, then you could of course re-write the Console so that it allows for tagging strings with a suggested/required type, and then parse out that string as part of your parameter reception...in fact, you wouldn't actually have to re-write the param passing mechanism in the console itself, simply add the tags in on the script side, and then decode them from the argv string in any ConsoleMethod that uses the concept.I think though that you may (unless you have some -really- specific requirements here) be barking up a non-existant tree: when you write a console method you need to know what the method is for, and what params it is going to expect, and when you call that console method from script, you simply need to send the params of the type that it needs...no real need to explicitly worry about the type itself, just do some data validation on the contents of each parameter if they may be something that could break your method.
In other words, robustness in design is really good, but you can go overboard with it as well: there is a certain level of implicit trust of those calling your console method, in that they need to know how to call it properly.
#5
I was waiting the for teh "what the hell are you trying to do!" question. I'm implementing an OSC (Open Sound Control) bridge. OSC requires explicit and multivariate typing. So the function needs and arbitray types and #args. My current implementation looks something like this
sendOSC(%port, %nameAddres, type, %value, type, %value.....);
so at the console I can put something like this. (Although I've devised my own type pnemonics)
sendOSC($oscPort, "\sendPosition", VectorF, %object.position);
This will send that data to my sound app and away we go. There isn't any way to really validate the data as it will break at the sound end rather than anything torque side. People that use OSC are likely to be pretty type savvy anyway.
I basically translate the Torque types into a usable OSC format on the C++ side.
Thanks for your comments, its help validate my approach in my own mind as alay the doubts I was having.
Cheers
Tim
08/25/2005 (5:43 pm)
Hi Stephen,I was waiting the for teh "what the hell are you trying to do!" question. I'm implementing an OSC (Open Sound Control) bridge. OSC requires explicit and multivariate typing. So the function needs and arbitray types and #args. My current implementation looks something like this
sendOSC(%port, %nameAddres, type, %value, type, %value.....);
so at the console I can put something like this. (Although I've devised my own type pnemonics)
sendOSC($oscPort, "\sendPosition", VectorF, %object.position);
This will send that data to my sound app and away we go. There isn't any way to really validate the data as it will break at the sound end rather than anything torque side. People that use OSC are likely to be pretty type savvy anyway.
I basically translate the Torque types into a usable OSC format on the C++ side.
Thanks for your comments, its help validate my approach in my own mind as alay the doubts I was having.
Cheers
Tim
#6
08/25/2005 (5:58 pm)
Yeah, I kind of figured you might actually have a valid reason for needing this, hehe. In that case, I would suggest doing pretty much exactly what you are doing: come up with some form of information tag that you can use from script to indicate what datatype the next param is, and parse from there. You'll want to grab the params 2 at a time off of argv, and then have a small state machine (switch statement is fine) that will properly populate your internal values for delivery to the destination.
#7
In both case the string is printed to console okay. The function OSCNewMethod() isn't working in the second example, and the only thing I can guess is that something is screwed in the consoles argv type.
Any help appreciated.
Tim
10/08/2005 (8:38 pm)
Can anyone tell me why the first code works and the second doesn't? I'm trying to pass a string from the console to init some other routines.ConsoleFunction(newOSCmessage, S32, 2, 2, "install new OSC message responder")
{
const char *addresName;
addresName = "test";
OSCInitMethodQueryResponseInfo(&omqris);
OSCNewMethod(addresName, topLevelContainer, PlayCallback, NULL, &omqris);
Con::printf("%s", addresName);
}ConsoleFunction(newOSCmessage, S32, 2, 2, "install new OSC message responder")
{
const char *addresName;
addresName = argv[1];
OSCInitMethodQueryResponseInfo(&omqris);
OSCNewMethod(addresName, topLevelContainer, PlayCallback, NULL, &omqris);
Con::printf("%s", addresName);
}In both case the string is printed to console okay. The function OSCNewMethod() isn't working in the second example, and the only thing I can guess is that something is screwed in the consoles argv type.
Any help appreciated.
Tim
#8
Forgot about the scope and persistence of the variable etc. Back to the Basics of C 101.
Cheers
Tim
10/09/2005 (12:54 am)
I've sorted it out thanks.Forgot about the scope and persistence of the variable etc. Back to the Basics of C 101.
Cheers
Tim
Torque 3D Owner Stephen Zepp