[Tutorial] [Advanced]How to mimic Object-Oriented Programming in PAWN
#12

Hi, I used to play SA-MP 6+ years ago and am thinking of getting back into it + the scripting side. In my years since SA-MP i have coded entirely OOPy using mainly PHP, Java or Ruby, so it would be nice to have some sort of (limited) OOPiness in Pawn. I am also thinking about just using the Java plugin to code in.

I tried to make an OOP style using the define directives, but they're also kind of limited (or my knowledge is, I haven't touched Pawn in 6 years as I say). Anyway, if I could have your view on this code I would appreciate it.

PHP код:
#include <a_samp>
const MAX_STRING 256;
new 
lastId;
stock _getNewClassObjectId(objectIds[], size) {
    for(
lastId 0lastId sizelastId++) {
        if(!
objectIds[lastId]) {
            
objectIds[lastId] = true;
            return 
lastId;
        }
    }
    
#if defined ThrowError
        
new string[MAX_STRING];
        
format(stringsizeof string"Ran out of objects, max: %d"size);
        
ThrowError(string);
    
#endif
 
return -1;
}
stock _countInstances(objectIds[], size) {
 new 
counter 0;
    for(new 
0sizei++) {
        if(
objectIds[i]) {
   
counter++;
  }
 }
 return 
counter;
}
stock _getInstance(objectIds[], sizenumber) {
 new 
counter 0;
    for(new 
0sizei++) {
        if(
objectIds[i]) {
            if(
counter == number) {
                return 
i;
            }
   
counter++;
  }
 }
 return -
1;
}
#define class(%0,%1) \
 
stock _%0_tag_check(%0:objid) { return int:objid; } \
 new 
bool:_%0_objects[%1] = false
#define delete(%0,%1) _%0_objects[_%0_tag_check(%1)] = false
#define method(%0).%1(%2) _%0_%1(%0:this,%2)
#define methodb(%0).%1()  _%0_%1(%0:this)
#define var(%0){%1} _%0_%1[sizeof _%0_objects]
#define val(%0){%1} _%0_%1[_%0_tag_check(this)]
#define callb(%0,%1).%2()  _%0_%2(%1)
#define call(%0,%1).%2(%3) _%0_%2(%1, %3)
#define object(%0) %0:_getNewClassObjectId(_%0_objects, sizeof _%0_objects)
#define instances(%0) _countInstances(_%0_objects, sizeof _%0_objects)
#define instance(%0,%1) %0:_getInstance(_%0_objects, sizeof _%0_objects, %1)
class(User100);
new var(
User){name}[MAX_STRING];
method(User).init(name[]) {
 for(new 
0strlen(name); i++) {
     
val(User){name}[i] = name[i];
 }
}
methodb(User).getName() {
 return 
val(User){name};
}
main()
{
 new 
User:object(User);
 
printf("User id: %d"int:u);
 
call(Useru).init("foo bar");
 print(
callb(Useru).getName());
 
printf("User instances: %d"instances(User)); // 1
 
delete(Useru);
 
printf("User instances: %d"instances(User)); // 0

Reply


Messages In This Thread
[Advanced]How to mimic Object-Oriented Programming in PAWN - by Sasino97 - 13.12.2015, 14:11
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by HydraHumza - 13.12.2015, 14:14
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by vassilis - 13.12.2015, 15:00
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by Sasino97 - 13.12.2015, 16:21
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by Yashas - 13.12.2015, 17:02
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by N0FeaR - 13.12.2015, 21:28
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by Sasino97 - 26.01.2016, 20:55
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by KillerDVX - 27.01.2016, 11:27
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by SystemX - 30.01.2016, 17:11
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by NewbProgrammer - 31.01.2016, 05:59
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by Sasino97 - 02.02.2016, 11:23
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by Scranton - 18.08.2016, 12:29
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by Eoussama - 07.11.2017, 18:31
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by Xeon™ - 07.11.2017, 19:21
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by 0x88 - 04.12.2017, 10:39
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by SammyJ - 04.12.2017, 11:08
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by Kar - 07.12.2017, 04:29
Re: [Advanced]How to mimic Object-Oriented Programming in PAWN - by BiosMarcel - 07.12.2017, 14:31

Forum Jump:


Users browsing this thread: 1 Guest(s)