Raised This Month: $51 Target: $400
 12% 

Why Can't I return a String?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SamuraiBarbi
Senior Member
Join Date: Aug 2006
Location: United States
Old 05-21-2009 , 11:55   Why Can't I return a String?
Reply With Quote #1

Does anyone have any idea why I can't return this string value? This is something I wrote real quick just to make sure I could return string values, but when I compile it says tag mismatch on line 43.

Code:
#pragma semicolon 1



#include <sourcemod>
#include <sdktools>
#include <sdktools_trace>
#include <clients>
#include <hacks>


    public OnPluginStart()
    {
        HookEvent("weapon_fire", Event_WeaponFire, EventHookMode_Pre);

    }

    public Action:Event_WeaponFire(Handle:event, const String:name[], bool:dontBroadcast)
    {
        new target_playerID = GetEventInt(event,"userid");
        new target_playerClient = GetClientOfUserId(target_playerID);
        
        PrintToChatAll("%s", GetPlayerName(target_playerClient));
    }

    GetPlayerName(any:target_playerClient)
    {
        new String:target_playerName[20];
        GetClientName(target_playerClient,target_playerName,sizeof(target_playerName));

        return target_playerName;
    }
SamuraiBarbi is offline
Send a message via AIM to SamuraiBarbi Send a message via MSN to SamuraiBarbi Send a message via Yahoo to SamuraiBarbi
pheadxdll
AlliedModders Donor
Join Date: Jun 2008
Old 05-21-2009 , 11:57   Re: Why Can't I return a String?
Reply With Quote #2

That's because you can't return strings in SourcePawn. I'd recommend just scrapping the function because there's GetClientName.
__________________
pheadxdll is offline
SamuraiBarbi
Senior Member
Join Date: Aug 2006
Location: United States
Old 05-21-2009 , 12:00   Re: Why Can't I return a String?
Reply With Quote #3

Thx, I know there's GetClientName, like I said this is just something I wrote to check if I could return a string. That can't possibly be right though. We can't return strings in SourcePawn? That's dumb. Why not?
SamuraiBarbi is offline
Send a message via AIM to SamuraiBarbi Send a message via MSN to SamuraiBarbi Send a message via Yahoo to SamuraiBarbi
BAILOPAN
Join Date: Jan 2004
Old 05-21-2009 , 12:14   Re: Why Can't I return a String?
Reply With Quote #4

SamuraiBarbi: It's supposed to work in some situations, in others its impossible. Your usage looks okay, the only problem is the tag mismatch - the compiler looks like it has a bug with tagged return arrays.

Feel free to file.

Impossible situations are: publics/natives returning strings, or trying to call a string-return into a mismatched, dynamic, or automatically sized array. there may be more.
__________________
egg
BAILOPAN is offline
SamuraiBarbi
Senior Member
Join Date: Aug 2006
Location: United States
Old 05-21-2009 , 12:30   Re: Why Can't I return a String?
Reply With Quote #5

Ah okie dokies! Thx again phread for letting me know and Bailopan for shedding more details on the matter.
SamuraiBarbi is offline
Send a message via AIM to SamuraiBarbi Send a message via MSN to SamuraiBarbi Send a message via Yahoo to SamuraiBarbi
naris
AlliedModders Donor
Join Date: Dec 2006
Old 05-21-2009 , 18:30   Re: Why Can't I return a String?
Reply With Quote #6

You can't return strings because strings are arrays and you can't return arrays, you can only return a single cell.

(Also, the function would have to be defined as String:GetPlayerName() to avoid a tag mismatch, but that's not sufficient since it really is a String array and you can define a String[]:function())

Also, even if you could return an array, this code would not work since you are attempting to return a local array (that exists on the stack), which is freed when the function returns (that would leave a dangling pointer, if pawn had pointers).

What you have to do is define the function like all the other sourcemod function that "return" strings, by passing the string and max length and copying the string to the result

Code:
    GetPlayerName(any:target_playerClient, String:name[], maxlength)
    {
        new String:target_playerName[20];
        GetClientName(target_playerClient,target_playerName,sizeof(target_playerName));

        strcopy(name, maxlength, target_playerName;
    }

Last edited by naris; 05-21-2009 at 18:32.
naris is offline
naris
AlliedModders Donor
Join Date: Dec 2006
Old 05-21-2009 , 18:31   Re: Why Can't I return a String?
Reply With Quote #7

Quote:
Originally Posted by BAILOPAN View Post
Your usage looks okay, the only problem is the tag mismatch
Wouldn't "returning" a reference to a local variable be a problem?
naris is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:29.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode