Automatons

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1333
Gender:
United States of America

Automatons

Post by Archive-Poster »

I was reading the pawn manual when i stepped into a topic called "Automatons". I had studied it in the university so it wasn't news for me.

Anyway, i have a use for it. It may give you ideas for use them somehow that it can improve plugins performance.

The code below can be used to enable/disable a Ham Forward without if clauses and without need to check if it registered.

Is it an improvement to the "normal" process? I don't know. I guess it is but, is safer to ask anyone that knows how to analyze compiled code.

Code: Select all

#include <amxmodx>
#include <hamsandwich>

#define PLUGIN    "Automaton"
#define AUTHOR    "Albernaz o Carniceiro Demoniaco"
#define VERSION    "1.0"

new HamHook:HamHookSpawn

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)        
    
    // The start point: unregistered 
    
    state unregistered;
}

public enableHam() <unregistered>
{
    // If it's unregistered and is wanted to be enabled, it must be registered
    
    HamHookSpawn = RegisterHam(Ham_Spawn,"player","playerSpawn");
    state enabled;
}

public enableHam() <disabled>
{
    // If it's disabled and is wanted to be enabled, it must be enabled
    
    EnableHamForward(HamHookSpawn);    
    state enabled;
}

public enableHam() <enabled> {}
public disableHam() <unregistered,disabled>{}
/*

The above two line can be:

public enableHam() <> {}
public disableHam() <>{}


*/


public disableHam() <enabled>
{    
    // If it's enabled and is wanted to be disabled, it must be disabled
    
    DisableHamForward(HamHookSpawn);    
    state disabled;
}

public playerSpawn(id)
{
    
} 
With the code above you would then do "enableHam()" to enable the forward, "disableHam()" to disable it.

Light version:

Code: Select all

#include <amxmodx>
#include <hamsandwich>

 #define PLUGIN    "Automaton"
#define AUTHOR    "Albernaz o Carniceiro Demoniaco"
#define VERSION    "1.0"

new HamHook:HamHookSpawn

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)                
    state unregistered;
}

public enableHam() <unregistered> state (HamHookSpawn = RegisterHam(Ham_Spawn,"player","playerSpawn") ) enabled;
public enableHam() <disabled> state (EnableHamForward(HamHookSpawn)) enabled;
public disableHam() <enabled> state (!DisableHamForward(HamHookSpawn)) disabled;
public enableHam() <> {}
public disableHam() <>{}

public playerSpawn(id)
{
    
} 
Debug version:

Code: Select all

#include <amxmodx>
#include <hamsandwich>

 #define PLUGIN    "Automaton"
#define AUTHOR    "Albernaz o Carniceiro Demoniaco"
#define VERSION    "1.0"

new HamHook:HamHookSpawn

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)        
    
    register_clcmd("enable","enable");
    register_clcmd("disable","disable");
        
    state unregistered;
}

public enable()
{
    enableHam()
    return PLUGIN_HANDLED;
}

public disable()
{
    disableHam()
    return PLUGIN_HANDLED;
}

public enableHam() <unregistered>
{
    HamHookSpawn = RegisterHam(Ham_Spawn,"player","playerSpawn");
    state enabled
    
    server_print("Registering");
}

public enableHam() <disabled>
{
    EnableHamForward(HamHookSpawn);    
    state enabled;
    
    server_print("Enabling");
}

public disableHam() <enabled>
{    
    DisableHamForward(HamHookSpawn);    
    state disabled;
    
    server_print("Disabling");
}

public enableHam() <> 
{
    server_print("Already enabled");
}
public disableHam() <>
{
    server_print("Already disabled");
}

public playerSpawn(id)
{
    
} 
Debug version usage output:

Code: Select all

] enable
Registering
] enable 
Already enabled
] disable
Disabling
] disable 
Already disabled
] enable
Enabling
] enable 
Already enabled 
User avatar
Archive-Poster
Posts: 1333
Gender:
United States of America

Re: Automatons

Post by Archive-Poster »

Adding to the tutorial:

When using automatons on functions that have parameters:

Code: Select all

saySomthing(id) <>
{
    client_print(id, print_chat, "This message is first time being sent, and the only time.");
    state disabled;
}

saySomething(id) <disabled>
{
    // compiler will say "id" is never used
}
To go around this, we can use a pre-compiler directive called #pragma.
In this situation, we will use #pragma unused.

Code: Select all

saySomthing(id) <>
{
    client_print(id, print_chat, "This message is first time being sent, and the only time.");
    state disabled;
}

saySomething(id) <disabled>
{
    // tells the compiler that it is okay that the variable is never used
    #pragma unused id
}
To learn more pre-compiler directives or more information about #pragma, read the Pawn manual.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests