Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, Nymphodora), 485 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
How to extract number from a string? #447112
11/17/14 16:46
11/17/14 16:46
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
T
tolu619 Offline OP
Junior Member
tolu619  Offline OP
Junior Member
T

Joined: Jun 2014
Posts: 97
Lagos, Nigeria
I'm trying to check if the player is in any region and if so, check if the region name starts with the word "abyss". If it does, it would be "abyss1", "abyss2", "abyss3", etc. So I want to extract the number at the end of the string and convert it to an integer. I've played around with most of the string manipulation functions defined in strio.c but somehow, I keep getting empty pointer exceptions.
Code:
//GLOBAL VARAIBLES
STRING* AbyssNameString = str_create("");
STRING* AbyssNumberString = str_create("");
STRING* TempString = str_create("");
STRING* ComparisonString = str_create("Abyss");

//THESE LINES ARE INSIDE PLAYER'S WHILE(1) LOOP
if(region_find(AbyssNameString, my.x))//check if ent is in region & store name of region
        {
            if(str_cmpni(AbyssNameString, "Abyss")) //if the region is an abyss
            {        	
        	    str_parse_head(AbyssNumberString, AbyssNameString, "Abyss"); //store number that comes after 'abyss'        
                if(region_check(AbyssNameString,vMin,vMax))
                {
             	   vec_set(my.x, Player1RespawnPosition[str_to_num(AbyssNumberString)]); 
                }        	
            }        
        }
//THIS DOESN'T WORK


Code:
if(region_find(AbyssNameString, my.x))//check if ent is in region & store name of region
        {
TempString = str_cut(NULL, AbyssNameString, 1, 5); //store first 5 letters in TempString
            if(str_cmpi(TempString, "Abyss")) //if the region is an abyss
            {        	
        	    str_parse_head(AbyssNumberString, AbyssNameString, "Abyss"); //store number that comes after 'abyss'        
                if(region_check(AbyssNameString,vMin,vMax))
                {
             	   vec_set(my.x, Player1RespawnPosition[str_to_num(AbyssNumberString)]); 
                }        	
            }        
        }
//THIS DOESN'T WORK


Code:
if(region_find(AbyssNameString, my.x))//check if ent is in region & store name of region
        {
TempString = str_cut(NULL, AbyssNameString, 1, 5); //store first 5 letters in TempString
            if(str_cmpi(TempString, ComparisonString)) //if the region is an abyss
            {        	
        	    str_parse_head(AbyssNumberString, AbyssNameString, "Abyss"); //store number that comes after 'abyss'        
                if(region_check(AbyssNameString,vMin,vMax))
                {
             	   vec_set(my.x, Player1RespawnPosition[str_to_num(AbyssNumberString)]); 
                }        	
            }        
        }
//THIS DOESN'T WORK


And I tried many other similar permutations

Re: How to extract number from a string? [Re: tolu619] #447115
11/17/14 17:48
11/17/14 17:48
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
why don't you just use clip/trunc (don't know which of these two it was, always mix them up) to cut off the fist couple of letters and the use str_to_int?

oh and that's not how you define global vars:
Code:
STRING* AbyssNameString = str_create("");
STRING* AbyssNumberString = str_create("");
STRING* TempString = str_create("");
STRING* ComparisonString = str_create("Abyss");


Last edited by Kartoffel; 11/17/14 17:56.

POTATO-MAN saves the day! - Random
Re: How to extract number from a string? [Re: Kartoffel] #447127
11/18/14 14:18
11/18/14 14:18
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
T
tolu619 Offline OP
Junior Member
tolu619  Offline OP
Junior Member
T

Joined: Jun 2014
Posts: 97
Lagos, Nigeria
I thought the global vars might be the problem but it didn't make a difference when I declared them as
Code:
STRING* AbyssNameString = "";
STRING* AbyssNumberString = "";
STRING* TempString = "";
STRING* ComparisonString = "";


Just to be sure, I just declared them this way again and used str_clip and it still didn't work
Code:
if(region_find(AbyssNameString, my.x))//check if ent is in region & store name of region
        {
        	wait(5);
        	str_printf(AbyssNameString); //IT CRASHES BEFORE I EVEN GET THE RESULTS OF THIS LINE
        	str_cpy(TempString, AbyssNameString);
        	
            
            str_clip(TempString, 5); //store first 5 letters in TempString
            if(str_cmpi(TempString, "Abyss")) //if the region is an abyss
            {        	
        	    str_parse_head(AbyssNumberString, AbyssNameString, "Abyss"); //store number that comes after 'abyss'        
                if(region_check(AbyssNameString,vMin,vMax))
                {
             	   vec_set(my.x, Player1RespawnPosition[str_to_num(AbyssNumberString)]); 
                }        	
            }        
        }


Re: How to extract number from a string? [Re: tolu619] #447128
11/18/14 14:23
11/18/14 14:23
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
you kind of misunderstood this...
you use str_cmp(ni) to check for 'abyss'. after that you delete the first 5 letters of the string, which means the number will be at the beginning of the string and str_to_int will do it's job.


