Edit enum with string (+REP)
#1

Код:
CMD:set(playerid,params[]){
if(sscanf(params,"usi",param[0],String,param[1])) return MSG(playerid,C_RED,"/Set [ID] [Database] [Amount]");
DB[param[0]][String] = param[1];
return 1;}
It's obviously giving me an error because of the [string] since it's not defined in the enum etc but there's a way to do it? instead of creating /setmoney, /setlevel, /setbalance etc etc etc....
Reply
#2

I would recommend to use string as the last parameter of your command. Also, create new array 'String' which is supposed to be your sscanf parameter and compare it to string from enum.
Reply
#3

Quote:
Originally Posted by cdoubleoper
Посмотреть сообщение
I would recommend to use string as the last parameter of your command. Also, create new array 'String' which is supposed to be your sscanf parameter and compare it to string from enum.
ye sure bro but it's still giving me an error
Reply
#4

Quote:
Originally Posted by Lirbo
Посмотреть сообщение
ye sure bro but it's still giving me an error
Show your code with enum that you want to use.
Reply
#5

Quote:
Originally Posted by cdoubleoper
Посмотреть сообщение
Show your code with enum that you want to use.
just a random enum to edit doesnt matter what
Reply
#6

you can't replace a constant enum variable with a randomly created variable,

and this is not the proper way to assign a value to a string variable
firstly, i guess you want to create a string variable in the command, so simply:
pawn Код:
new String[128]; // <- let's assume that the sizeof the string is 128.
secondly, when using string sscanf prefix which is 's', you have to assign the size of it beside it between two brackets, so the sscanf code will be:
pawn Код:
if(sscanf(params,"us[128]i",param[0],String,param[1])) return MSG(playerid,C_RED,"/Set [ID] [Database] [Amount]"); // we added the '[128]' beside the string prefix 's' as you can see, which is the length of 'String' variable.
and then, add to DB's enum the string variable, well, we are not gonna make it's title as same as the string defined in the command which is "String" to avoid warnings, so let's put it as "str", add this to your DB's enum:
pawn Код:
str[128],
and finally, the value assigning, you got a variable for strings in the enum, and you got an independent string variable in your command, we can now assign the value:
pawn Код:
format(DB[params[0]][str], 128, String);
simply, format function is used to assign a string value to a string variable, '=' operator is not the proper way to do it.
- it would be better to create variable for each parameter, i.e, instead of that params[0] and params[1], just add 'id' variable, and a variable for amount, let's call it 'a', and the changes done are:
pawn Код:
new id, a;
if(sscanf(params,"us[128]i",id,String,a)) return MSG(playerid,C_RED,"/Set [ID] [Database] [Amount]");
format(DB[id][str], 128, String);
Reply
#7

This is happening because you are miss-understanding what enum is, You can think of enum as constant variables that will be replaced by numbers at compile time, so a Money in enum is not a string, but rather a number (for example 6th element in an array), so "Money" as string is not equal to Money of an enum, what you can do is however making a nested if-else
PHP код:
if(!strcmp(string,"Money"))
{
//Set his money bla bla bla

UPDATE: I didn't actually know you can use a switch in this case, went ahead and tested, turns out you can't use it, so strcmp is your only way to do it I guess.
Reply
#8

Quote:
Originally Posted by Sawalha
Посмотреть сообщение
you can't replace a constant enum variable with a randomly created variable,

and this is not the proper way to assign a value to a string variable
firstly, i guess you want to create a string variable in the command, so simply:
pawn Код:
new String[128]; // <- let's assume that the sizeof the string is 128.
secondly, when using string sscanf prefix which is 's', you have to assign the size of it beside it between two brackets, so the sscanf code will be:
pawn Код:
if(sscanf(params,"us[128]i",param[0],String,param[1])) return MSG(playerid,C_RED,"/Set [ID] [Database] [Amount]"); // we added the '[128]' beside the string prefix 's' as you can see, which is the length of 'String' variable.
and then, add to DB's enum the string variable, well, we are not gonna make it's title as same as the string defined in the command which is "String" to avoid warnings, so let's put it as "str", add this to your DB's enum:
pawn Код:
str[128],
and finally, the value assigning, you got a variable for strings in the enum, and you got an independent string variable in your command, we can now assign the value:
pawn Код:
format(DB[params[0]][str], 128, String);
simply, format function is used to assign a string value to a string variable, '=' operator is not the proper way to do it.
- it would be better to create variable for each parameter, i.e, instead of that params[0] and params[1], just add 'id' variable, and a variable for amount, let's call it 'a', and the changes done are:
pawn Код:
new id, a;
if(sscanf(params,"us[128]i",id,String,a)) return MSG(playerid,C_RED,"/Set [ID] [Database] [Amount]");
format(DB[id][str], 128, String);
error 033: array must be indexed (variable "str")


Quote:
Originally Posted by PrO.GameR
Посмотреть сообщение
This is happening because you are miss-understanding what enum is, You can think of enum as constant variables that will be replaced by numbers at compile time, so a Money in enum is not a string, but rather a number (for example 6th element in an array), so "Money" as string is not equal to Money of an enum, what you can do is however making a switch
PHP код:
switch(string)
{
case 
"Money":
{
//Set his money bla bla bla
}

C:\Users\мйшп\Desktop\All\GRRP\gamemodes\GlobalDM. pwn(1475) : error 033: array must be indexed (variable "-unknown-")
C:\Users\мйшп\Desktop\All\GRRP\gamemodes\GlobalDM. pwn(1477) : error 001: expected token: "-string end-", but found "-identifier-"
Reply
#9

where do you get the first error "error 033: array must be indexed (variable "str") "? copy the line and paste it here
Reply
#10

Quote:
Originally Posted by Sawalha
Посмотреть сообщение
where do you get the first error "error 033: array must be indexed (variable "str") "? copy the line and paste it here
Код:
CMD:setint(playerid,params[]){
new id, a, str[128];
if(sscanf(params,"us[128]i",id,String,a)) return MSG(playerid,C_RED,"/Set [ID] [Database] [Amount]");
format(DB[id][str], 128, String);
return 1;}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)