SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Discussion

Reply
 
Thread Tools Display Modes
Old 17/07/2012, 05:19 PM   #11
Slice
High-roller
 
Slice's Avatar
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,858
Reputation: 1234
Default Re: Classes (OO)

Well, if you did use a generic tag for instances instead of the class it would solve some problems.

For example, you could create operators for assigning a value tagged "Instance:" to both floats and ints seamlessly.

The only downside would be functions can't be strict on what type of instance they want.
__________________
Do you like SA-MP and DayZ? Come play both: ulclan.com:1421

Do you use any scripts I've made?
Star them on GitHub or reply in their topics so I know what I should keep working on!
Slice is offline   Reply With Quote
Old 17/07/2012, 05:39 PM   #12
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 17,962
Reputation: 2496
Default Re: Classes (OO)

That is a very good thought!
Y_Less is offline   Reply With Quote
Old 19/07/2012, 05:55 PM   #13
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,451
Reputation: 979
Default Re: Classes (OO)

I'm finally learning about OOP (It's quite simple... no idea why I avoided it before assuming it was complex)

I just want to confirm a bit how this would be used in Pawn/SA:MP.

So for instance, I have some simple Create/Destroy script (Like objects (models, not OOP objects)) would it go something like this:


pawn Code:
// Question: Where would this be declared/initiated?
// Could it just be intiated outside of code or would it have to be in a function of some sort
// Like main() or OnGamemode/FilterScriptInit?

class Item()
{
    new
        Float:iPosX,
        Float:iPosZ,
        Float:iPosY,
        iState,
        iSize;     

    method setPos(Float:x, Float:y, Float:z)
    {
        if(z > 100)return print("Too high");
        iPosX = x;
        iPosY = y;
        iPosZ = z;
        return 1; // I assume returns would work as expected
    }
   
    method setState(state)
    {
        if(state == 0 || state == 1 || state == 2 || state == 3)
        {
            iState = state;
        }
        else print("State error");
    }
   
    method setSize(size)
    {
        if(2 < size < 10)iSize = size;
        print("Size error");
    }
}

public OnFilterScriptInit()
{
    new Item:myItem = Item();

    myItem _ setPos(1.0, 2.0, 5.0);
    myItem _ setState(5); // Would print an error
    myItem _ setSize(7);
}

// What about inheriting? Is that possible or just out of the question?

class ItemVersion2 extends Item()
{
    new
        bool:isFlying,
        bool:isSwimming,
        bool:isRunning;
   
    method setRun()
    {
        isRunning = !isRunning;
    }
    // etc...
}

public OnSomething()
{
    new ItemVersion2:mySecondItem = ItemVersion2();
   
    mySecondItem _ setPos(2.0, 2.0, 4.0); // From 'Item'
    mySecondItem _ setRun();
   
    // Also, is direct variable changing possible?
    // Maybe not for the inherited variables, just in general
   
    mySecondItem _ isFlying = true;
}

Sorry for all the questions, I'm probably just thinking too far out the box! I'm kind of new to OOP and I'm not entirely sure how you do these things! But for all I know it could be possible, you've done some pretty good things with Pawn that I would never have thought to be possible


And I'm glad I understand how OOP/Classes work now, I can appreciate this even more!
[HLF]Southclaw is online now   Reply With Quote
Old 19/07/2012, 06:17 PM   #14
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 17,962
Reputation: 2496
Default Re: Classes (OO)

Yes, but there's no "extends" currently - I can't think of a way to do that at all. The closest there is would be membership instead.
Y_Less is offline   Reply With Quote
Old 03/10/2012, 01:12 PM   #15
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 17,962
Reputation: 2496
Default Re: Classes (OO)

I'm moving some files about so I thought I would post what little code I had written ages ago here:

pawn Code:
// VERY EXPERIMENTAL AND INCOMPLETE CODE!  DO NOT USE CURRENTLY!

#include "y_malloc"
#include "y_inline"

#define method%0(%1) OO_Add(#%0);inline%0(%1)

