Raised This Month: $28 Target: $400
 7% 

Module: OO (Simulating OOP in AMXXPAWN language)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
11922911
Senior Member
Join Date: Dec 2011
Location: Yuen Long Country
Old 09-03-2023 , 00:47   Module: OO (Simulating OOP in AMXXPAWN language)
Reply With Quote #1


Object-Oriented Simulation for AMXXPawn
GITHUB repo: https://github.com/hollacs/oo_amxx


Introduce:
- OO is an amxx module for the amxxpawn language that can implement the basic features of object-oriented programming,
- such as classes, objects, inheritance, etc.
- It uses some special syntax and functions to simulate the concepts of object-oriented programming,
- allowing developers to write amxxpawn code in a more concise and flexible way.


Installation:
1. Download the latest release file in https://github.com/hollacs/oo_amxx/releases/latest
(or you can compile it on your own)

2. Extract the zip into the /addons/amxmodx/ (the /scripting/ folder contains some example plugins and .inc)

3. Include the oo.inc and use it in your plugin.


Usage:
PHP Code:
#include <amxmodx>
#include <oo>

#define main plugin_init

public oo_init()
{
    
// A base class for all animals
    
oo_class("Animal")
    {
        new 
cl[] = "Animal";

        
oo_var(cl"age"1); // A attribute that stores the age of the animal
        
oo_var(cl"name"32); // A attribute that stores the name of the animal

        // A constructor that takes the name and the age of the animal
        
oo_ctor(cl"Ctor", @str(name), @int(age));

        
// A destructor of the animal
        
oo_dtor(cl"Dtor");

        
// A function that returns the sound of the animal
        
oo_mthd(cl"MakeSound", @stref(msg), @int(len));

        
// A function that returns the number of legs of the animal
        
oo_mthd(cl"GetLegs");

        
// A function that prints the name and sound of the animal
        
oo_mthd(cl"Introduce");

        
oo_smthd(cl"Test"); // static method test
    
}

    
// A derived class for dogs that inherits from Animal
    
oo_class("Dog""Animal")
    {
        new 
cl[] = "Dog";

        
// A constructor that calls the base class constructor with the name "Dog"
        
oo_ctor(cl"Ctor", @int(age));

        
// An override function that returns the sound of a dog
        
oo_mthd(cl"MakeSound", @stref(msg), @int(len));

        
// An override function that returns the number of legs of a dog
        
oo_mthd(cl"GetLegs");
    }

    
// A derived class for cats that inherits from Animal
    
oo_class("Cat""Animal")
    {
        new 
cl[] = "Cat";

        
// A constructor that calls the base class constructor with the name "Cat"
        
oo_ctor(cl"Ctor", @int(age));

        
// An override function that returns the sound of a cat
        
oo_mthd(cl"MakeSound", @stref(msg), @int(len));

        
// An override function that returns the number of legs of a cat
        
oo_mthd(cl"GetLegs");
    }

    
// A derived class for birds that inherits from Animal
    
oo_class("Bird""Animal")
    {
        new 
cl[] = "Bird";

        
// A constructor that calls the base class constructor with the name "Bird"
        
oo_ctor(cl"Ctor", @int(age));

        
// An override function that returns the sound of a bird
        
oo_mthd(cl"MakeSound", @stref(msg), @int(len));

        
// An override function that returns the number of legs of a bird
        
oo_mthd(cl"GetLegs");
    }

    
// A derived class for snakes that inherits from Animal
    
oo_class("Snake""Animal")
    {
        new 
cl[] = "Snake";

        
// A constructor that calls the base class constructor with the name "Snake"
        
oo_ctor(cl"Ctor", @int(age));

        
// An override function that returns the sound of a snake
        
oo_mthd(cl"MakeSound", @stref(msg), @int(len));

        
// An override function that returns the number of legs of a snake
        
oo_mthd(cl"GetLegs");
    }
}

// A constructor of Animal
public Animal@Ctor(const name[], age)
{
    new 
this oo_this(); // Get this object hash
    
oo_set_str(this"name"name); // Assign string value to the member variable of "name"
    
oo_set(this"age"age); // Assign integer value to the member variable of "age"
}

