Turn an integer into variable?
#1

Hi,
I'm trying to use an "if" statement with "data" in this code: ( From HTTP in wiki )
PHP код:
forward MyHttpResponse(indexresponse_codedata[]);
 
public 
OnPlayerCommandText(playeridcmdtext[])
{
    if(!
strcmp("/hello",cmdtext,true))
    {
        
HTTP(playeridHTTP_GET"kc.gd/hello.txt""""MyHttpResponse");
        return 
1;
    }
    return 
0;
}
 
public 
MyHttpResponse(indexresponse_codedata[])
{
    
// In this callback "index" would normally be called "playerid" ( if you didn't get it already :) )
    
new
        
buffer128 ];
    if(
response_code == 200//Did the request succeed?
    
{
        
//Yes!
        
format(buffersizeof(buffer), "The URL replied: %s"data);
        
SendClientMessage(index0xFFFFFFFFbuffer);
    }
    else
    {
        
//No!
        
format(buffersizeof(buffer), "The request failed! The response code was: %d"response_code);
        
SendClientMessage(index0xFFFFFFFFbuffer);
    }

and it returns:
Код:
error 033: array must be indexed (variable "databody")
example of usage:
PHP код:
if(databody == -1){ 
thanks.
Reply
#2

"databody" is an array, so you have to index it, like so:

pawn Код:
if(databody[0] == -1){
If "databody" is meant to be a string, you can just do this to check whether it's empty or not:

pawn Код:
if ( ! strlen ( databody ) ) {
// code here to be called if databody is empty
}
also i'm pretty sure an integer IS a variable lol
Reply
#3

If your API always returns an integer then you can simply use strval(). Otherwise you need to use strcmp().
Reply
#4

Databody hasn't just -1, it has 0 and 1 + between 0 and 1 ( decimal ) ...
Reply
#5

Solved using strval(), thanks both.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)