Jump to content

Warthog scripting - using "strings", passing to function, comparing, printing


TheT
 Share

Recommended Posts

Ladies and Gents,

 

This is for working with TARGET for "Warthog Thrustmaster HOTAS"

 

I am trying to use a string as a variable for condition detection:

Code:

if (my_condition == 'red') do_something();

 

I've looked at "strcmp" function, but I keep getting a "Runtime Error: Bad alias" error in code:

alias test3 = 'test';
alias test4 = 'test';
int myfunction() {
if ( strcmp(test3, test3) ) {
	printf("OK 12 \xa");
}
}

 

What variable type do I need/can use to achieve this feat?

And in this manner, would I be able to pass this variable into a function?

Code:

 

??? my_string_var = 'blue';
my_function(my_string_var);

// snip

int my_function (??? my_local_var = 'some default value') {}

I've been trying to use Alias, but it does not comply well. I went as far as trying to use structs and char-array compare functions, but have not gotten that to work either.

 

 

And, while done with that, can I output the variable contents somehow?

Code:

printf(" and the var is " + my_string_var + "!\xa");
// or I will take even
printf(my_string_var);

 

I understand printf wants a "parameter" for printing, but %s doesn't work =(

printf("OK 2 %s \xa", 'test3');

 

I figure I can use int conditions, but they would be a number and not a string.

 

I would also love to know if I can actually build C-style Class functionality in tmc files, but I am not holding my breath on possibility of this. =)

 

Thanks much.

~T

Link to comment
Share on other sites

A few things...

 

  • String literals need to be in double quotes, not single.
  • A later reference to an alias needs to be preceded by &.
  • strcmp() returns a 0 if the values are equal, and >0 or <0 if one value is higher.

So...

alias test3 = "test";
alias test4 = "test";
// ~/~
int myfunction() {
   if ( strcmp(&test3, &test4) == 0 ) {
       printf("OK 12 \xa");
   }
}

You can also pass an alias to a function...

alias my_string_var = "blue";
// ~/~
my_function(&my_string_var);

// snip

int my_function (alias my_local_var) {
// don't think you can set default parameters in TARGET
// remember to refer to &my_local_var with the &
}

For printf, %s works, but you need that & again (and no quotes)...

printf("OK 2 %s \xa", &test3);

As an alternative to using an alias, you can also use define, which then does not make use of & when referring to it. Swapping it for alias in your first example...

define test3 "test"
define test4 "test"
// ~/~
int myfunction() {
   if ( strcmp(test3, test4) == 0 ) {
       printf("OK 12 \xa");
   }
}


Edited by Achiral
Corrected incorrect define format
Link to comment
Share on other sites

Achiral, Thanks for the help. It is very much appreciated.

 

I was not aware the Double-Quotes were required. Good to know.

 

I have not tried out your suggestions yet, but just to double-check:

Alias is passed by reference, with the use of & sign? Just want to confirm that's the case. All of the other variable seem to be by-value. I can see the use for alias for items like Joystick and Thruster box inputs (aka you send your commands to a physical location, which is kind of clever).

 

I was thinking about defines, but wanted to make sure I have the string base down first without using defines as scapegoats.

 

Thanks.

Link to comment
Share on other sites

Had a chance to test out some code. Unfortunately, getting a bad-alias error:

"Runtime Error: Bad alias"

 

Code:

alias test3 = 'test';
alias test4 = 'test';
int my_function(  ) {
if ( strcmp(&test3, &test3) == 0 ) {
	printf("OK 12 \xa");
}
}

Link to comment
Share on other sites

Correct, alias is always passed by reference with a prepended &. Well, almost always. One case where it wouldn't be is if you assign the alias to a constant via define. Once done though, you would need to access both by reference...

alias xxx = "test";
someFunction(&xxx);
define yyy xxx // yyy is now the same as xxx - i.e., an alias
someFunction(&yyy);

Note also that I messed up my define examples in the last post (it was late). A define does not use =, nor is the line terminated with ;. Hopefully, I can edit that still. The above format is correct.

 

To be clear on the quotes - double quotes for string literals, single quotes for characters. So if you wanted to define a constant for gear toggle as keyboard g, you would use...

define gear_toggle 'g'

But for a string, you would use...

define my_color "red"

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...