Page 1 of 5

Getting string data from integer variable

PostPosted: Sun May 20, 2007 5:18 pm
by Troodon
Hi,
How can I get string data from a number variable?
For example, I want to analyze from a number (for example 2047):

if (first number == 2)
{
something happens
}

etc etc :wink:

PostPosted: Sun May 20, 2007 5:45 pm
by makslane
If the number is an integer, use:

Code: Select all
sprintf(text, "%d", number);

if(text[0] == '2')
{
  ...
}

PostPosted: Sun May 20, 2007 5:48 pm
by Troodon
Thanks! I will try this as soon as I get home! :D :D

PostPosted: Sun May 20, 2007 7:46 pm
by Troodon
Uhm, I entered
Code: Select all
sprintf(text, "%d", numero);

if(text[0] == '2')
{
  CreateActor("numero", "icon", "no parent", "no path", rand(10), rand(10), false);
}


And it says:
Errror line 1: passing a struct/union to variadic function "sprintf"

Where do I put the actor name and what's the input number place?

PostPosted: Sun May 20, 2007 8:31 pm
by makslane
Double check the code. Works here.

PostPosted: Mon May 21, 2007 8:01 am
by DilloDude
The input number is the number in the first line. It should be
Code: Select all
sprintf(text, "%d", your_input_number);
...

Also, if you don't want to display the number as 'text', you can create another temporary variable:
Code: Select all
char temp[10];
sprintf(temp, "%d"...
if (temp[0] == '2')
...

PostPosted: Mon May 21, 2007 2:57 pm
by Troodon
It gave me another error message but only when I go to demo mode:

Code: Select all
luku -> key down (a, Atleast one key is pressed), line 4: READ attempted beyond allowed access area


The line 4 is:
Code: Select all
if(text[0] == '2')

PostPosted: Mon May 21, 2007 3:42 pm
by makslane
Sorry, use an other string var instead.

PostPosted: Tue May 22, 2007 12:07 am
by Game A Gogo
The var "text" is already a var, it is the text of an actor, like the textNumber is the number of an actor.

Try using Text instead.

PostPosted: Tue May 22, 2007 5:45 am
by Troodon
Now it says:
Suspicious pointer conversion
Must have pointer

This is my current code:
Code: Select all
numero = rand(1000);

sprintf(Text, "%d", numero);

if(Text[0] == '2')
{
  CreateActor("tuloste", "shot", "no parent", "no path", rand(100), rand(100), false);
}

PostPosted: Tue May 22, 2007 11:40 am
by DilloDude
You'll need to define Text before you use it, at the beginning of the script:
Code: Select all
char Text[20];

The number should be the maximum number of characters you'll want plus one (because the string must end in a null terminator).

Create number in pieces

PostPosted: Tue May 22, 2007 4:44 pm
by Troodon
Fantastic! It works! :D

I will ask you still one question, then I will let you in peace :oops:

Is it possible to create that integer with a way something like this:

Text[0] = 'rand(9)'
Text[1] = 'rand(9)'
Text[2] = 'rand(2)'
etc etc

If it's not possible, nevermind.
I know I could do this easier with many variables instead of one but my project has too many variables to be displayed in the script window. In my project this will be used like a gene. Little difficult to explain but it should be in one integer variable to work. :wink:

Re: Create number in pieces

PostPosted: Tue May 22, 2007 4:52 pm
by Sgt. Sparky
tekdino wrote:I know I could do this easier with many variables instead of one but my project has too many variables to be displayed in the script window.

you can make an array and Access points of the array. :)
(one variable to access 1000's or more functions)

PostPosted: Tue May 22, 2007 4:55 pm
by makslane
Use:

Code: Select all
Text[0] = rand(9) + '0';
Text[1] = rand(9) + '0';


All C strings must end with a NULL char. So, in the last position put:

Code: Select all
Text[last pos] = 0;

PostPosted: Wed May 23, 2007 1:05 pm
by Troodon
Thanks. :D

By the way, Sparky talked something about arrays. What are they? Can they be saved like normal variables?