enum E_OO_CLASS_DATA
{
    E_OO_CLASS_DATA_NAME[32 char],
    //E_OO_CLASS_DATA_METHOD_START,
    E_OO_CLASS_DATA_SIZE
}

enum E_OO_METHOD_DATA
{
    //E_OO_METHOD_DATA_NAME[32 char],
    //E_OO_METHOD_DATA_SPECIFIER,
    E_OO_METHOD_DATA_PARENT,
    // Point to the next method with the same name.  Methods can NEVER be
    // removed (they're static compiled code), so we can just loop through the
    // list linerarly.
    E_OO_METHOD_DATA_SAME,
    // Store the y_inline data locally.
    E_OO_METHOD_DATA_INLINE[E_CALLBACK_DATA]
    //E_OO_METHOD_DATA_PTR
}

#define MAX_OO_METHODS (16)
#define MAX_OO_CLASSES (16)

static stock
    // Temporary storage to put stack variables in.
    YSI_gsMethods,
    YSI_gsStack[256],
    YSI_gsClassData[MAX_OO_CLASSES][E_OO_CLASS_DATA],
    YSI_gsMethodData[MAX_OO_CLASSES * MAX_OO_METHODS][E_OO_METHOD_DATA];

stock OO_StoreParams(GLOBAL_TAG_TYPES:...)
{
    // This function was surprisingly easy to write!  As was the required
    // y_inline "Callback_Array" function - I added just ONE "LOAD.I", but now
    // public functions don't work through that system (not that I really care -
    // they make no sense in OO anyway).
    new
        num = numargs() - 1,
        from,
        to;
    #emit LCTRL      5
    #emit ADD.C      12
    #emit STOR.S.pri from
    #emit CONST.pri  YSI_gsStack
    #emit STOR.S.pri to
    while (num--)
    {
        #emit LREF.S.pri from
        #emit SREF.S.pri to
        from += 4;
        to += 4;
    }
}

static stock OO_GetClass(that)
{
    return 0;
}

stock operator-(CTest:that, __Methods:matches)
{
    new
        cls = OO_GetClass(that);
    while (matches != __Methods:-1)
    {
        if (YSI_gsMethodData[_:matches][E_OO_METHOD_DATA_PARENT] == cls)
        {
            // Found the correct matching function name.
            // TODO: Code rewriting.
            Callback_Array(YSI_gsMethodData[_:matches][E_OO_METHOD_DATA_INLINE], YSI_gsStack);
            return 0;
        }
        matches = __Methods:YSI_gsMethodData[_:matches][E_OO_METHOD_DATA_SAME];
    }
    // Didn't find the correct method.
    return cellmin;
}

//stock OO_

stock OO_Add(const string:method[])
{
    new
        result[E_CALLBACK_DATA];
    //if (Callback_Get(callback_tag:method, YSI_gsMethodData[YSI_gsMethods][E_OO_METHOD_DATA_INLINE])
    if (Callback_Get(callback_tag:method, result))
    {
        printf("Found method %s", method);
    }
}



CTest:CTest()
{
    for (new __memory = 0, __loop; ; ++__loop)
    {
        if (__loop)
        {
            // Allocate the data.
            return;
        }
        else
        {
            for (new CTest:__this, __padding[5]; !__this; ++__this)
            {
                // This is where the main constructor code goes.
                new
                    mVar1 = 10,
                    mVar2 = 20,
                    mVar3 = 30;
               
                static
                    smInAllClasses = 40;
               
                method AddOne()
                {
                    mVar1 += 1;
                }
               
                method AddTwo()
                {
                    mVar2 += 2;
                }
                printf("loop");
            }
        }
    }
}

