Raised This Month: $12 Target: $400
 3% 

New API and Syntax


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
BAILOPAN
Join Date: Jan 2004
Old 07-13-2014 , 15:36   New API and Syntax
Reply With Quote #1

Hi, everyone! SourceMod 1.7 development is underway, and I'm excited to announce that SourcePawn is undergoing radical changes. The API is becoming object-oriented, and the syntax is becoming more C#/Java-like. All of the changes are opt-in, and old scripts will continue to compile.

Without further ado...

Motivation

Pawn is derived from "Small C", which was made in 1984. It has a number of severe syntactic and semantic flaws compared to modern languages, and it's become quite painful to use. All of its problems can be ultimately blamed on the tag system. The tag system erases type information, which prevents us from implementing garbage collection, which prevents us from implementing better datastructures, true arrays, true strings, objects, and lambda functions.

Our ultimate goal is for Pawn to become a "C# Lite", but getting there is difficult: we have to rewrite the compiler, the JIT, and our entire API. That's an insane amount of work, so we're taking a new approach by introducing transitional syntax, transitional APIs, and a transitional type system.

Hopefully we're killing two birds with one stone: we'll make a major dent in the tag system, put foundations in place for our future API, and provide some major short-term improvements to Pawn usability.

Syntax Changes
As of SourcePawn 1.7, there is a new preferred way of declaring variables and function signatures. Fans of Java/C# will find it much more familiar. Here are some before and after examples:

PHP Code:
// Before:
stock bool:IsUserACrab(client);

// After:
stock bool IsUserACrab(int client);

// Before:
public OnReceivedString(const String:name[], Float:fval);

// After:
public void OnReceivedString(const char[] namefloat fval);

// Before:
new Handle:PlayerHandles[MAXPLAYERS 1];

// After:
Handle PlayerHandles[MAXPLAYERS 1]; 
New-style declarations allow array dimensions to come either before or after the variable name. Empty dimensions should come before, and fixed-lengths should come after.

The new style is important for a few reasons. First, it visually removes the concept of tags. Second, it formalizes int as an actual type, replacing "cells" (which were part of the tag system). Third, it formalizes void, which will make it easier to implement modules in the future. Finally, it gives us a chance to replace old type names with better ones. For example, String is not only misnamed, but will be confusing when we have a true string type.

The new type names are int, float, char, void, and object, and are only available in new-style declarations. They cannot be used as tags.

Methodmaps

SourcePawn 1.7 has a new feature called "methodmaps" for creating object-oriented APIs. Most users will not be creating their own methodmaps, so I defer to the full documentation for the gory details. The idea is that you can now attach functions to tags.

For example:

PHP Code:
enum Player:
{
    
SERVER_PLAYER 0,
}

methodmap Player
{
    public 
void Kick(const char[] message) {
        
KickClent(this.Index"%s"message);
    }
    
property int Health {
        public 
get() {
            return 
GetClientHealth(this.Index);
        }
    }
    
property int Index {
        public 
get() {
            return 
int(this);
        }
    }

This methodmap lets us do things like:
PHP Code:
void CheckPlayerHealth(Player player)
{
    if (
player.Health 5)
        
player.Kick("Wow, you are very bad at this game.");


Object-Oriented API

We've ported adt_trie, adt_array, and menus. For example, creating a simple menu beforehand might have looked something like this:

PHP Code:
new Handle:menu CreateMenu(Menu_Handler)
AddMenuItem(menu"de_dust""Dust")
AddMenuItem(menu"cs_assault""Assault")
AddMenuItem(menu"cs_thunder""Thunder")
SetMenuExitButton(menutrue)
DisplayMenu(menuclient)
...
CloseHandle(menu); 
Now, you can write it like this:
PHP Code:
Menu menu Menu(MenuHandler)
menu.AddItem("de_dust""Dust")
menu.AddItem("cs_assault""Assault")
menu.AddItem("cs_thunder""Thunder")
menu.ExitButton true
menu
.Display(client)
...
delete menu 
Future Work

Most of our API has not yet been transitioned to methodmaps. I am hoping we'll be able to release 1.7 with a full object-oriented API, so if you want to use something in the includes that has not yet been ported, feel free to submit a patch!

We may also consider an automatic porting tool for plugins once enough of the API is available.

The full documentation for the transitional syntax is here: https://wiki.alliedmods.net/SourcePa...itional_Syntax

Please let me know if you have any questions.
__________________
egg

Last edited by BAILOPAN; 07-14-2014 at 03:51.
BAILOPAN is offline
Zephyrus
Cool Pig B)
Join Date: Jun 2010
Location: Hungary
Old 07-13-2014 , 15:52   Re: New API and Syntax
Reply With Quote #2

why, just why? at least before it was a language that was used by some other stuff like samp, but now this just makes it a new language to learn for a lot of ppl. javascript would be a better choice overall, as its the most widespread scripting language with the fastest JITs out there. also countless libraries have been written in js already...
__________________
Taking private C++/PHP/SourcePawn requests, PM me.
Zephyrus is offline
VoiDeD
AlliedModders Donor
Join Date: Mar 2009
Location: Illinois, USA
Old 07-13-2014 , 15:57   Re: New API and Syntax
Reply With Quote #3

For developers looking to harness the methodmap syntax for an object-oriented API for interacting with game entities, check out these helpers includes.

Example of usage:
PHP Code:
stock void GiveObjectHatCBaseObject objectEnt, const char hatModelPLATFORM_MAX_PATH ] )
{
    
CBaseEntity hatProp Entity_CreateByName"prop_dynamic_override" );

    if ( 
hatProp.IsValid )
    {
        
hatProp.SetModelhatModel );
        
