Raised This Month: $ Target: $400
 0% 

String to int


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Xenogenetics
Member
Join Date: Jul 2007
Old 07-17-2007 , 12:46   String to int
Reply With Quote #1

How would I take a number that user typed with the say command and make it a number that the plugin can use.
Xenogenetics is offline
teame06
i have a hat
Join Date: Feb 2005
Location: Hat City
Old 07-17-2007 , 12:49   Re: String to int
Reply With Quote #2

StringToInt

Look at string.inc for other string natives.
__________________
No private support via Instant Message
GunGame:SM Released
teame06 is offline
Send a message via AIM to teame06
Xenogenetics
Member
Join Date: Jul 2007
Old 07-17-2007 , 13:22   Re: String to int
Reply With Quote #3

So in Source each word is its own string? So if I used this plugin :http://wiki.alliedmods.net/Commands_...oking_Commands I would add 1 to startidx each time I wanted the next word?

Basically, this isn't working:
Code:
if (StrEqual(text[startidx], "!color")){
  new rcol = StringToInt(text[startidx + 1])
  new gcol = StringToInt(text[startidx + 2])
  new bcol = StringToInt(text[startidx + 3])
  set_rendering(client, 0,rcol,gcol,bcol, Glow, 255)
}
I think it might have to do with each part having quotes around it but I don't know.

Last edited by Xenogenetics; 07-17-2007 at 13:30.
Xenogenetics is offline
ferret
SourceMod Developer
Join Date: Dec 2004
Location: Atlanta, GA
Old 07-17-2007 , 13:30   Re: String to int
Reply With Quote #4

No. Startidx is just moving forward in the string by one character.

You need BreakString or ExplodeString to split the string into "words"
__________________
I'm a blast from the past!
ferret is offline
Xenogenetics
Member
Join Date: Jul 2007
Old 07-17-2007 , 14:27   Re: String to int
Reply With Quote #5

I thought that in source strings came in with a number of arguments equal to the number of words, wouldn't I just have to switch to them?
Xenogenetics is offline
ferret
SourceMod Developer
Join Date: Dec 2004
Location: Atlanta, GA
Old 07-17-2007 , 14:31   Re: String to int
Reply With Quote #6

The say command only has one argument. It's a big string with many words, surrounded by "s.

GetCmdArg(1, buffer, sizeof(buffer)); retrieves that string.

Now, after you remove the end ", and set startidx, you want to find the first word?

Code:
new len, String:wordone[64];
len = BreakString(buffer[startidx], wordone, sizeof(wordone));
 
new String:wordtwo[64];
len = BreakString(buffer[len+startidx], wordtwo, sizeof(wordtwo));
Each call to BreakString returns the first "argument". An argument is determined by the first space found, or the first space after ""s. Here are the arguments from this example: "Test test" red blue "green blue"

"Test Test"
red
blue
"green blue"

When BreakString returns -1 instead of a length, it means there's no more arguments in the source string.
__________________
I'm a blast from the past!
ferret is offline
Xenogenetics
Member
Join Date: Jul 2007
Old 07-17-2007 , 17:01   Re: String to int
Reply With Quote #7

I get errors when I try to use that, and are you saying the wiki is wrong?

This is what I have:
Code:
new lenzero, String:wordzero[64];
 lenzero = BreakString(text[startidx], wordzero, sizeof(wordzero));
 if(StrContains(wordzero,"!color")){
 new lenone, String:wordone[64];
 lenone = BreakString(text[lenzero + startidx], wordone, sizeof(wordone));
 
 new lentwo, String:wordtwo[64];
 lentwo = BreakString(text[lenone+startidx], wordtwo, sizeof(wordtwo));
 
 new lenthree, String:wordthree[64];
 lenthree = BreakString(text[lentwo+startidx], wordthree, sizeof(wordthree)); 
  new rcol = StringToInt(wordone)
  new gcol = StringToInt(wordtwo)
  new bcol = StringToInt(wordthree)
  set_rendering(client, 0,rcol,gcol,bcol, Glow, 255)
 }
The player just turns black and it messes up all the other commands.

Last edited by Xenogenetics; 07-17-2007 at 17:36.
Xenogenetics is offline
ferret
SourceMod Developer
Join Date: Dec 2004
Location: Atlanta, GA
Old 07-17-2007 , 17:11   Re: String to int
Reply With Quote #8

The wiki isn't wrong. I don't understand what you mean by "words".
__________________
I'm a blast from the past!
ferret is offline
Xenogenetics
Member
Join Date: Jul 2007
Old 07-17-2007 , 17:40   Re: String to int
Reply With Quote #9

ok, the player says !color 255 0 0 and he should turn red.
The plugin sees !color and takes the three other parts of the string and converts them into ints which are then used to render the player
Xenogenetics is offline
sumguy14
Senior Member
Join Date: Apr 2006
Old 07-17-2007 , 18:15   Re: String to int
Reply With Quote #10

Here is what I do in my raffle plugin
Stocks written by PimpinJuice (This is my first plugin)

Make sure to hook say and say_team in load and make it call the name of the first function in this example

Code:
public Action:SayCommand(index,argc)
{
  if(argc>0)
  {
    // Declare strings
    decl String:args[192],String:litstring[192],String:command[8];
    GetCmdArgString(args,192);
    GetLiteralString(args,litstring,192); // Takes away the quotes within the string, for example "say "!color 255 3 5"" becomes "say !color 255 3 5"
    StrToken(litstring,1,command,7); // Find first word in the string, which should be "!color" for this plugin
    if(StrEqual(command,"!color"))
    {
      if(index==0)
        PrintToServer("You can't set your own color!"); // Block console from using
      else
      {
        decl String:red[4],etc...;
        StrToken(litstring,2,red,4);
        StrToken(litstring,3,green,4);
        StrToken(litstring,4,blue,4);
        Convert to ints and use..;
      }
    }
  }
  return Plugin_Continue;
}

stock StrToken(const String:inputstr[],tokennum,String:outputstr[],maxlen)
{
  new String:buf[256]="";
  new cur_idx;
  new idx;
  new curind;
  idx=StrBreak(inputstr,buf,maxlen);
  if(tokennum==1)
  {
    strcopy(outputstr,maxlen,buf);
    return;
  }
  curind=1;
  while (idx!=-1)
  {
    cur_idx+=idx;
    idx=StrBreak(inputstr[cur_idx],buf,maxlen);
    curind++;
    if(tokennum==curind)
    {
      strcopy(outputstr,maxlen,buf);
      break;
    }
  }
}
 
stock GetLiteralString(const String:cmd[],String:buffer[],maxlength)
{
  StrCopy(buffer,strlen(cmd)+1,cmd);
  ReplaceString(buffer,maxlength,"\"","");
}

Last edited by API; 07-21-2007 at 12:14.
sumguy14 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:51.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode