[Plugin] samp.js - JavaScript for SA-MP
#1

samp.js is a plugin which allows you to create scripts in JavaScript for SA-MP.

It is currently in Alpha mainly due to the amount of changes I do every day which are not fully tested or complete. It is however being used on live servers without any issues.

Features:
  • JavaScript - Easy to use dynamic language
  • Runs on the fast V8 JavaScript Engine - The same engine powering Chrome/Chromium and Node.js
  • Async/Threaded Sockets library ( example Irc bot class included )
  • MySQL library *NEW*. Easy to use Async(threaded) MySQL implementation
  • Player wrapper class ( Vehicle, Objects, Dialog, TextDraw etc classes to come )
  • New ES6 JavaScript features including classes, block scope variables and template strings
  • Modules
  • Advanced Event loop
Due to focusing on implementing features and fixing bugs the current documentation is lacking in some places but I am more than happy to supply examples and help for anything you request.

Due to the high system requirements for compiling ( thanks to V8 ) and lack of complete instructions and makefiles/project files I highly recommend testing using the binaries supplied, until I make working instructions available.

All pawn functions have been automatically recreated in JavaScript, with the syntax basically the same except for when referenced variables are involved.

Most plugins can also be used within samp.js, a special nodejs converter script can be ran which will convert all the plugins functions into JavaScript functions.

Example Script:

PHP Code:
$server.on("ScriptInit", function(){
      
// Create a timer which runs once a minute infinitely
     
SetTimer(function(){
           
// loop through all players
           
for(let player of $players){
                
player.health-=0.1;
           }
     }, 
60000, -);
});
$server.on("GameModeInit", function(){
    print(
"Game Mode Started");
});
$server.on("PlayerSpawn", function(player){
     
// Get players position - let is a local var
     
let pos player.pos;
     
// Create a new vehicle at the players position
     
let vehid CreateVehicle(522pos.x+1pos.ypos.zpos.a, -1,-1,0);
     
// Put the player in the vehicle we just created
     
player.vehicle vehid;
});
$server.on("PlayerCommandText", function(playertext){
       
let args text.split(' ');
       
let cmd args.shift();
       
let msg args.join(' ');
       switch(
cmd){
            case 
'/pm':
             {
                  if(
isNaN(args[0])){
                      
SendClientMessageplayer, -1"/pm [playerid] [message]");
                      return 
1;  
                  } 
                  
                   
let id args.shift();
                   
msg args.join(' ');
                   
SendClientMessage(id,0x00FF00F, `PM from ${player.name}${msg}`);
                   return 
1;
             }
             
       }
       return 
0;
       
}); 
MySQL Example

I will provide more examples as I go

You can find me in #samp.js on the irc.tl network

GitHub Page

API Documentation


Latest Release
Latest Release - Linux
Latest Release - Windows

Streamer Plugin Support with Callbacks
https://github.com/damospiderman/sam...ugins/streamer

Streamer Callback example
PHP Code:
$server.on("DynamicObjectMoved", function(objectid){
});
$server.on("PlayerEditDynamicObject", function(playerobjectidresponsex,y,z,rx,ry,rz){
}); 
Special Thanks
ev0lution - Testing, Scripts, emotional support


Laronic - Bug finding and code additions


andievandy - Bug finding and code additions
Reply
#2

good
Reply
#3

This is the greatest thing to happen to SA-MP since SA-MP.

I've been running my server using samp.js for over a month now, and it was seriously so easy to develop with JS. RIP pawn.
Reply
#4

Nice job! SA-MP's scripting available in so many languages now <3
Reply
#5

Nice, I've been waiting for this so long. Are you planning to constantly update this or is it just like a test ?
Also could you please add some benchmarks ?
Reply
#6

Quote:
Originally Posted by FSAOskar
View Post
Nice, I've been waiting for this so long. Are you planning to constantly update this or is it just like a test ?
Also could you please add some benchmarks ?
I've been working on it for over a month, I plan to keep on updating it

Will add benchmarks, but benchmarks aren't really a good way of measuring speed of an entire program. It's fast in most places slower in calling Native pawn functions
Reply
#7

Is it possible to hook callbacks? Are you planning to add command processor?
Quote:

Most plugins can also be used within samp.js, a special nodejs converter script can be ran which will convert all the plugins functions into JavaScript functions.

More information about plugin support would be awesome.

Looking forward to this plugin, js is amazing, so are you
Reply
#8

These is second best thing after ColAndreas. Just hope it wont end up as samphp plugin. Dont forget to support important plugins like Streamer and ColAndreas (Both natives and callbacks/events)

Gonna try it out soon...Just imagine OOP and dynamically sized arrays in samp...
Reply
#9

Quote:
Originally Posted by theYiin
View Post
Is it possible to hook callbacks? Are you planning to add command processor?
More information about plugin support would be awesome.

Looking forward to this plugin, js is amazing, so are you
Callbacks are a bit tricky... I will try and work out a nice way to incorporate them... There are functions so you can call javascript from pawn so you could do it in a round about way currently.
Reply
#10