/*
#define _DEBUG 7

#include <a_samp>
#include <YSI\y_malloc>
#include <YSI\y_inline>
#include <YSI\y_oo>

Get(callback:callback, ...)
{
    new
        result[E_CALLBACK_DATA],
        arr[] = {44, 45, 46},
        num = numargs() - 1,
        tmp,
        addr;
    #emit LCTRL      5
    #emit ADD.C      16
    #emit STOR.S.pri tmp
    #emit ADDR.pri   arr
    #emit STOR.S.pri addr
    while (num--)
    {
        #emit LREF.S.pri tmp
        #emit SREF.S.pri addr
        tmp += 4;
        addr += 4;
    }
    Callback_Get(callback, result);
    Callback_Call(result, 42, 43, 44);
    Callback_Array(result, arr);
}

main()
{
    print("\n----------------------------------");
    print(" Blank Gamemode by your name here");
    print("----------------------------------\n");
    inline Hello(a, b, c)
    {
        printf("%d %d %d", a, b, c);
    }
    Get(using inline Hello, 77, 78, 79);
    CTest();
}
*/

The commented stuff at the end (IIRC) does actually work, but I can't remember what I was doing at the minute. The "CTest" "class" is currently expanded and contains the code that the macros should end up with.
Y_Less is offline   Reply With Quote
Old 04/02/2013, 09:21 AM   #16
Gamer_Z
High-roller
 
Gamer_Z's Avatar
 
Join Date: Jun 2009
Location: Fontys Uni. - Applied Sciences (Currently doing the hardest possible study Fontys has to offer)
Posts: 2,639
Reputation: 841
Default Re: Classes (OO)

this stuff is very cool, but isn't it -by the time you create all this - easier/faster to just make a custom compiler which generates sa-mp compatible output(amx) (while maintaining compatibility with current scripts)?

SA-MP won't change the pawn version anymore, At least I think so.

I have a failed attempt at a custom pre-processor.. but it's not a compiler so there is a difference.
__________________

New Site (http://gz0.nl)
Google code not working? Try GitHub
Gamer_Z is offline   Reply With Quote
Old 04/02/2013, 10:27 AM   #17
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 17,962
Reputation: 2496
Default Re: Classes (OO)

A compiler is A LOT of work - serious amounts! I would have liked to have written an LLVM front/back end for PAWN/P-code, but don't have the time.
Y_Less is offline   Reply With Quote
Old 04/02/2013, 10:39 AM   #18
Gamer_Z
High-roller
 
Gamer_Z's Avatar
 
Join Date: Jun 2009
Location: Fontys Uni. - Applied Sciences (Currently doing the hardest possible study Fontys has to offer)
Posts: 2,639
Reputation: 841
Default Re: Classes (OO)

Quote:
Originally Posted by Y_Less View Post
A compiler is A LOT of work - serious amounts! I would have liked to have written an LLVM front/back end for PAWN/P-code, but don't have the time.
Hmm my words sounded a bit too big I think, I had in mind modifing the current pawn compiler :P
I read about LLVM on wiki but I still don't understand it. What's it all about? Standalone compiler or something that generates some output and passes the remaining stuff onto the original compiler?
__________________

New Site (http://gz0.nl)
Google code not working? Try GitHub
Gamer_Z is offline   Reply With Quote
Old 04/02/2013, 10:52 AM   #19
Blackazur
High-roller
 
Blackazur's Avatar
 
Join Date: Apr 2012
Posts: 1,573
Reputation: 0
Default AW: Classes (OO)

That is nice.
__________________




Zombie Escape [0.3z]
178.33.168.73:7777 - www.zom-ftw.tk
Join us!
Blackazur is offline   Reply With Quote
Old 04/02/2013, 10:59 AM   #20
Meta
Gangsta
 
Meta's Avatar
 
Join Date: Feb 2010
Location: Germany
Posts: 648
Reputation: 44
Default Re: Classes (OO)

This looks nice
Meta is offline   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Certain Classes? jameskmonger Help Archive 3 13/04/2010 08:48 PM
I need Classes XxJoexX Help Archive 17 14/03/2010 05:30 PM
[HELP]Classes txraptor Help Archive 0 29/12/2009 03:11 PM
ysf and npc classes Jason_Borne420 Help Archive 1 17/10/2009 11:01 PM


All times are GMT. The time now is 09:29 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.