Raised This Month: $32 Target: $400
 8% 

Passing a string in function


Post New Thread Reply   
 
Thread Tools Display Modes
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 03-11-2017 , 06:03   Re: Passing a string in function
Reply With Quote #11

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 #12

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
KiLLeR.
Senior Member
Join Date: Jul 2014
Location: Bulgaria
Old 03-28-2017 , 12:13   Re: Passing a string in function
Reply With Quote #13

I have not forgotten about this post, I didn't noticed, that I'm using the same name for both an array and int variable, thanks to all for help.
KiLLeR. is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 03-28-2017 , 13:55   Re: Passing a string in function
Reply With Quote #14

Whats wrong with get_user_team(id, team[], len) native its also returns team number..
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
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 22:59.


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