Jump to content
  • 0

understanding the function of atoi


ToiletMaster

Question


  • Group:  Members
  • Topic Count:  76
  • Topics Per Day:  0.02
  • Content Count:  276
  • Reputation:   7
  • Joined:  08/11/12
  • Last Seen:  

Hi guys,

i'm trying to understand the use of atoi in script commands but for some reason i can't seem to understand.

*atoi("<string>")

These commands are used to convert strings to numbers. 'atoi' will interpret
given string as a decimal number (base 10), while 'axtoi' interprets strings as
hexadecimal numbers (base 16). 'strtol' lets the user specify a base (valid range
is between 2 and 36 inclusive, or the special value0, which means auto-detection).

The 'atoi' and 'strtol' functions conform to the C functions with the same names,
and 'axtoi' is the same as strtol, with a base of 16. Results are clamped to signed
32 bit int range (INT_MIN ~ INT_MAX).

Examples:

	.@var = atoi("11");        // Sets .@var to 11

 

Based on script commands i noticed the example given is setting the

.@var = atoi("11")

what's the difference if i were to directly put set .@var = 11? aside from it being string and integer.

 

On the Horror Toy Factory and other scripts, i noticed they used this alot but i can't seem to understand that portion at all, would be great if someone could explain on how it exactly functions. Thank you in advance! Here's one part of the script in Horror Toy Factory that used Atoi

OnStart:
	.@event$ = instance_npcname( strnpcinfo(0) ) + "::OnMyMobDead";
	killmonster 'xm_d_map$, .@event$;
	getunitdata 'celene_id, .@data;
	setarray .@coord[0], .@data[UMOB_X], .@data[UMOB_Y];
	.@num = atoi(strnpcinfo(2));
	.@index = ( .@num > 2 ? 1 : 0 );// x or y
	.@signe = pow(-1,.@num+1);
	while(1) {
		.@coord[.@index] = .@coord[.@index] + (2 * .@signe);
		.@coord[!.@index] = .@coord[!.@index] + rand(0,2) - 1;
		monster 'xm_d_map$,.@coord[0], .@coord[1], "#f_w_1",3038,1, .@event$;
		if (.@coord[0] < 211 || .@coord[0] > 241 || .@coord[1] < 166 || .@coord[1] > 201)
			break;
		sleep 200;
	}
	sleep 6000;
	killmonster 'xm_d_map$, .@event$;
	end;

 

Edit:

 

Added another portion of the script of Horror Toy Factory that uses it that i can't exactly understand on how the switch cases relate to Atoi

1@xm_d,210,141,3	script	Packaged Present#1	4_TREASURE_BOX,{
	specialeffect EF_COIN;
	disablenpc instance_npcname( strnpcinfo(0) );
	initnpctimer;
	end;