// A destructor of Animal
public Animal@Dtor()
{
    new 
name[32];
    
oo_get_str(oo_this(), "name"name32); // Get string value from the member variable of "name"
    
server_print("%s has been euthanized."name);
}

public 
Animal@MakeSound(msg[], len)
{
    
// format the message to the msg[]
    
formatex(msglen"I am an animal");
}

public 
Animal@GetLegs()
{
    return 
0;
}

public 
Animal@Introduce()
{
    new 
this oo_this();

    new 
name[32];
    
oo_get_str(this"name"name32);

    new 
age oo_get(this"age"); // Get integer value from the member variable of "age"

    
new legs oo_call(this"GetLegs");

    
// Call "MakeSound" function of this animal and retrieve the result to the msg[]
    
new msg[64];
    
oo_call(this"MakeSound"msgcharsmax(msg));

    
// Print the introduction
    
server_print("Hello, my name is %s, I'm %d years old, I have %d legs, and I say %s"nameagelegsmsg);
}

public 
Animal@Test() { server_print("static method test"); }

public 
Dog@Ctor(age)
{
    
// Call the parent constructor of this dog
    
oo_super_ctor("Animal""Dog"age);
}

public 
Dog@MakeSound(msg[], len)
{
    
formatex(msglen"Woof woof");
}

public 
Dog@GetLegs()
{
    return 
4;
}

public 
Cat@Ctor(age)
{
    
oo_super_ctor("Animal""Cat"age);
}

public 
Cat@MakeSound(msg[], len)
{
    
formatex(msglen"Meow meow");
}

public 
Cat@GetLegs()
{
    return 
4;
}

public 
Bird@Ctor(age)
{
    
oo_super_ctor("Animal""Bird"age);
}

public 
Bird@MakeSound(msg[], len)
{
    
formatex(msglen"Tweet tweet");
}

public 
Bird@GetLegs()
{
    return 
2;
}

public 
Snake@Ctor(age)
{
    
oo_super_ctor("Animal""Snake"age);
}

public 
Snake@MakeSound(msg[], len)
{
    
formatex(msglen"Sss sss");
}

public 
Snake@GetLegs()
{
    return 
0;
}

