AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   New API and Syntax (https://forums.alliedmods.net/showthread.php?t=244092)

BAILOPAN 07-13-2014 15:36

New API and Syntax
 
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.

Zephyrus 07-13-2014 15:52

Re: New API and Syntax
 
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...

VoiDeD 07-13-2014 15:57

Re: New API and Syntax
 
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 );



Dr. Greg House 07-13-2014 16:10

Re: New API and Syntax
 
If I may keep it short.
<---------------------------

psychonic 07-13-2014 16:17

Re: New API and Syntax
 
Quote:

Originally Posted by BAILOPAN (Post 2167095)
All of the changes are opt-in, and old scripts will continue to compile.

I feel that this should be echoed.

Dr. Greg House 07-13-2014 16:34

Re: New API and Syntax
 
Quote:

Originally Posted by psychonic (Post 2167112)
I feel that this should be echoed.

I think this needs color, bigger font, and a big arrow.

Arkarr 07-13-2014 16:40

Re: New API and Syntax
 
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 ?

Zephyrus 07-13-2014 16:41

Re: New API and Syntax
 
Quote:

Originally Posted by Arkarr (Post 2167124)
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

Dr. Greg House 07-13-2014 16:42

Re: New API and Syntax
 
Quote:

Originally Posted by Arkarr (Post 2167124)
Totally not okay with that[...]

Because it is actually a char-array and not a string-array.

Arkarr 07-13-2014 16:45

Re: New API and Syntax
 
Nevermind, thanks.


All times are GMT -4. The time now is 03:28.

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