OnTimer1000:
	switch( atoi(strnpcinfo(2)) ) {
	case 1:
		.@num = rand(4,8);
		for ( .@i = 0; .@i < .@num; .@i++ )
			makeitem 7642,1, 'xm_d_map$, rand(208,212), rand(139,143);// Bloody_Coin
		if (rand(1,1000) > 150)
			makeitem 644,1, 'xm_d_map$,209,141;// Gift_Box;
		if (rand(1,1000) > 600)
			makeitem 617,1, 'xm_d_map$,210,141;// Old_Violet_Box;
		if (rand(1,1000) > 900)
			makeitem 22534,1, 'xm_d_map$,211,141;// Closedmind_Box
		break;
	case 2:
		.@num = rand(3,7);
		for ( .@i = 0; .@i < .@num; .@i++ )
			makeitem 7642,1, 'xm_d_map$, rand(212,216), rand(139,143);// Bloody_Coin
		if (rand(1,1000) > 400)
			makeitem 603,1, 'xm_d_map$,213,141;// Old_Blue_Box
		if (rand(1,1000) > 700)
			makeitem 616,1, 'xm_d_map$,214,141;// Old_Card_Album
		if (rand(1,1000) > 950)
			makeitem 13442,1, 'xm_d_map$,215,141;// Old_Parasol
		break;
	case 3:
		.@num = rand(2,6);
		for ( .@i = 0; .@i < .@num; .@i++ )
			makeitem 7642,1, 'xm_d_map$, rand(216,220), rand(139,143);// Bloody_Coin
		if (rand(1,1000) > 850)
			makeitem 7229,1, 'xm_d_map$,217,141;// Silver_Bullion
		if (rand(1,1000) > 800)
			makeitem 12246,1, 'xm_d_map$,218,141;// Magic_Card_Album
		if (rand(1,1000) > 950)
			makeitem 2486,1, 'xm_d_map$,219,141;// Shadow_Walk_
		break;
	case 4:
		.@num = rand(4,8);
		for ( .@i = 0; .@i < .@num; .@i++ )
			makeitem 7642,1, 'xm_d_map$, rand(220,224), rand(139,143);// Bloody_Coin
		if (rand(1,1000) > 700)
			makeitem 7228,1, 'xm_d_map$,221,141;// Gold_Bullion
		if (rand(1,1000) > 600)
			makeitem 617,1, 'xm_d_map$,222,141;// Old_Violet_Box
		if (rand(1,1000) > 900)
			makeitem 22534,1, 'xm_d_map$,223,141;// Closedmind_Box
		break;
	case 5:
		.@num = rand(3,7);
		for ( .@i = 0; .@i < .@num; .@i++ )
			makeitem 7642,1, 'xm_d_map$, rand(224,228), rand(139,143);// Bloody_Coin
		if (rand(1,1000) > 150)
			makeitem 644,1, 'xm_d_map$,225,141;// Gift_Box
		if (rand(1,1000) > 700)
			makeitem 616,1, 'xm_d_map$,226,141;// Old_Card_Album
		if (rand(1,1000) > 950)
			makeitem 2976,1, 'xm_d_map$,227,141;// Red_Lantern
		break;
	case 6:
		.@num = rand(2,6);
		for ( .@i = 0; .@i < .@num; .@i++ )
			makeitem 2976,1, 'xm_d_map$, rand(208,212), rand(134,138);// Red_Lantern
		if (rand(1,1000) > 400)
			makeitem 603,1, 'xm_d_map$,209,136;// Old_Blue_Box
		if (rand(1,1000) > 800)
			makeitem 12246,1, 'xm_d_map$,210,136;// Magic_Card_Album
		if (rand(1,1000) > 950)
			makeitem 2977,1, 'xm_d_map$,211,136;// Hurt_Mind
		break;
	case 7:
		.@num = rand(4,8);
		for ( .@i = 0; .@i < .@num; .@i++ )
			makeitem 7642,1, 'xm_d_map$, rand(212,216), rand(134,138);// Bloody_Coin
		if (rand(1,1000) > 850)
			makeitem 7229,1, 'xm_d_map$,213,136;// Silver_Bullion
		if (rand(1,1000) > 600)
			makeitem 617,1, 'xm_d_map$,214,136;// Old_Violet_Box
		if (rand(1,1000) > 900)
			makeitem 22534,1, 'xm_d_map$,215,136;// Closedmind_Box
		break;
	case 8:
		.@num = rand(3,7);
		for ( .@i = 0; .@i < .@num; .@i++ )
			makeitem 7642,1, 'xm_d_map$, rand(216,220), rand(134,138);// Bloody_Coin
		if (rand(1,1000) > 700)
			makeitem 7228,1, 'xm_d_map$,217,136;// Gold_Bullion
		if (rand(1,1000) > 700)
			makeitem 616,1, 'xm_d_map$,218,136;// Old_Card_Album
		if (rand(1,1000) > 950)
			makeitem 2978,1, 'xm_d_map$,219,136;// KindHeart
		break;
	case 9:
		.@num = rand(2,6);
		for ( .@i = 0; .@i < .@num; .@i++ )
			makeitem 7642,1, 'xm_d_map$, rand(220,224), rand(134,138);// Bloody_Coin
		if (rand(1,1000) > 150)
			makeitem 644,1, 'xm_d_map$,221,136;// Gift_Box
		if (rand(1,1000) > 800)
			makeitem 12246,1, 'xm_d_map$,222,136;// Magic_Card_Album
		if (rand(1,1000) > 950)
			makeitem 18848,1, 'xm_d_map$,223,136;// Lush_Rose
		break;
	case 10:
		.@num = rand(4,8);
		for ( .@i = 0; .@i < .@num; .@i++ )
			makeitem 7642,1, 'xm_d_map$, rand(224,228), rand(134,138);// Bloody_Coin
		if (rand(1,1000) > 400)
			makeitem 603,1, 'xm_d_map$,225,136;// Old_Blue_Box
		if (rand(1,1000) > 600)
			makeitem 617,1, 'xm_d_map$,226,136;// Old_Violet_Box
		if (rand(1,1000) > 900)
			makeitem 22534,1, 'xm_d_map$,227,136;// Closedmind_Box
		break;
	}

 

Edited by ToiletMaster
Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

  • Group:  Forum Manager
  • Topic Count:  282
  • Topics Per Day:  0.06
  • Content Count:  3127
  • Reputation:   1617
  • Joined:  03/26/12
  • Last Seen:  

