Getting string data from integer variable

Non-platform specific questions.

Getting string data from integer variable

Postby Troodon » Sun May 20, 2007 5:18 pm

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:
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby makslane » Sun May 20, 2007 5:45 pm

If the number is an integer, use:

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

if(text[0] == '2')
{
  ...
}
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby Troodon » Sun May 20, 2007 5:48 pm

Thanks! I will try this as soon as I get home! :D :D
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby Troodon » Sun May 20, 2007 7:46 pm

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?
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby makslane » Sun May 20, 2007 8:31 pm

Double check the code. Works here.
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby DilloDude » Mon May 21, 2007 8:01 am

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')
...
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby Troodon » Mon May 21, 2007 2:57 pm

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')
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby makslane » Mon May 21, 2007 3:42 pm

Sorry, use an other string var instead.
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby Game A Gogo » Tue May 22, 2007 12:07 am

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.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Postby Troodon » Tue May 22, 2007 5:45 am

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);
}
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby DilloDude » Tue May 22, 2007 11:40 am

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).
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Create number in pieces

Postby Troodon » Tue May 22, 2007 4:44 pm

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:
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Re: Create number in pieces

Postby Sgt. Sparky » Tue May 22, 2007 4:52 pm

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)
Image
Random Links:
viewtopic.php?p=19474#19474
Right now (10/14/2009) I'm working on some C++ projects, but I might be able to help if you have some Game Editor questions. :)
User avatar
Sgt. Sparky
 
Posts: 1850
Joined: Sat Oct 07, 2006 5:28 pm
Location: Somewhere out there, beneath the pale blue sky...
Score: 236 Give a positive score

Postby makslane » Tue May 22, 2007 4:55 pm

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;
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby Troodon » Wed May 23, 2007 1:05 pm

Thanks. :D

By the way, Sparky talked something about arrays. What are they? Can they be saved like normal variables?
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Next

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest