AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Passing a string in function (https://forums.alliedmods.net/showthread.php?t=294897)

KiLLeR. 03-10-2017 17:04

Passing a string in function
 
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"


HamletEagle 03-10-2017 17:08

Re: Passing a string in function
 
Pass the string size as a param.

edon1337 03-10-2017 17:15

Re: Passing a string in function
 
?
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; }

KiLLeR. 03-10-2017 18:36

Re: Passing a string in function
 
Quote:

Originally Posted by HamletEagle (Post 2502499)
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 (Post 2502500)
?
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!

OciXCrom 03-10-2017 18:55

Re: Passing a string in function
 
team[] => team[16]

Bugsy 03-10-2017 19:16

Re: Passing a string in function
 
Quote:

Originally Posted by OciXCrom (Post 2502520)
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;



KiLLeR. 03-10-2017 19:55

Re: Passing a string in function
 
Quote:

Originally Posted by Bugsy (Post 2502527)
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.

OciXCrom 03-10-2017 19:59

Re: Passing a string in function
 
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.

Bugsy 03-10-2017 20:10

Re: Passing a string in function
 
Yes the problem was not having to specify the string size in the parameter.

HamletEagle 03-11-2017 02:19

Re: Passing a string in function
 
It was one of the problems. Compiler does not know the size of team param(unless it's cleary specified in the header - team[32] or whatever) and will issue a warning:
Code:

warning 224: indeterminate array size in "sizeof" expression (symbol "")
In certain situations this could lead to a crash or unwanted behaviour.


All times are GMT -4. The time now is 18:02.

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