View Single Post
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 04-27-2012 , 11:49   Re: how would I write this plugin?
Reply With Quote #10

If you find yourself writing the same code repeatedly, extract a method out of it... find the things that change, make them variables, then move the code to a method that takes those variables. For instance:

PHP Code:
/* Plugin Template generated by Pawn Studio */

#include <sourcemod>
#include <sdktools>

public Plugin:myinfo 
{
    
name "New Plugin",
    
author "Unknown",
    
description "<- Description ->",
    
version "1.0",
    
url "<- URL ->"
}

public 
OnPluginStart()
{
    
RegAdminCmd("sm_bm_on"Command_OneADMFLAG_SLAY"");
    
RegAdminCmd("sm_bm_off"Command_TwoADMFLAG_SLAY"");
    
RegAdminCmd("sm_rm_on"Command_ThreeADMFLAG_SLAY"");
    
RegAdminCmd("sm_rm_off"Command_FourADMFLAG_SLAY"");
}

public 
Action:Command_One(clientargs)
{
    
SetValue("bsm"1);
    return 
Plugin_Handled;
 }

public 
Action:Command_Two(clientargs)
{
    
SetValue("bsm"0);
    return 
Plugin_Handled;
  }

public 
Action:Command_Three(clientargs)
{
    
SetValue("rsm"1);
    return 
Plugin_Handled;
}

public 
Action:Command_Four(clientargs)
{
    
SetValue("rsm"0);
    return 
Plugin_Handled;
}

SetValue(const String:name[], value)
{
    new 
entity = -1;
    while ((
entity FindEntityByClassname(entity"logic_branch"))!=INVALID_ENT_REFERENCE)
    {
        new 
String:entName[MAX_NAME_LENGTH];
        
GetEntPropString(entityProp_Data"m_iName"entNamesizeof(entName));
        if(
StrEqual(entNamename))
        {
            
SetVariantInt(value);
            
AcceptEntityInput(entity"SetValueTest");
            break;
        }
    }

Note: I left it as SetVariantInt and the type as a number, because I wasn't sure if should be a bool or not.
I also included Tylerst's fix as noted in the previous post.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 04-27-2012 at 11:52.
Powerlord is offline