Returning strings in a public
#1

Has anyone done it?
Reply
#2

I asked the same question a few days ago.
Mine was about using CallRemoteFunction to get a vehiclename returned from another script.
Strings aren't returned by public functions, you'll get an error if you try.
Reply
#3

I did and got no errors.

Here's how i've done it (gotta say i kinda borrowed the idea while reading one of ******'s posts)

Код:
public Anypublic(playerid)
{
new string[24];
GetPlayerName(playerid, string, 24);
return string, 1;
}
Is it only for me the code's working?
Reply
#4

Use GetPVarString and SetPVarString for communication between scripts
Reply
#5

or something like this...?
pawn Код:
// Some FS
new
    somestr[8] = "abcd123";
forward GetSomeStr(idx);
public GetSomeStr(idx)
{
     return somestr[idx];
}
// GM
new
    str[8];
for (new idx = 0; idx != 8; ++ idx)
{
    str[idx] = CallRemoteFunction("GetSomeStr", "i", idx);
    if (str[idx] == '\0') break;
}
printf("%s", str);
Reply
#6

Quote:
Originally Posted by LeLeTe
Посмотреть сообщение
I did and got no errors.

Here's how i've done it (gotta say i kinda borrowed the idea while reading one of ******'s posts)

Код:
public Anypublic(playerid)
{
new string[24];
GetPlayerName(playerid, string, 24);
return string, 1;
}
Is it only for me the code's working?
Have you made a print what you're actually returning?
I've seen a tutorial a few days ago and a line with multiple things after return would return only the last value.
So I think you're only returning the value "1".


Quote:
Originally Posted by Djole1337
Посмотреть сообщение
or something like this...?
pawn Код:
// Some FS
new
    somestr[8] = "abcd123";
forward GetSomeStr(idx);
public GetSomeStr(idx)
{
     return somestr[idx];
}
// GM
new
    str[8];
for (new idx = 0; idx != 8; ++ idx)
{
    str[idx] = CallRemoteFunction("GetSomeStr", "i", idx);
    if (str[idx] == '\0') break;
}
printf("%s", str);
I understand this code, but it seems you're using alot of CallRemoteFunction for one string, especially when it's large (say 50 characters).
But it would work.

I used another approach for this:
http://forum.sa-mp.com/showpost.php?...39&postcount=5

Script A needs a string stored in script B;
Script A therefore uses CallRemoteFunction to ask script B to send the string back.
Script B uses another CallRemoteFunction to send the string in one go back to script A, which stores it in a special place.
Then script A is able to use the entire string at once.
It doesn't have to grab character per character from script B and reconstruct it again.
I don't know which one is faster.

My approach works perfectly as well but to avoid too many CallRemoteFunctions, I'm merging all my filterscripts into one big script.
Then only CallRemoteFunction must be used between this big filterscript and the gamemode.
Reply
#7

You cannot return string from public functions. If you want communication between scripts with strings, use properties.
Reply
#8

Due to the architecture of the public functions, it's only possible to return one-cell values (integers, characters, float values) since they were specifically designed to do so. You'll have to stick to using properties, for now.

I was actually writing a script that modifies the return address of a function to return a string, but it only returned the first character, which made me believe that you can't return strings with them at all, sorry to say (perhaps, I can work something out).

Quote:
Originally Posted by LeLeTe
Посмотреть сообщение
I did and got no errors.

Here's how i've done it (gotta say i kinda borrowed the idea while reading one of ******'s posts)

Код:
public Anypublic(playerid)
{
new string[24];
GetPlayerName(playerid, string, 24);
return string, 1;
}
Is it only for me the code's working?
That won't work. You can't return multiple values in PAWN. That will just return "1".

Quote:
Originally Posted by Djole1337
Посмотреть сообщение
or something like this...?
pawn Код:
// Some FS
new
    somestr[8] = "abcd123";
forward GetSomeStr(idx);
public GetSomeStr(idx)
{
     return somestr[idx];
}
// GM
new
    str[8];
for (new idx = 0; idx != 8; ++ idx)
{
    str[idx] = CallRemoteFunction("GetSomeStr", "i", idx);
    if (str[idx] == '\0') break;
}
printf("%s", str);
That will keep calling the function until the string has reached it's end. It's horribly inefficient, especially if you want to return strings with 100+ characters.
Reply
#9

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
That will keep calling the function until the string has reached it's end. It's horribly inefficient, especially if you want to return strings with 100+ characters.
How come that it's "horribly" inefficient?
Reply
#10

Quote:
Originally Posted by Djole1337
Посмотреть сообщение
How come that it's "horribly" inefficient?
Technically, the longer the string, the more function calls. It's a great method, but personally I think that calling a remote function several times isn't ideal.

Edit: Actually, your code does infact give me an idea for something!

pawn Код:
stock GetRemoteString(function[], dest[], size)
{
    for (new i = 0; (dest[i] = CallRemoteFunction(function, "d", i)) && i < size; i ++) {}
    return 1;
}
^ There's more to it, though. That's just a function. ^
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)