Sign in to follow this  
Followers 0
kemperrr

Local Chat / RP chat (with example)

10 posts in this topic

ATTENTION: topic for those who have already studied JS!

I am using an example from the last tutorial!

Today we look at how to make a local chat

Create a new file named utility.js and put the given code

"use strict";

let Utility = module.exports;

//Creates a Sphere object
Utility.sphere = class Sphere { 

    constructor(x, y, z, opt_radius) {
	    this.x = x;
	    this.y = y;
	    this.z = z;
	    this.radius = opt_radius || 1;
    }

};
//add checking position
Utility.sphere.prototype.inRangeOfPoint = function(position) { 

	return (Math.pow((position.x - this.x), 2) +
            Math.pow((position.y - this.y), 2) +
            Math.pow((position.z - this.z), 2) < Math.pow(this.radius, 2));
}

Utility.PlayerToPoint = (range, player, x, y, z) => {

    let sphere = new Utility.sphere(x, y, z, range)
		
    return sphere.inRangeOfPoint(player.position);
};

Utility.proximityMessage = function(radi, sender, message) { //function local chat
		mp.players.forEach(_player => {
			if(Utility.PlayerToPoint(radi, _player, sender.position.x, sender.position.y, sender.position.z)/* && receiver.dimension == sender.dimension*/) {
				_player.outputChatBox(message);
			}
		});
};

 

Now let's add a variable that stores utility.js.
After var mysql = require ( 'mysql'); add:

global.gm = {};
gm.utility = require('./utility.js');

Now replace in the event: playerChat

mp.players.forEach(_player => { _player.outputChatBox(str); });

on

gm.utility.proximityMessage(25.0, player, str)

 

Nothing more, thank you

Full codehttps://www.dropbox.com/s/we4e6ffswbw3yfc/gm in forum2.zip?dl=0

Edited by kemperrr
6 people like this

Share this post


Link to post
Share on other sites
2 минуты назад, MasterZero сказал:

Nice tutorial for beginnners! :)

Thx ^_^

Share this post


Link to post
Share on other sites
14 минуты назад, Iterate сказал:

Nice thank you mate.

thx

 

Share this post


Link to post
Share on other sites

Hey, thanks for doing tutorials. I have a few improvements for your code.

First of all you should read into a Coding Style Guide, it's not really important which one you use, but it is important that you stay consistent throughout your code.
Personally I usually use Java Guidelines, most importantly that means you should write ALL you classes with an uppercase Letter at the beginning and all your functions with a lowercase letter at the beginning.

Also you should take a look at the ES6 Syntax for classes: http://www.2ality.com/2015/02/es6-classes-final.html
You shouldn't add properties to the prototype manually in ES6.

In the proximityMessage you are using a variable player which does not exist at that point in the code. I replaced it with sender.

The refactored code could look like this:

"use strict";

class Utility {
	static playerToPoint(range, player, x, y, z) {
		let sphere = new Utility.Sphere(x, y, z, range)
		return sphere.inRangeOfPoint(player.position);
	};

	static proximityMessage(radi, sender, message) { //function local chat
		mp.players.forEach(_player => {
			if(Utility.PlayerToPoint(radi, sender, _player.position.x, _player.position.y, _player.position.z)/* && receiver.dimension == sender.dimension*/) {
				_player.outputChatBox(message);
			}
		});
	};
}

Utility.Sphere = class Sphere { 

    constructor(x, y, z, opt_radius) {
	    this.x = x;
	    this.y = y;
	    this.z = z;
	    this.radius = opt_radius || 1;
    }
	
	inRangeOfPoint(position) { 
		return (Math.pow((position.x - this.x), 2) +
				Math.pow((position.y - this.y), 2) +
				Math.pow((position.z - this.z), 2) < Math.pow(this.radius, 2));
		}

};

module.exports.Utility = Utility;

 

This code should be correct, but I can't test it since I don't have access to a server to test this.

 

5 people like this

Share this post


Link to post
Share on other sites

Both of your sources seem a little bit outdated, since they don't make use of the ES6 stuff.
Which is probably necessary if you are creating a website that should be backwards compatible, but certainly makes things more complicated when developing for node.js with ES6 support.

 

1 person likes this

Share this post


Link to post
Share on other sites

It became useless since 0.2 beta has built-in broadcast function with limited range:

mp.players.broadcastInRange(player.position, 25.0, "msg");

 

1 person likes this

Share this post


Link to post
Share on other sites
1 час назад, ragempdev сказал:

It became useless since 0.2 beta has built-in broadcast function with limited range:


mp.players.broadcastInRange(player.position, 25.0, "msg");

 

;c

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.