Matheus Posted April 27, 2012 Posted April 27, 2012 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. 3
Lighta Posted April 27, 2012 Posted April 27, 2012 (edited) 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 April 27, 2012 by Lighta
KeyWorld Posted April 27, 2012 Posted April 27, 2012 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... 2
Lemongrass Posted April 27, 2012 Posted April 27, 2012 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 1
Hiero Posted April 27, 2012 Posted April 27, 2012 Very useful suggestion, I hope that your request will be accepted. I want it~
Panallox Posted April 27, 2012 Posted April 27, 2012 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. 1
Matheus Posted May 5, 2012 Author Posted May 5, 2012 (edited) 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 May 5, 2012 by Matheus
Recommended Posts