Raised This Month: $ Target: $400
 0% 

Passing a string in function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
KiLLeR.
Senior Member
Join Date: Jul 2014
Location: Bulgaria
Old 03-10-2017 , 17:04   Passing a string in function
Reply With Quote #1

PHP Code:
new team[32];
get_opposite_team(idattackerteam);

stock get_opposite_team(indexteam[])
{
    new 
CsTeams:team cs_get_user_team(index);
    
    if(
team == CS_TEAM_T)
    {
        
copy(teamcharsmax(team), "TERRORIST"); // line 549;
    
}
    else
    {
        
copy(teamcharsmax(team), "CT"); // line 553;
    
}
    
    return 
0;
// line 557; 
It's obviously what i'm trying, but i got some errors.. WHY!??
Code:
D:\...\custom_stocks.inc(549) : error 035: argument type mismatch (argument 1)
D:\...\custom_stocks.inc(549) : error 035: argument type mismatch (argument 1)
D:\...\custom_stocks.inc(553) : error 035: argument type mismatch (argument 1)
D:\...\custom_stocks.inc(553) : error 035: argument type mismatch (argument 1)
D:\...\custom_stocks.inc(557) : warning 203: symbol is never used: "team"
KiLLeR. is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 03-10-2017 , 17:08   Re: Passing a string in function
Reply With Quote #2

Pass the string size as a param.
__________________
HamletEagle is offline
KiLLeR.
Senior Member
Join Date: Jul 2014
Location: Bulgaria
Old 03-10-2017 , 18:36   Re: Passing a string in function
Reply With Quote #3

Quote:
Originally Posted by HamletEagle View Post
Pass the string size as a param.
Did you mean this:
PHP Code:
new team[32];
get_opposite_team(idattackerteamcharsmax(team));

stock get_opposite_team(indexteam[], len)
{
    new 
CsTeams:team cs_get_user_team(index);
    
    if(
team == CS_TEAM_T)
    {
        
copy(teamlen"TERRORIST"); // line 549;
    
}
    else
    {
        
copy(teamlen"CT"); // line 553;
    
}
    
    return 
0;
// line 557; 
If yes, still throws the same errors!

Quote:
Originally Posted by edon1337 View Post
?
Code:
if(equal(get_opposite_team(idattacker), "CT")) // ... // stock get_opposite_team(index) {     new CsTeams:team_player = cs_get_user_team(index);     new team[32];           if(team_player == CS_TEAM_T)     {         copy(team, charsmax(team), "TERRORIST"); // line 549;     }         else if(team_player == CS_TEAM_CT)     {         copy(team, charsmax(team), "CT"); // line 553;     }           return team; }
If I wanted to do it like that, I would, but this doesn't help me!

Last edited by KiLLeR.; 03-10-2017 at 18:41.
KiLLeR. is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 03-10-2017 , 17:15   Re: Passing a string in function
Reply With Quote #4

?
Code:
if(equal(get_opposite_team(idattacker), "CT")) // ... // stock get_opposite_team(index) {     new CsTeams:team_player = cs_get_user_team(index);     new team[32];           if(team_player == CS_TEAM_T)     {         copy(team, charsmax(team), "TERRORIST"); // line 549;     }         else if(team_player == CS_TEAM_CT)     {         copy(team, charsmax(team), "CT"); // line 553;     }           return team; }
__________________
edon1337 is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 03-10-2017 , 18:55   Re: Passing a string in function
Reply With Quote #5

team[] => team[16]
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-10-2017 , 19:16   Re: Passing a string in function
Reply With Quote #6

Quote:
Originally Posted by OciXCrom View Post
team[] => team[16]
Why?

PHP Code:
new szTeam10 ];
get_opposite_teamid szTeam charsmaxszTeam ) );
    
stock get_opposite_teamindex szTeam[] , maxchars )
{
    return 
copyszTeam maxchars cs_get_user_teamindex ) == CS_TEAM_T "CT" "TERRORIST" );

Instead of getting the team name string, why not get the CsTeams team value? If the specified player is unassigned or spec, it will return the same.

PHP Code:
new CsTeams:ctOppositeTeam get_opposite_teamid );

stock CsTeams:get_opposite_teamid )
{
    new 
CsTeams:ctTeam cs_get_user_teamid );
    
    if ( 
CS_TEAM_T <= ctTeam <= CS_TEAM_CT 
        
ctTeam = ( ctTeam == CS_TEAM_T ) ? CS_TEAM_CT CS_TEAM_T;
        
    return 
ctTeam;

__________________

Last edited by Bugsy; 03-10-2017 at 19:18.
Bugsy is offline
KiLLeR.
Senior Member
Join Date: Jul 2014
Location: Bulgaria
Old 03-10-2017 , 19:55   Re: Passing a string in function
Reply With Quote #7

Quote:
Originally Posted by Bugsy View Post
Why?

PHP Code:
new szTeam10 ];
get_opposite_teamid szTeam charsmaxszTeam ) );
    
stock get_opposite_teamindex szTeam[] , maxchars )
{
    return 
copyszTeam maxchars cs_get_user_teamindex ) == CS_TEAM_T "CT" "TERRORIST" );

