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

Solved Need help with methodmap please.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
GlowingTree
New Member
Join Date: Dec 2021
Location: Guangdong-China
Old 05-21-2023 , 11:13   Need help with methodmap please.
Reply With Quote #1

Hi, i am trying to write a L4D2 log plugin by methodmap which may make it more object-oriented, its code looks like this, and i saved it as a single ".sp" file.
PHP Code:
methodmap Logger __nullable__
{

    
// because the methodmap must has a constructor and the constructor cannot do nothing like 'public Logger() {}', so i use a bool value as a data
    
public Logger(bool enable)
    {
        return 
view_as<Logger>(enable);
    }

    
// print the log info to all clients' console via VFormat and PrintToConsoleAll
    
public void debug(const char[] messageany ...)
    {
        
char buffer[512];
        
VFormat(buffersizeof(buffer), message2);
        
PrintToConsoleAll(buffer);
    }

    
// print the log info to the given client's console
    
public void debugToClient(int client, const char[] messageany ...)
    {
        if (
client || client >= MaxClients || !IsClientInGame(client) || IsFakeClient(client)) { return; }
        
char buffer[512];
        
VFormat(buffersizeof(buffer), message3);
        
PrintToConsole(clientbuffer);
    }

    
// other logging methods...


And i writed a simple testing plugin to test the Logger and check if it works properly. The code for testing plugin looks like this.
PHP Code:
#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdktools>
// including the file "logging.sp"
#include "logger.sp"

public void OnPluginStart()
{
    
int a 10;
    
float b 20.0;
    
bool c false;
    
char[] "hello";
        
// because Logger is nullable so we should use "new" to create an object, and use the "debug" method
    
Logger log = new Logger(true);
    
log.debug("test debug 1: int:%d, float:%.2f, bool:%b, string:%s"abcd);
        
// using method "debugInMainFile" from the testing plugin file
    
debugInMainFile("test debug 2: int:%d, float:%.2f, bool:%b, string:%s"abcd);
}

// this method is the same as the "debug" method in the file "logger"
public void debugInMainFile(const char[] messageany ...)
{
    
char buffer[512];
    
VFormat(buffersizeof(buffer), message2);
    
PrintToConsoleAll(buffer);

I am attamping to print the value of variables to all clients' console, but the outputs of the two methods are different, the console output is as follows.
PHP Code:
test debug 1int:1953719668float:0.00bool:1000001101000000000000000000000string:
test debug 2int:10float:20.00bool:0string:hello 
We could see the output using method "log.debug" is incorrect while the output using method "debugInMainFile" is correct. I am wondering why, any help would be greatly appreiated, thank you
Sorry for my poor English...

Last edited by GlowingTree; 05-22-2023 at 00:50.
GlowingTree is offline
helloworlds
Junior Member
Join Date: Oct 2022
Old 05-21-2023 , 12:59   Re: Need help with methodmap please.
Reply With Quote #2

I'm not too familiar with methodmaps, but a lot of these object oriented things use some kind of notion of "self" as the implicit first argument. Try VFormat with the third argument, instead of second:

PHP Code:
VFormat(buffersizeof(buffer), message3); 

Last edited by helloworlds; 05-21-2023 at 13:05.
helloworlds is offline
GlowingTree
New Member
Join Date: Dec 2021
Location: Guangdong-China
Old 05-22-2023 , 00:48   Re: Need help with methodmap please.
Reply With Quote #3

Quote:
Originally Posted by helloworlds View Post
I'm not too familiar with methodmaps, but a lot of these object oriented things use some kind of notion of "self" as the implicit first argument. Try VFormat with the third argument, instead of second:

PHP Code:
VFormat(buffersizeof(buffer), message3); 
Thanks a lot! , i changed the code of the method "debug" like this
PHP Code:
methodmap Logger __nullable__
{

    public 
Logger(bool enable)
    {
        return 
view_as<Logger>(enable);
    }

    public 
void debug(const char[] messageany ...)
    {
        
char buffer[512];
        
VFormat(buffersizeof(buffer), message3);
        
PrintToConsoleAll(buffer);
    }

    
// other code...

Now it can work properly.
GlowingTree 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 12:56.


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