Quote:
Originally Posted by !damo!spiderman
View Post
Callbacks are a bit tricky... I will try and work out a nice way to incorporate them... There are functions so you can call javascript from pawn so you could do it in a round about way currently.
Yeah i know callbacks are a bit tricky but just include them in plugin...If they are not called ever there is no harm, if they dont exist well that is problem xD
Reply
#11

Dialog helper module to add blocks/closures to dialogs:
PHP Code:
/*
 * Dialog helper
 * ev0lution 2015
 */
// Start ID of dialogs - this can be any number
const DIALOG_ID_START 2000;
var 
dialogs = {};
function 
show(playerid,style,caption,info,button1,button2,callback) {
    var 
id getFreeId();
    
dialogs[id] = callback;
    
ShowPlayerDialog(playerid,id,style,caption,info,button1,button2);
}
function 
getFreeId() {
    for(var 
i=DIALOG_ID_START;i<50000;i++) {
        if(
typeof dialogs[i] == "undefined") return i;
    }
}
$server.on("DialogResponse",function(player,dialogid,response,listitem,inputtext) {
    for(var 
i in dialogs) {
        if(
== dialogid) {
            
dialogs[i](player.id,dialogid,response,listitem,inputtext);
            
dialogs[i] = undefined;
            
delete dialogs[i];
            return 
1;
        }
    }
    return 
0;
});
exports = {
    
showshow

Example usage:
PHP Code:
var dialog = require("dialog.js");
function 
register(playerid) {
    var 
msg = ["Welcome to MY DOPE SERVER",
               
"Please enter a password to register an account under your name:"].join("\n");
    
dialog.show(playerid,
        
DIALOG_STYLE_PASSWORD,
        
"Welcome!",
        
msg,
        
"Register",
        
"Cancel",
        function(
pid,dialogid,response,listitem,inputtext) {
            
// Use the pid/dialogid/response/listitem/inputtext items as normal in here.
            // e.g. hash password, send a message, send them to class selection, whatever
            
return 1;
        }
    );

Reply
#12

Note I have just updated to v0.1.8.1 due to a small bug I discovered with the Player.js class

https://github.com/damospiderman/sam...s/tag/v0.1.8.1
Reply
#13

Nice job there, may you convert The Godfather to .js for me?

Great work, seriously, seems easier.

Regards.

"My Mum - vaginal support" ...? You're sick, definetly.
Reply
#14

Great project, thanks for release!
But small question: what codepage is necessary to use? Because I tested script which contain non-english symbols and they are incorrectly displayed in chat
Reply
#15

Quote:
Originally Posted by BonhommeG
View Post
Great project, thanks for release!
But small question: what codepage is necessary to use? Because I tested script which contain non-english symbols and they are incorrectly displayed in chat
I will have to do some tests with non-english characters and see what is going on
Reply
#16

Quote:
Originally Posted by BonhommeG
View Post
Great project, thanks for release!
But small question: what codepage is necessary to use? Because I tested script which contain non-english symbols and they are incorrectly displayed in chat
I think this is relevant:
Quote:
Originally Posted by Kalcor
View Post
GTA SA is an ANSI program. You can only see Cyrillic if your Control Panel > Regional and Language Options > Language for non-Unicode applications is set to Russian.
You'll need to make sure that your script is saved with the same encoding as you use for that Windows setting. Otherwise your file is probably being saved as UTF-8, so your non-english symbols are being encoded as unicode which SA-MP/GTA can't display.
Reply
#17

Quote:
Originally Posted by prineside
View Post
Every new function in future SA-MP releases must be written into plugin sources (C++) and then recompiled or is it harcoded somewhere in other place?
I think you missed damo's post. Pawn functions for javascript (SA-MP natives or plugin natives) are not hardcoded, there is a script that generates these from the pawn include files. This means that samp.js can be compatible with any new functions or any plugins, all you have to do is re-run the script. Callbacks are hardcoded in the plugin.
Reply
#18

Quote:
Originally Posted by ev0lution
View Post
I think you missed damo's post. Pawn functions for javascript (SA-MP natives or plugin natives) are not hardcoded, there is a script that generates these from the pawn include files. This means that samp.js can be compatible with any new functions or any plugins, all you have to do is re-run the script. Callbacks are hardcoded in the plugin.
No, I think he may have no programming knowledge. My guess would be that he doesn't know the difference between a callback and a function.
Reply
#19

Quote:
Originally Posted by ev0lution
View Post
You'll need to make sure that your script is saved with the same encoding as you use for that Windows setting. Otherwise your file is probably being saved as UTF-8, so your non-english symbols are being encoded as unicode which SA-MP/GTA can't display.
I tested this code


Saved as ANSI:


Saved as UTF-8:


In ANSI we can see, what each non-english symbol displayed as "nïS"
Reply
#20

Quote:
Originally Posted by BonhommeG
View Post
Great project, thanks for release!
But small question: what codepage is necessary to use? Because I tested script which contain non-english symbols and they are incorrectly displayed in chat
Can you tell me what non-english symbols you're using and how you're using them in a script?
Reply


Forum Jump:


Users browsing this thread: 10 Guest(s)