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

Solved argument type mismatch


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Watermelonnable
Member
Join Date: Feb 2017
Old 12-03-2019 , 22:32   argument type mismatch
Reply With Quote #1

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?

Last edited by Watermelonnable; 12-04-2019 at 18:16. Reason: typos
Watermelonnable is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-03-2019 , 23:05   Re: argument type mismatch
Reply With Quote #2

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"
}
__________________

Last edited by fysiks; 12-03-2019 at 23:08.
fysiks is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 12-03-2019 , 23:07   Re: argument type mismatch
Reply With Quote #3

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.
__________________

Last edited by ddhoward; 12-03-2019 at 23:09.
ddhoward is offline
Watermelonnable
Member
Join Date: Feb 2017
Old 12-04-2019 , 00:11   Re: argument type mismatch
Reply With Quote #4

Thank you so much guys. It worked like a charm now. And thank you fysiks, will have that in mind for the future
Watermelonnable 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 07:39.


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