Sign in to follow this  
DevGrab

First step to sync your own Peds!

Recommended Posts

Hello RAGE Beast's!

I want to write this tutorial instead of releasing my packages with the Ped-Syncing, so people wont get spoonfeeded and everyone who can code and use brain, can reconstruct what i've done in many hours of work. In this tutorial i just want to show, or explain, the basics of syncinc peds for everybody at the server.

First of all we need to start thinking about, what we need, and how we can realize our idea of syncing them.

 

I've started to get more knowledge about how pedestrians work on GTA and how i can control the activities of what they do.. so i've sit down.. working hours on studying natives, functions, behaviour and some more stuff about how to get "life" into a spawned ped object. At the start i've just controlled them client-sided.. playing animations, let them walk around.. and such stuff.

After this i came to the conclusion that i'll not get far in programming with the given functions GTA5 handles to me.. Since we've no direct access to the engine / internals of GTA i've need to build my own "AI" to make the Peds "think" what they do. The restriction of not having access to control internal stuff is starting at directly receive informations what the ped is doing.. which animation is tasked.. and s lot of other problems who popped up.

 

So i've sit down again, starting to construct new classes.. kind of creating my own Controling-Class of my Peds, instead of using RAGEMP's one. You can use RAGEMP's "mp.peds.new" but i just wanted to make clear, i'm using my own stuff. The controlling class is working with the Natives of GTA5. So the class manage to spawn peds and directly put the handle into a class-object which contains all informations of what i do with them.. positions, animations, flags (walking, running, random walking).. 

 

Since we've a unique ID for each ped, we can control them via ID's and our Control-Class has the ability to pick out those ID's and control the single ped. Since everything is stored in the class.. we need to start working on functions which give our Peds an own AI.

First of all we need to know math (vectors, cos / sinus / tan), thanks to RAGE and GTA we've also got ray-casting. At this tutorial i'll just explain how to let them walk in one direction... with a "random" heading (15°-20°) to left or right.. Kind of a "human" natural walk. Since ped.getHeading() isnt that accurate for our AI.. we need to use our own "heading" variable.. so i've just popped it into my Ped-Class.. and we also need to check the "old" and the "new" position of where the Ped should work.

Let's store the "old" position before we set a new one.

this.oldPos = new mp.Vector3(this.handle.getCoords(false).x, this.handle.getCoords(false).y, this.handle.getCoords(false).z);

 

After this we create our "random" heading. To the "old" heading we add the needed "left" or "right" pitch. So we create a temporary variable storing our old heading in it and add a random value.

var randomHeading = this.oldHeading + ((Math.random() * 1.0) - 0.5);
this.newHeading = randomHeading;

You need to think about that we're using cos/sin and to make sure that you know we're working inside of a coordinate/vector system. (x/y/z) So you need to think about the relations about degree and single steps inside of a coordinate system. (1 is a unit of length)

After this we're going to calculate the new position in relation to your null-point (the player) so we pick our "new" heading and multiply it by 3. (1 unit is like 1 ingame meter)

var headingX = Math.cos(this.newHeading) * 3;
var headingY = Math.sin(this.newHeading) * 3;

 

Since we've got your range from direct null-point we need to add those headingX / headingY to your real player coordinates. (x / y)

var position = this.handle.getCoords(false);
this.newPos = new mp.Vector3(position.x + headingX, position.y + headingY, position.z);

Since Z is the same and remains the same.. we dont need a tan.

So we've got our "new" position out of this calulcation. 

AM43epM.png

 

After this we're going to check if the new position is traceable / ray-cast.. so we check if the ped is running / walking against a wall or not. You can find this function inside the RAGE Wiki. (raytrace) If there is no object between the old position and the new position.. you can set the ped walking at the position. If there is something between the old and new position.. You'll need to take care to walk or recalculate the path.

At my point, i've just set the heading * 2.

if (isTraceable(this.oldPos, this.newPos)) {
	this.walk(new mp.Vector3(position.x + headingX, position.y + headingY, position.z));
	this.oldHeading = this.newHeading;
	this.position = this.newPos;
}
else {
	this.oldHeading = this.newHeading * 2;
	this.randomwalk();
}

 

So the rest of the other stuff, you can do it.. if you know where we are at the current point. We've done our client-side stuff.. so we need to go over how to sync the ped with the players.

The biggest question is how to get the sync between the server and the client. Since we're storing all information about our ped at our client.. we need to send it to the server.. but we need to take care that we don't spam the server with our informations and also take care to send smallest infos as possible.. think about that there could be hundret of players who send informations.. so take care of resources.

p6hQWYC.png

The red dots are the peds of other users.. as client.. we dont need to send the informations of them to the server.. since they're controlled by another user.

The green dots are the peds of our user.. as client.. we need to send the informations to the server.. cuz we control them.

The blue dots are all peds stored at the server.. which contains informations.. (ids, positions, ...) the server sends the information the the clients

The orange dots are all peds who aren't created at the client-side and theyre from other users.. so you need to create them and update the information of them.

 

I decided to just send the unique ID and the position data and also the flags to the server.. At the server we've got also a class with the informations.. kind of we build at our client. We need some functions to check if the unique ID already exists.. and if we can update the informations or if we need to destroy the element and stuff. If there is no ID at the server, the server will update the sended informations of the client and send all the informations to each other player on the server, who is in range the ped. (virutal ped, since the position is server-sided) This is the first step of preventing the server using too much bandwidth/stress. When the server sends the data to each client.. the clients need to check if the ID of the ped exists on their client.. if not, create the ped and store the informations send by the server to the ped-object class. Also check if the ped you've created is client-side or server-side. Since we dont need the data of your own peds the client-side has been created.

So to put everything together:

Your client creates peds.. they're local. You just stream the information of your OWN peds to the server.. while this happens.. the server send the information of other player peds to you. If you dont got the ped with the unique ID created.. you need to create the ped and update the informations you've got.. so let the ped wak to xyz.. update models,.. health...

 

I hope with this non-spoonfeed tutorial i helped you a bit.. Ask question if you want.. Thank you! :) 

  • Like 7

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  

  • Recently Browsing   0 members

    No registered users viewing this page.