public 
main()
{
    
register_plugin("[OO] Animal""0.1""holla");

    new 
Animal:animals[5];
    
animals[0] = oo_new("Dog"7);
    
animals[1] = oo_new("Cat"6);
    
animals[2] = oo_new("Bird"4);
    
animals[3] = oo_new("Snake"3);
    
animals[4] = oo_new("Animal""Unknown"0);

    for (new 
05i++)
    {
        
oo_call(animals[i], "Introduce"); // Call Introduce function for each animals
    
}

    
// Tests
    
oo_call(0"Animal@Test"); // Test calling the static method

    
server_print("Object #%d %s a Snake"animals[3], oo_isa(animals[3], "Snake") ? "IS" "IS NOT");
    
server_print("Object #%d %s a Dog"animals[3], oo_isa(animals[3], "Dog") ? "IS" "IS NOT");

    
server_print("Class Dog %s a subclass of Animal"oo_subclass_of("Dog""Animal") ? "IS" "IS NOT");
    
server_print("Class Animal %s a subclass of Cat"oo_subclass_of("Animal""Cat") ? "IS" "IS NOT");

    
server_print("Class Bird %s"oo_class_exists("Bird") ? "EXISTS" "DOES NOT EXIST");
    
server_print("Class Fish %s"oo_class_exists("Fish") ? "EXISTS" "DOES NOT EXIST");

    new class[
32];
    
oo_get_classname(animals[0], class, charsmax(class));
    
server_print("Object #%d's classname is %s"animals[0], class);

    
server_print("Object #%d %s"animals[0], oo_object_exists(animals[0]) ? "EXISTS" "DOES NOT EXIST");

    for (new 
05i++)
    {
        
oo_delete(animals[i]); // Delete each animal objects
    
}

    
server_print("Object #%d %s"animals[0], oo_object_exists(animals[0]) ? "EXISTS" "DOES NOT EXIST");


Outputs:
Code:
Hello, my name is Dog, I'm 7 years old, I have 4 legs, and I say Woof woof
Hello, my name is Cat, I'm 6 years old, I have 4 legs, and I say Meow meow
Hello, my name is Bird, I'm 4 years old, I have 2 legs, and I say Tweet tweet
Hello, my name is Snake, I'm 3 years old, I have 0 legs, and I say Sss sss
Hello, my name is Unknown, I'm 0 years old, I have 0 legs, and I say I am an animal
static method test
Object #1943714116 IS a Snake
Object #1943714116 IS NOT a Dog
Class Dog IS a subclass of Animal
Class Animal IS NOT a subclass of Cat
Class Bird EXISTS
Class Fish DOES NOT EXIST
Object #461123433's classname is Dog
Object #461123433 EXISTS
Dog has been euthanized.
Cat has been euthanized.
Bird has been euthanized.
Snake has been euthanized.
Unknown has been euthanized.
Object #461123433 DOES NOT EXIST

Known issues: (not possible to be fixed)
  • You cannot use default parameter in methods or constructors
  • You cannot use multi-dimensional array in member variables
  • OO_STRING_REF type in method/constructor max length is 255 (unless you initialize the string array with more than 255 non-empty characters)


For a more detailed explanation please read:
https://github.com/hollacs/oo_amxx/b...-std/README.md
__________________
youtube:
@holla16

Last edited by 11922911; 06-28-2024 at 03:29.
11922911 is offline
Roccoxx
AlliedModders Donor
Join Date: Jan 2012
Location: Argentina
Old 09-25-2023 , 15:04   Re: Module: OO (Simulating OOP in AMXXPAWN language)
Reply With Quote #2

Interesting. Good Job!
__________________
Tutorials here (Spanish)

Like as another Pijudo said: "Tired and retired"
Roccoxx is offline
Send a message via MSN to Roccoxx
lexzor
Veteran Member
Join Date: Nov 2020
Old 10-02-2023 , 03:53   Re: Module: OO (Simulating OOP in AMXXPAWN language)
Reply With Quote #3

nice, thanks for sharing
lexzor is offline
11922911
Senior Member
Join Date: Dec 2011
Location: Yuen Long Country
Old 06-28-2024 , 03:22   Re: Module: OO (Simulating OOP in AMXXPAWN language)
Reply With Quote #4

Just added some experimental features since the last commit, but it's still under testing

They are Multiple Inheritance and Hook System

If you wanna test it, you can download the latest build that auto generated by GitHub workflows in the repo's Actions page

The binary files are in the Artifacts section (you have to login to see the downloadable files)
And you also need to update the oo.inc

Bonus:
And I've also made a plugin recently which is based on OO (as a reference to tell people how to use this)
Link: https://github.com/hollacs/Assets-Manager-OO
__________________
youtube:
@holla16

Last edited by 11922911; 06-28-2024 at 04:44.
11922911 is offline
Cristian505
Senior Member
Join Date: Oct 2020
Old 08-20-2024 , 17:26   Re: Module: OO (Simulating OOP in AMXXPAWN language)
Reply With Quote #5

You should change ``oo_`` to ``oop_``
Cristian505 is offline
Destro-
Veteran Member
Join Date: Jun 2010
Location: $me->location();
Old 09-08-2024 , 14:15   Re: Module: OO (Simulating OOP in AMXXPAWN language)
Reply With Quote #6

It's far from being a good object-oriented solution; the correct approach would be something like sourcepawn methodmap. It's a shame; AMX Mod X should never have ignored the compiler improvements driven by sourcepawn.
__________________

Last edited by Destro-; 09-08-2024 at 14:18.
Destro- is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 09-09-2024 , 06:52   Re: Module: OO (Simulating OOP in AMXXPAWN language)
Reply With Quote #7

Quote:
Originally Posted by Destro- View Post
It's far from being a good object-oriented solution; the correct approach would be something like sourcepawn methodmap. It's a shame; AMX Mod X should never have ignored the compiler improvements driven by sourcepawn.
I don't think the current state of activity justifies making sweeping changes to the language.
__________________
HamletEagle is offline
Destro-
Veteran Member
Join Date: Jun 2010
Location: $me->location();
Old 09-09-2024 , 12:21   Re: Module: OO (Simulating OOP in AMXXPAWN language)
Reply With Quote #8

Not now, but I was referring to the past; it's a shame they didn't keep up with SourcePawn when those changes were being made.
__________________

Last edited by Destro-; 09-09-2024 at 12:24.
Destro- 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 09:15.


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