Page 1 of 1

void function(text, ...)

PostPosted: Thu Apr 05, 2012 4:16 pm
by Hblade
How would I make this work? I want to be able to display variables in a command that creates a text actor, but for some reason, when I do this, it always ends up as 392 O-o no matter what, even if you set it to 94 lol
Code: Select all
function("variable: %d", var);


or:
Code: Select all
function("variables: %d-%d", xscreen, yscreen);


So how would I get this to work?

Re: void function(text, ...)

PostPosted: Thu Apr 05, 2012 7:56 pm
by skydereign
So you are just writing a wrapper function to sprintf? Make sure the char* you specify is a const char*.
Code: Select all
void
function (const char* format, int xs, int ys)
{
    sprintf(text, format, xs, ys);
}

But not sure why you would even do this, because you already know what format you want (at least the part that involves the integers you pass it). So the same function can be accomplished like this.
Code: Select all
sprintf(text, "variables: %d-%d", xscreen, yscreen);

Re: void function(text, ...)

PostPosted: Thu Apr 05, 2012 8:43 pm
by Hblade
I've worked around it using text variables. The command is:
Code: Select all
createWindow("text_actor_to_display", "message", x,y,width,height, index);


Check game demos to see what I mean. But now its all better.

Re: void function(text, ...)

PostPosted: Thu Apr 05, 2012 9:01 pm
by tintran
I think Hblade is trying to use something like
http://www.thinkage.ca/english/gcos/expl/c/incl/stdarg.html
like a function that will accept an optional list of parameters like sprintf right?
I tried looking into this but there's no way to include <stdarg.h> so i can't even play around with functions va_start, va_arg, va_end.

Re: void function(text, ...)

PostPosted: Thu Apr 05, 2012 9:06 pm
by skydereign
It looked like he might be, but as you've noted gE doesn't support argument lists so it would be better just to call a function that can handle it (his function looks just like sprintf without the text).

Re: void function(text, ...)

PostPosted: Thu Apr 05, 2012 9:29 pm
by lcl
Hblade, the easiest way to go is to create a local string for that, a string that only is there in the event you call the function and only the frame you call the function. You can do it like this:
Code: Select all
char myTemporaryString[256];
sprintf(myTemporaryString, "Variable: %i\nMy string: %s", someIntVar, someString);
//And then just input the 'myTemporaryString' into your function,
//to the place where you want the text to be, like:
myFunction(myTemporaryString, ...other arguments of the function...);

So you see that there's actually no need for the thing you want. =D

(Of course, I don't recommend using namemonsters like 'myTemporaryString' ... XD)