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

Question about modularizing code


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 03-05-2015 , 20:45   Question about modularizing code
Reply With Quote #1

trying to learn how to modularizing code
but having trouble
following is my code
i can't get "test2" or "test2 from main" to print only prints "hello"

main.sp
PHP Code:
#include <sourcemod>

#if !defined STANDALONE_BUILD
#include "module/test2.sp"
#endif

public OnPluginStart()
{
    
HookEvent("player_jump"Event_PlayerJump);
    
#if !defined STANDALONE_BUILD
    
Test2_OnPluginStart();
#endif

}

public 
Action:Event_PlayerJump(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));

    if(!
IsClientInGame(client))
        return 
Plugin_Continue;
    
PrintToChat(client"hello");
    return 
Plugin_Continue;

    
#if !defined STANDALONE_BUILD
        
Test2_OnPlayerJump(client);
    
#endif
        
    
return Plugin_Continue;

test2.sp
PHP Code:
#if defined STANDALONE_BUILD
#include <sourcemod> //include sourcemod if compile by itself
#endif

#if defined STANDALONE_BUILD
public OnPluginStart() //use this if compile by itself
#else
public Test2_OnPluginStart() //use this if not compiled by itself
#endif
{
    
PrintToServer("test2");
#if defined STANDALONE_BUILD
    
HookEvent("player_jump"Test2_PlayerJump); //hook if compile by itself
#endif
}

#if defined STANDALONE_BUILD
public Action:Test2_PlayerJump(Handle:event, const String:name[], bool:dontBroadcast//use this if compiled by itself
#else
public Test2_OnPlayerJump(client//use this if not compiled by itself
#endif
{
    
//use the follow code if compile by itself
    #if defined STANDALONE_BUILD
        
new client GetClientOfUserId(GetEventInt(event"userid"));

        if(!
IsClientInGame(client))
            return 
Plugin_Continue;
        
PrintToChat(client"test2");
    
#endif
    
PrintToChat(client"test2 from main"); //use this if not compiled by itself
    
    #if defined STANDALONE_BUILD
        
return Plugin_Continue;
    
#endif

8guawong is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 03-05-2015 , 21:00   Re: Question about modularizing code
Reply With Quote #2

One thing, you have unreachable code in main.sp's jump hook(being the bit calling test2's OnJump method).
Edit: Try below:
main.sp
PHP Code:
#include <sourcemod>
//#define STANDALONE_BUILD //Uncomment this if you want module test2 to become the main plugin.
#include "module/test2.sp"
#if !defined STANDALONE_BUILD
public OnPluginStart()
{
    
HookEvent("player_jump"Event_PlayerJump);
    
Test2_OnPluginStart();
}

public 
Action:Event_PlayerJump(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));
    if(!
IsClientInGame(client))
        return 
Plugin_Continue;
    
PrintToChat(client"hello");
    
Test2_OnPlayerJump(client);
    return 
Plugin_Continue;
}
#endif 
test2.sp
PHP Code:
#if defined STANDALONE_BUILD
#include <sourcemod> //include sourcemod if compile by itself
#endif

#if defined STANDALONE_BUILD
public OnPluginStart() //use this if compile by itself
#else
public Test2_OnPluginStart() //use this if not compiled by itself
#endif
{
    
PrintToServer("test2");
#if defined STANDALONE_BUILD
    
HookEvent("player_jump"Test2_PlayerJump); //hook if compile by itself
#endif
}