Simply put, the value of strnpcinfo(2) is a string formed by the name of the NPC itself. "NPC Name#45" would give "45" as a string, so you'd use "atoi" to change the string to an integer.

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  39
  • Topics Per Day:  0.01
  • Content Count:  615
  • Reputation:   201
  • Joined:  11/09/11
  • Last Seen:  

right another reason might be, it's easier to store everything as a string because it's always readable, and then only return items as INT if you need to use them as INT... for instance...

 

// Set Character Name / Level
setarray(.@var$[0], "Z3R0", "100"); 

set(.@var$[1], ( atoi(.@var$[1]) + 100 ) + ""); 
// Here we have taken the numeric value of "100" as 100, added 100, and then converted it back
// to string... this keeps all our variables on 1 array, but let's us use them as either STRING or INTEGER :D

 

Edited by Z3R0
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  76
  • Topics Per Day:  0.02
  • Content Count:  276
  • Reputation:   7
  • Joined:  08/11/12
  • Last Seen:  

On 14/08/2017 at 3:10 AM, Akkarin said:

Simply put, the value of strnpcinfo(2) is a string formed by the name of the NPC itself. "NPC Name#45" would give "45" as a string, so you'd use "atoi" to change the string to an integer.

First and foremost, would like to apologize for the late reply, got caught up yesterday and couldn't respond back to the topics. Thank you for sharing that, i understand that atoi changes from string to integer, but mainly on the uses of it as i can't seem to imagine on how it would be useful.

13 hours ago, Z3R0 said:

right another reason might be, it's easier to store everything as a string because it's always readable, and then only return items as INT if you need to use them as INT... for instance...

 


// Set Character Name / Level
setarray(.@var$[0], "Z3R0", "100"); 

set(.@var$[1], ( atoi(.@var$[1]) + 100 ) + ""); 
// Here we have taken the numeric value of "100" as 100, added 100, and then converted it back
// to string... this keeps all our variables on 1 array, but let's us use them as either STRING or INTEGER :D

 

Hi @Z3R0,

 

Thanks for the example! This makes it easier for me to understand, i wasn't aware that this was possible.

 

Off Topic,

 

does your scripting for dummies cover this portion? i saw a little bit earlier on but it was mainly on the basics of the scripting, would love to see if you'd cover a little bit more on this topic if you have the time! Kudos

Link to comment
Share on other sites

  • 0

  • Group:  Forum Manager
  • Topic Count:  282
  • Topics Per Day:  0.06
  • Content Count:  3127
  • Reputation:   1617
  • Joined:  03/26/12
  • Last Seen:  

1 hour ago, ToiletMaster said:

i understand that atoi changes from string to integer, but mainly on the uses of it as i can't seem to imagine on how it would be useful.

A good example is in the instance scripts, where specific NPCs do things dependant on their hidden name value. This is very useful when using the duplicate NPC function. You provided an example in your first post :)

  • MVP 1
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  76
  • Topics Per Day:  0.02
  • Content Count:  276
  • Reputation:   7
  • Joined:  08/11/12
  • Last Seen:  

12 minutes ago, Akkarin said:

A good example is in the instance scripts, where specific NPCs do things dependant on their hidden name value. This is very useful when using the duplicate NPC function. You provided an example in your first post :)

You've just blew my mind! I completely forgotten about that portion. 

 

Now I see exactly how it works out! Thank you so much for making me understand! I fully understand on how it works now :D

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  22
  • Topics Per Day:  0.01
  • Content Count:  135
  • Reputation:   4
  • Joined:  07/29/17
  • Last Seen:  

On 8/15/2017 at 11:39 AM, ToiletMaster said:

First and foremost, would like to apologize for the late reply, got caught up yesterday and couldn't respond back to the topics. Thank you for sharing that, i understand that atoi changes from string to integer, but mainly on the uses of it as i can't seem to imagine on how it would be useful.

Hi @Z3R0,

 

Thanks for the example! This makes it easier for me to understand, i wasn't aware that this was possible.

 

Off Topic,

 

does your scripting for dummies cover this portion? i saw a little bit earlier on but it was mainly on the basics of the scripting, would love to see if you'd cover a little bit more on this topic if you have the time! Kudos

Hi,

Converting string to int is very important especially in data manipulation on database. Example is when the specific table only receive integer data, when you input string sometime it gives you warning. Though now a days it is not the issue anymore the new version of mysql can now detect the string and convert it on integer as long as it is not a character letter.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...