Instead of getting the team name string, why not get the CsTeams team value? If the specified player is unassigned or spec, it will return the same.

PHP Code:
new CsTeams:ctOppositeTeam get_opposite_teamid );

stock CsTeams:get_opposite_teamid )
{
    new 
CsTeams:ctTeam cs_get_user_teamid );
    
    if ( 
CS_TEAM_T <= ctTeam <= CS_TEAM_CT 
        
ctTeam = ( ctTeam == CS_TEAM_T ) ? CS_TEAM_CT CS_TEAM_T;
        
    return 
ctTeam;

The first code looks pretty same as mine.
And second code don't help me, because I need the string to use directly. And yes i can check with if and apply to string, but i wan't to know what's not good with mine code.
KiLLeR. is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 03-10-2017 , 19:59   Re: Passing a string in function
Reply With Quote #8

The destination buffer needs to have a size if I'm not mistaking. I'm on my phone and can test it right now, but I'm pretty sure I had the same problem once and fixed it like that. Try it. I'm 99% sure that's the problem.

//edit: lol, I just noticed... The "team" string in the function header has the same name with the "team" integer down below, so you're basically using "copy" on an integer. This is why szTeam and iTeam style names should be used.
__________________

Last edited by OciXCrom; 03-10-2017 at 20:03.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 03-11-2017 , 06:03   Re: Passing a string in function
Reply With Quote #9

Quote:
Originally Posted by OciXCrom View Post
//edit: lol, I just noticed... The "team" string in the function header has the same name with the "team" integer down below, so you're basically using "copy" on an integer. This is why szTeam and iTeam style names should be used.
Yup, I already realised it out and posted but he didn't like that way.
__________________
edon1337 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-11-2017 , 10:22   Re: Passing a string in function
Reply With Quote #10

Going back to his original post, as OciXCrom just pointed out, the problem is he has the same variable 'team' defined as both an array and a single cell variable. Within the function, copy() is using the cell version of the variable 'team' instead of the array 'team[]' in the parameter list; this results in error 035: argument type mismatch (argument 1) since copy() expects an array.

Code:
new team[32]; get_opposite_team(idattacker, team); stock get_opposite_team(index, team[]) {     new CsTeams:team = cs_get_user_team(index);         if(team == CS_TEAM_T)     {         copy(team, charsmax(team), "TERRORIST"); // line 549;     }     else     {         copy(team, charsmax(team), "CT"); // line 553;     }         return 0; } // line 557;

In addition to that, you cannot use charsmax() on an unsized array in the function parameter list. So yes, specifying the array size in the parameter list will allow charsmax to be used. But it is more common to leave the array size open and require the user to specify the maximum chars of the array/string being passed.
PHP Code:
public Testid )
{
    new 
szTeam10 ];

    
get_opposite_teamid szTeam charsmaxszTeam ) );

    
get_opposite_team2id szTeam );
}

stock get_opposite_teamindex szTeam[] , maxchars )
{
    return 
copyszTeam maxchars cs_get_user_teamindex ) == CS_TEAM_T "CT" "TERRORIST" );
}  

stock get_opposite_team2index szTeam10 ] )
{
    return 
copyszTeam charsmaxszTeam ) , cs_get_user_teamindex ) == CS_TEAM_T "CT" "TERRORIST" );

Both versions still have a flaw because if the user is a spectator or unassigned, it will always return TERRORIST.

Here is another version that will do the following:
  • Only return a value if the source player is T or CT
  • Allow you to get both the string team name and integer team value CS_TEAM_T or CS_TEAM_CT. The string/size parameters are optional. If specified, it will populate with team name. The function will always return integer opposite team value.
PHP Code:
public Testid )
{
    
//Use function to get string team name.
    
new szTeam10 ];
    if ( 
get_opposite_teamid szTeam charsmaxszTeam ) ) )
    {
        
//Successfully found opposite T or CT team
        //szTeam holds opposite team name.
    
}
    else
    {
        
//Source player was unassigned or spectator.
        //szTeam holds nothing (remains unchanged).
    
}
    
    
//============================================================

    //Use function to get integer team value (CS_TEAM_T , CS_TEAM_CT)
    
new CsTeams:iOppositeTeam get_opposite_teamid );
    if ( 
iOppositeTeam )
    {
        
//Successfully found opposite T or CT team
    
}
    else
    {
        
//Source player was unassigned or spectator.
    
}
}

stock CsTeams:get_opposite_teamindex szTeam[]="" iMaxChars=)
{
    new 
CsTeams:iOppositeTeam;
    new 
CsTeams:iTeam cs_get_user_teamindex );
    new const 
szTeamNamesCsTeams ][] = { "" "TERRORIST" "CT" "" };
    
    if ( 
CS_TEAM_T <= iTeam <= CS_TEAM_CT )
    {
        
iOppositeTeam CsTeams:abs_:( iTeam CS_TEAM_SPECTATOR ) ); 
        
        if ( 
iMaxChars )
            
copyszTeam iMaxChars szTeamNamesiOppositeTeam ] );
    }
    
    return 
iOppositeTeam;

__________________

Last edited by Bugsy; 03-11-2017 at 11:01.
Bugsy is offline
Reply


Thread Tools
Display Modes

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 18:02.


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