Jump to content

Switch ("string")


Matheus

Recommended Posts


  • Group:  Members
  • Topic Count:  8
  • Topics Per Day:  0.00
  • Content Count:  63
  • Reputation:   6
  • Joined:  11/15/11
  • Last Seen:  

Hi, it's me again (haha), i think it is be nice if you guys (core developers), it's make switch accept words and numbers (now, just accept numbers ..), for example:

switch(getarg(0)){

case 'mes':
mes getarg(1);
break;

case 'getitem':
getitem getarg(1), 1;
break;

}

and again: sorry for my bad english.

  • Upvote 3
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  16
  • Topics Per Day:  0.00
  • Content Count:  737
  • Reputation:   216
  • Joined:  11/29/11
  • Last Seen:  

switch only accept integer or constant value in C, Java...

so you'll need to insert some lookup to do the job, in the end I doubt your switch will be more effecient then multiple else if. (Well probably still be for long swtich)

Anyway we could try treate it like that :

#include <string.h>
#include <assert.h>
#include <stdint.h>


#define p_ntohl(u) ({const uint32_t Q=0xFF000000;       
                    uint32_t S=(uint32_t)(u);           
                  (*(uint8_t*)&Q)?S:                    
                  ( (S<<24)|                            
                    ((S<<8)&0x00FF0000)|                
                    ((S>>8)&0x0000FF00)|                
                    ((S>>24)&0xFF) );  })

main (void)
{
   uint32_t s[0x40]; 
   assert((unsigned char)1 == (unsigned char)(257));
   memset(s, 0, sizeof(s));
   fgets((char*)s, sizeof(s), stdin);

   switch (p_ntohl(s[0])) {
       case 'open':
       case 'read':
       case 'seek':
           puts("ok");
           break;
       case 'rmn0':
           puts("not authorized");
           break;
       default:
           puts("unrecognized command");  
   }
   return 0;
}

Edited by Lighta
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.00
  • Content Count:  379
  • Reputation:   304
  • Joined:  11/10/11
  • Last Seen:  

Yeah it's possible since switch is hardcode with goto.

First way:

Change $@__SW%x_VAL, to be $@__SW%x_VAL$ (and all stuff related to decimal value (strtol(), etc. ) to convert directly int to string and don't have problems.

Second way:

Create a new variable ($@__SW%x_TYPE ?) that will store if the data is a int (value 0) or string (value 1), create a new var $@__SW%x_VAL$ and will do the right tests in the 'case' stuff based on $@__SW%x_TYPE to know if using $@__SW%x_VAL$ or $@__SW%x_VAL.

@devs It should be good to use .@vartype to store this king of private data...

  • Upvote 2
Link to comment
Share on other sites


  • Group:  Developer
  • Topic Count:  28
  • Topics Per Day:  0.01
  • Content Count:  547
  • Reputation:   270
  • Joined:  11/08/11
  • Last Seen:  

switch only accept integer or constant value in C, Java...

just to be correct, you can switch with strings since the latest java release aka jdk 1.7^^

i would like to see string switches in here, but first there should be a working direct variable assignment like introduced in revision 15982(havent tested it though) and things like var++ and ++var for the loops etc. but as i saw in the diff of that revision they seem to intend to implement those too :)

  • Upvote 1
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  1
  • Topics Per Day:  0.00
  • Content Count:  10
  • Reputation:   7
  • Joined:  12/06/11
  • Last Seen:  

Very useful suggestion, I hope that your request will be accepted. :)

I want it~

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  12
  • Topics Per Day:  0.00
  • Content Count:  117
  • Reputation:   167
  • Joined:  11/10/11
  • Last Seen:  

I don't think this is a feature rAthena really needs. It'd be nice, sure, but it's not something that affects every day scripting. You can just as easily convert strings into integers, and then process them accordingly:

set @type, ( getarg(0) == "mes" ? 1 : (getarg(0) == "next" ? 2 : 0) );

Besides, this would mean converting the current labelling system and would require much more fiddling around with the source. This might decrease backwards compatibility, or may affect the behaviour of the scripting engine. So, for the most part, I'd have to say I reject this proposal.

  • Upvote 1
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  8
  • Topics Per Day:  0.00
  • Content Count:  63
  • Reputation:   6
  • Joined:  11/15/11
  • Last Seen:  

Here one example Epoque, with multiple configs, i don't know if this is right, but is just a example:

function script configSystem {
explode(.@_config, getarg(0), ",");
switch(.@_config[0])
{
 case "useZeny":
  $zenyCost = .@_config[1];
  break;

 case "itemID":
  $itemID = .@_config[1];
  break;

 case "itemAMOUNT":
  $itemAMOUNT = .@_config[1];
  break;
}
}
- testScript -1,{
OnPCLoginEvent:
 configSystem("useZeny,5000");
 configSystem("itemID,678");
 configSystem("itemAMOUNT,678");

if(($zenyCost) && zeny < $zenyCost){
 mes "Zeny is necessary !";
 close;
}
else
{
 getitem $itemID, $itemAMOUNT;
 mes "Here is your item!";
 close;
}

}

Edited by Matheus
Link to comment
Share on other sites

×
×
  • Create New...