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

Automatons


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 02-18-2009 , 19:13   Automatons
Reply With Quote #1

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.
I've searched the forums to see if there were some known good use to them but just find this:

https://forums.alliedmods.net/showpo...11&postcount=2

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.

PHP Code:

#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(PLUGINVERSIONAUTHOR)        
    
    
// 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:

PHP Code:
#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(PLUGINVERSIONAUTHOR)                
    
state unregistered;
}

public 
enableHam() <unregisteredstate (HamHookSpawn RegisterHam(Ham_Spawn,"player","playerSpawn") ) enabled;
public 
enableHam() <disabledstate (EnableHamForward(HamHookSpawn)) enabled;
public 
disableHam() <enabledstate (!DisableHamForward(HamHookSpawn)) disabled;
public 
enableHam() <> {}
public 
disableHam() <>{}

public 
playerSpawn(id)
{
    

Debug version:

PHP Code:
#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(PLUGINVERSIONAUTHOR)        
    
    
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:

PHP Code:
enable
Registering
enable 
Already enabled
disable
Disabling
disable 
Already disabled
enable
Enabling
enable 
Already enabled 
__________________

Last edited by joaquimandrade; 02-19-2009 at 07:03. Reason: Code optimization
joaquimandrade is offline
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 11-17-2010 , 03:38   Re: Automatons
Reply With Quote #2

Sorry to *Bump*
But can u give more examples on this
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 11-17-2010 , 05:48   Re: Automatons
Reply With Quote #3

You can see a simple example in the InfiniteRound plugin. If I remember well, there are some more explanations in the Pawn guide.
__________________
Arkshine is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 11-17-2010 , 06:33   Re: Automatons
Reply With Quote #4

Don't use this or Albernaz o Carniceiro Demoniaco will come to get you.
Seta00 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-22-2010 , 14:55   Re: Automatons
Reply With Quote #5

Adding to the tutorial:

When using automatons on functions that have parameters:
Code:
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:
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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 11-22-2010 , 15:11   Re: Automatons
Reply With Quote #6

Or just do :

Code:
saySomething() <disabled>
{

}
LoL
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 11-22-2010 , 15:20   Re: Automatons
Reply With Quote #7

The header has to be the same. ;)

@Exolent[jNr] : I don't have this problem. Paste the whole code.
__________________

Last edited by Arkshine; 11-22-2010 at 15:31.
Arkshine is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-22-2010 , 16:12   Re: Automatons
Reply With Quote #8

Quote:
Originally Posted by Arkshine View Post
@Exolent[jNr] : I don't have this problem. Paste the whole code.
I've experienced this is my own plugins, but I'm sure this example will also produce the same result.
PHP Code:
#include < amxmodx >

public plugin_init( )
{
    
register_plugin"Automaton Function Parameter Test""0.0.1""Exolent" );
    
    
register_clcmd"say /print""CmdPrint" );
}

public 
CmdPrintiPlayer )
{
    
printSomethingiPlayer );
}

printSomethingiPlayer ) <>
{
    
client_printiPlayerprint_chat"Message." );
    
state printed;
}

printSomethingiPlayer ) <printed>
{
    
// compiler says
    // warning 203: symbol is never used: "iPlayer"

PHP Code:
#include < amxmodx >

public plugin_init( )
{
    
register_plugin"Automaton Function Parameter Test""0.0.1""Exolent" );
    
    
register_clcmd"say /print""CmdPrint" );
}

public 
CmdPrintiPlayer )
{
    
printSomethingiPlayer );
}

printSomethingiPlayer ) <>
{
    
client_printiPlayerprint_chat"Message." );
    
state printed;
}

printSomethingiPlayer ) <printed>
{
    
// compiler is happy
    #pragma unused iPlayer

It also seems that if the function is public then the compiler does not care if the parameters are unused.

PHP Code:
#include < amxmodx >

public plugin_init( )
{
    
register_plugin"Automaton Function Parameter Test""0.0.1""Exolent" );
    
    
register_clcmd"say /print""CmdPrint" );
}

public 
CmdPrintiPlayer )
{
    
printSomethingiPlayer );
}

printSomethingiPlayer ) <>
{
    
client_printiPlayerprint_chat"Message." );
    
state printed;
}

public 
printSomethingiPlayer ) <printed>
{
    
// compiler is happy

It is also strange that the compiler would allow one of them to be public and the other private.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 11-22-2010 , 16:52   Re: Automatons
Reply With Quote #9

I remember having the same issue when coding InfiniteRound, that's why I've used "public" as solution. Though no idea why there is this behavior.
__________________
Arkshine is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 11-22-2010 , 17:03   Re: Automatons
Reply With Quote #10

Quote:
Originally Posted by Arkshine View Post
I remember having the same issue when coding InfiniteRound, that's why I've used "public" as solution. Though no idea why there is this behavior.
Because when calling public functions you don't have access to its signature, just like you don't have access to natives' signatures.
Seta00 is offline
Reply


Thread Tools
Display Modes

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 03:23.


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