POTATO-MAN saves the day! - Random
Re: How to extract number from a string? [Re: Kartoffel] #447129
11/18/14 20:51
11/18/14 20:51
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
T
tolu619 Offline OP
Junior Member
tolu619  Offline OP
Junior Member
T

Joined: Jun 2014
Posts: 97
Lagos, Nigeria
Doesn't this delete the first 5 characters and store only the numerical part?
Code:
str_parse_head(AbyssNumberString, AbyssNameString, "Abyss"); //store number that comes after 'abyss'



What bothers me is that I think it's crashing right from the first if statement. My str_printf line doesn't print anything when I run my tests.

Re: How to extract number from a string? [Re: tolu619] #447130
11/18/14 22:32
11/18/14 22:32
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
First of all, you've created this topic in wrong forum (mods please move it to the correct one?), "User Resources" - forum used to share something with community, it's used for contributions!

About your question, take a look at this example:
Code:
// get the name of the region via 'region_find'!
// then save it into temporary string:
STRING* regName = "Abyss1";
STRING* tempStr = "#10";
str_cpy(tempStr, regName);
// then, you have to check if we've entered any of "Abyss" regions:
if(str_cmp(str_trunc(tempStr, 1), "Abyss")){
           // after that, you can check for the number of the region:
           if(str_cmp(str_parse(NULL, regName, 6), "1")){
                       // entered first Abyss region here!
           }
}

This example works, as I've tested it right before sharing! Probably not the best way, but should handle your situation without any troubles!

Greets


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: How to extract number from a string? [Re: 3run] #447131
11/18/14 22:43
11/18/14 22:43
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
T
tolu619 Offline OP
Junior Member
tolu619  Offline OP
Junior Member
T

Joined: Jun 2014
Posts: 97
Lagos, Nigeria
Thanks! I'll try it out! The topic creation thing must have been on User Resources by default, I didn't edit it at all. I didn't even notice the option to do so.

Re: How to extract number from a string? [Re: tolu619] #447132
11/19/14 06:03
11/19/14 06:03
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Originally Posted By: tolu619
The topic creation thing must have been on User Resources by default, I didn't edit it at all. I didn't even notice the option to do so.
You can create topics under almost any forum here, next time ask for help under "Lite-C Programming".


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: How to extract number from a string? [Re: 3run] #447147
11/20/14 10:42
11/20/14 10:42
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
T
tolu619 Offline OP
Junior Member
tolu619  Offline OP
Junior Member
T

Joined: Jun 2014
Posts: 97
Lagos, Nigeria
Originally Posted By: 3run
Originally Posted By: tolu619
The topic creation thing must have been on User Resources by default, I didn't edit it at all. I didn't even notice the option to do so.
You can create topics under almost any forum here, next time ask for help under "Lite-C Programming".

Yes sir! Can't I change which forum my topic falls under myself after creation?

Re: How to extract number from a string? [Re: 3run] #447148
11/20/14 10:49
11/20/14 10:49
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
T
tolu619 Offline OP
Junior Member
tolu619  Offline OP
Junior Member
T

Joined: Jun 2014
Posts: 97
Lagos, Nigeria
Thanks, 3run!
I was finally able to solve it. For some reason, global variables didn't work. So I declared the variables inside my player's function and extracted the numbers like so:
Code:
function PlayerMovementFunction(MONSTER* Mon)
{
	STRING* AbyssNameString = str_create("");
    STRING* AbyssNumberString = str_create("");
    STRING* TempString =  str_create("#10");
while(1)
	{
		vec_set(vMin,my.x);
        vec_set(vMax,my.x);
        vec_add(vMin,my.min_x);
        vec_add(vMax,my.max_x);
        
        //CHECK IF PLAYER HAS FALLEN INTO AN ABYSS REGION, THEN RESPAWN HIM AT CORRESPONDING RESPAWN POINT
        if(region_find(AbyssNameString, my.x))//check if ent is in region & store name of region
        {
        	str_cpy(TempString, AbyssNameString);
        	TempString = str_cut(NULL, AbyssNameString, 1, 5); //store first 5 letters in TempString
            if(str_cmpi(TempString, "Abyss")) //if the region is an abyss
            {
        	    str_cpy(AbyssNumberString, AbyssNameString);
        	    str_clip(AbyssNumberString, 5); //Delete first 5 characters then store resulting number
                if(region_check(AbyssNameString,vMin,vMax))
                {
             	   vec_set(my.x, Player1RespawnPosition[str_to_num(AbyssNumberString)]); 
                }        	
            }        
        }


I decided to name any Abyss region that isn't a double digit as Abyss01, Abyss02, etc. It still works that way, and I doubt I'LL ever have up to 99 Abysses in a level, so I can safely hard code lines like "str_clip(AbyssNumberString, 5);" knowing that my Abyss region names are always 5 letters followed by 2 digits.

Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1