hatProp.Spawn();

        
hatProp.AcceptInput"DisableCollision" );
        
hatProp.AcceptInput"DisableShadow" );

        
ParentHathatPropobjectEnt );
    }
}

stock void ParentHatCBaseEntity hatPropCBaseObject objectEnt )
{
    
char hatModelPLATFORM_MAX_PATH ];
    
hatProp.GetModelhatModelsizeofhatModel ) );

    
float modelScale 1.0;
    
float modelOffset 0.0;

    if ( !
Config_GetHatByModelhatModelmodelOffsetmodelScale ) )
    {
        
LogError"Unable to find hat config for hat: %s"hatModel );
        return;
    }

    
hatProp.Skin objectEnt.Builder.Team 2;
    
hatProp.ModelScale modelScale;

    
char attachmentName128 ];
    
GetAttachmentNameobjectEntattachmentNamesizeofattachmentName ) );

    
hatProp.SetParentobjectEnt );
    
hatProp.SetParentAttachmentattachmentName );

    
float vecPos]; float angRot];
    
hatProp.GetLocalOriginvecPos );
    
hatProp.GetLocalAnglesangRot );

    
// apply z offset
    
vecPos] += modelOffset;

    
// apply position/angle fixes based on object type
    
OffsetAttachmentPositionobjectEntvecPosangRot );

    
hatProp.TeleportvecPosangRotNULL_VECTOR );

__________________
VoiDeD is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 07-13-2014 , 16:10   Re: New API and Syntax
Reply With Quote #4

If I may keep it short.
<---------------------------
__________________
Santa or Satan?

Watch out when you're paying people for private requests! Most stuff already exists and you can hardly assess the quality of what you'll get, and if it's worth the money.
Dr. Greg House is offline
psychonic

BAFFLED
Join Date: May 2008
Old 07-13-2014 , 16:17   Re: New API and Syntax
Reply With Quote #5

Quote:
Originally Posted by BAILOPAN View Post
All of the changes are opt-in, and old scripts will continue to compile.
I feel that this should be echoed.
psychonic is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 07-13-2014 , 16:34   Re: New API and Syntax
Reply With Quote #6

Quote:
Originally Posted by psychonic View Post
I feel that this should be echoed.
I think this needs color, bigger font, and a big arrow.
__________________
Santa or Satan?

Watch out when you're paying people for private requests! Most stuff already exists and you can hardly assess the quality of what you'll get, and if it's worth the money.
Dr. Greg House is offline
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 07-13-2014 , 16:40   Re: New API and Syntax
Reply With Quote #7

Totally not okay with that :
Code:
// Before:
public OnReceivedString(const String:name[], Float:fval);

// After:
public void OnReceivedString(const char[] name, float fval);
Why 'char' instend of String ?
__________________
Arkarr is offline
Zephyrus
Cool Pig B)
Join Date: Jun 2010
Location: Hungary
Old 07-13-2014 , 16:41   Re: New API and Syntax
Reply With Quote #8

Quote:
Originally Posted by Arkarr View Post
Totally not okay with that :
Code:
// Before:
public OnReceivedString(const String:name[], Float:fval);

// After:
public void OnReceivedString(const char[] name, float fval);
Why 'char' instend of String ?
because if its a string you shouldnt need to use [] in the first place
__________________
Taking private C++/PHP/SourcePawn requests, PM me.
Zephyrus is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 07-13-2014 , 16:42   Re: New API and Syntax
Reply With Quote #9

Quote:
Originally Posted by Arkarr View Post
Totally not okay with that[...]
Because it is actually a char-array and not a string-array.
__________________
Santa or Satan?

Watch out when you're paying people for private requests! Most stuff already exists and you can hardly assess the quality of what you'll get, and if it's worth the money.
Dr. Greg House is offline
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 07-13-2014 , 16:45   Re: New API and Syntax
Reply With Quote #10

Nevermind, thanks.
__________________

Last edited by Arkarr; 07-13-2014 at 17:18. Reason: new reply
Arkarr 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 18:47.


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