SA-MP Forums Archive
Turn an integer into variable? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Turn an integer into variable? (/showthread.php?tid=633624)



Turn an integer into variable? - YouHack - 04.05.2017

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.


Re: Turn an integer into variable? - Dignity - 04.05.2017

"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


Re: Turn an integer into variable? - Vince - 04.05.2017

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


Re: Turn an integer into variable? - YouHack - 04.05.2017

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


Re: Turn an integer into variable? - YouHack - 04.05.2017

Solved using strval(), thanks both.