AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved argument type mismatch (https://forums.alliedmods.net/showthread.php?t=320091)

Watermelonnable 12-03-2019 22:32

argument type mismatch
 
Hello guys. I'm trying to understand where's the problem with my code here.

If I try to call my function "MessageEveryone" and pass it a string as argument, the compiler returns me "argument type mismatch"

This is how I'm trying to test it:

PHP Code:

MessageEveryone("helo"); 

And this is my MessageEveryone function:
PHP Code:

public MessageEveryone(message)
{
    for (new 
0g_MaxPlayersi++) {
        if (!
is_user_connected(i)) {
            continue;
        }
            
        
doMagic(imessageGREEN5.0);
    }


If I remove the "message" parameter and insert the string directly in the "MessageEveryone" function it works perfectly:
PHP Code:

public MessageEveryone()
{
    for (new 
0g_MaxPlayersi++) {
        if (!
is_user_connected(i)) {
            continue;
        }
            
        
doMagic(i"this is a test"GREEN5.0);
    }


What am I doing wrong?

fysiks 12-03-2019 23:05

Re: argument type mismatch
 
You need to understand how strings are stored in Pawn. You can take a look at one of the introductions to Pawn programming (e.g. here and here).

Long story short, strings are arrays of characters, your argument in your definition of MessageEveryone() needs to be an array:

Code:

MessageEveryone(message[])
P.S. That is a very inefficient method to loop through all players. The generally accepted "best" method is as follows:

Code:

new iPlayers[32], iNumPlayers, id
get_players(iPlayers, iNumPlayers)
for( new i = 0; i < iNumPlayers; i++)
{
    id = iPlayers[i]
    // Execute your code with "id"
}


ddhoward 12-03-2019 23:07

Re: argument type mismatch
 
PHP Code:

public MessageEveryone(message)


The compiler assumes that message is a single integer.

https://wiki.alliedmods.net/Pawn

PHP Code:

public MessageEveryone(message[])


Also, depending on what doMagic() is doing, you may want to also pass the length of the character array.

PHP Code:

public MessageEveryone(message[], len)
{
    for (new 
0g_MaxPlayersi++) {
        if (!
is_user_connected(i)) {
            continue;
        }
            
        
doMagic(imessagelenGREEN5.0);
    }


EDIT: also yeah loop through players how fysiks suggested. Your method would be correct in Sourcemod, but I guess it's a different story in AMX Mod X.

Watermelonnable 12-04-2019 00:11

Re: argument type mismatch
 
Thank you so much guys. It worked like a charm now. And thank you fysiks, will have that in mind for the future :)


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

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