#if defined STANDALONE_BUILD
public Action:Test2_PlayerJump(Handle:event, const String:name[], bool:dontBroadcast//use this if compiled by itself
#else
public Test2_OnPlayerJump(client//use this if not compiled by itself
#endif
{
    
//use the follow code if compile by itself
    #if defined STANDALONE_BUILD
    
new client GetClientOfUserId(GetEventInt(event"userid"));
    if(!
IsClientInGame(client))
        return 
Plugin_Continue;
    
PrintToChat(client"test2");
    return 
Plugin_Continue;
    
#else
    
PrintToChat(client"test2 from main"); //use this if not compiled by itself
    #endif

Lysis decompile of above code I posted:
PHP Code:
public Test2_OnPluginStart()
{
    
PrintToServer("test2");
    return 
0;
}

public 
Test2_OnPlayerJump(client)
{
    
PrintToChat(client"test2 from main");
    return 
0;
}

public 
void:OnPluginStart()
{
    
HookEvent("player_jump"Event_PlayerJumpEventHookMode:1);
    
Test2_OnPluginStart();
    return 
void:0;
}

public 
Action:Event_PlayerJump(Handle:eventString:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"0));
    if (!
IsClientInGame(client))
    {
        return 
Action:0;
    }
    
PrintToChat(client"hello");
    
Test2_OnPlayerJump(client);
    return 
Action:0;

__________________

Last edited by WildCard65; 03-05-2015 at 21:59.
WildCard65 is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 03-05-2015 , 21:16   Re: Question about modularizing code
Reply With Quote #3

hey thanks! that worked after removing
Code:
return Plugin_Continue;
it works but my question is now why when i compile test2.sp by itself i get

PHP Code:
public Test2_OnPluginStart()
{
    
PrintToServer("test2");
    return 
0;
}

public 
Test2_OnPlayerJump(client)
{
    
PrintToChat(client"test2 from main");
    return 
0;

when i'm expecting

PHP Code:
public void:OnPluginStart()
{
    
HookEvent("player_jump"Test2_PlayerJumpEventHookMode:1);
    return 
0;
}

public 
Action:Test2_PlayerJump(Handle:eventString:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"0));
    if (!
IsClientInGame(client))
    {
        return 
Action:0;
    }
    
PrintToChat(client"test2");
    return 
Action:0;

from lysis

Last edited by 8guawong; 03-05-2015 at 21:17.
8guawong is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 03-05-2015 , 21:57   Re: Question about modularizing code
Reply With Quote #4

I get the following(with updated reply above with the define uncommented):
PHP Code:
public void:OnPluginStart()
{
    
PrintToServer("test2");
    
HookEvent("player_jump"Test2_PlayerJumpEventHookMode:1);
    return 
void:0;
}

public 
Action:Test2_PlayerJump(Handle:eventString:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"0));
    if (!
IsClientInGame(client))
    {
        return 
Action:0;
    }
    
PrintToChat(client"test2");
    return 
Action:0;

Edit: Reason your not getting your expected output with compiling test2 byitself cause of no define.
__________________

Last edited by WildCard65; 03-05-2015 at 22:00.
WildCard65 is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 03-05-2015 , 22:14   Re: Question about modularizing code
Reply With Quote #5

Quote:
Originally Posted by WildCard65 View Post
I get the following(with updated reply above with the define uncommented):
PHP Code:
public void:OnPluginStart()
{
    
PrintToServer("test2");
    
HookEvent("player_jump"Test2_PlayerJumpEventHookMode:1);
    return 
void:0;
}

public 
Action:Test2_PlayerJump(Handle:eventString:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"0));
    if (!
IsClientInGame(client))
    {
        return 
Action:0;
    }
    
PrintToChat(client"test2");
    return 
Action:0;

Edit: Reason your not getting your expected output with compiling test2 byitself cause of no define.
ah thanks i knew it was something basic that i'm missing
i thought STANDALONE_BUILD was built into sourcemod
8guawong is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 03-05-2015 , 22:22   Re: Question about modularizing code
Reply With Quote #6

Quote:
Originally Posted by 8guawong View Post
ah thanks i knew it was something basic that i'm missing
i thought STANDALONE_BUILD was built into sourcemod
pretty sure it's not for SM plugins
__________________
WildCard65 is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 03-05-2015 , 23:04   Re: Question about modularizing code
Reply With Quote #7

Quote:
Originally Posted by WildCard65 View Post
pretty sure it's not for SM plugins


doing some more testing, it seems like you can hook an event twice in one plugin??
i always thought it'll use the first hook

using example from above

main.php
PHP Code:
#include <sourcemod> 

#if !defined STANDALONE_BUILD 
#include "module/test2.sp" 
#endif 

public OnPluginStart() 

    
HookEvent("player_jump"Event_PlayerJump); 
     
#if !defined STANDALONE_BUILD 
    
Test2_OnPluginStart(); 
#endif 



public 
Action:Event_PlayerJump(Handle:event, const String:name[], bool:dontBroadcast

    new 
client GetClientOfUserId(GetEventInt(event"userid")); 

    if(!
IsClientInGame(client)) 
        return 
Plugin_Continue
    
PrintToChat(client"hello");          
    return 
Plugin_Continue

test2.php
PHP Code:
#if defined STANDALONE_BUILD 
public OnPluginStart() //use this if compile by itself 
#else 
public Test2_OnPluginStart() //use this if not compiled by itself 
#endif 

    
PrintToServer("test2"); 
    
HookEvent("player_jump"Test2_PlayerJump); //hook if compile by itself 


public 
Action:Test2_PlayerJump(Handle:event, const String:name[], bool:dontBroadcast//use this if compiled by itself 

     new 
client GetClientOfUserId(GetEventInt(event"userid")); 

    if(!
IsClientInGame(client)) 
        return 
Plugin_Continue

    
PrintToChat(client"test2 from main"); //use this if not compiled by itself 
     
    
return Plugin_Continue

is it bad to do this?
8guawong is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 03-06-2015 , 06:52   Re: Question about modularizing code
Reply With Quote #8

First, let me explain how it works: HookEvent takes the input function and stores it in an internal function list that gets iterated on event fire. 2ndly, I have no idea if it's good or not.
__________________
WildCard65 is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 03-06-2015 , 07:22   Re: Question about modularizing code
Reply With Quote #9

using global forwards would be the easiest...
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram 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 08:26.


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