Jump to content

What will affect performance the most?


DRW

Recommended Posts

Hi,

So I'm having a pretty big problem related to performance. I have some AI, and the client sometimes loops through all of them so it can handle operations like jumping, for example.

At first, I used to loop through all peds, and filtered them for their elementdata, and that was obviously really performance-hungry and not very effective.

Now, the script is a little bit more optimized, it does no longer loop through every single ped. The server sends the player a table with all bots, and the client does everything else by just using that table, so instead of looping through almost 500 peds, it just loops trough 170 or so.

I've checked it up, and I've seen that, ~140 bots in the table won't cause performance problems, but ~200 will make everyone have minimal lag spikes every 2-4 seconds.

What should I do? I absolutely need to loop through all bots and I need ways to optimize this.

Thank you for your help.

Link to comment

So, just giving a few ideas here, based on what info you gave...

Why exactly do you need to loop through all of the peds?

Can you break down the table further so the amount you loop through gets smaller?

Can you reduce the amount the loops are run, e.g calling the loop less often?

 

  • Thanks 1
Link to comment
21 minutes ago, 3aGl3 said:

So, just giving a few ideas here, based on what info you gave...

Why exactly do you need to loop through all of the peds?

Can you break down the table further so the amount you loop through gets smaller?

Can you reduce the amount the loops are run, e.g calling the loop less often?

 

I need the peds to jump when a lineOfSight is created and obstructed by an obstacle, so I can use setPedControlState. I'm using a timer every 2 seconds. The table's structure is this.

table = {
{botPed},
{botPed},
{botPed},
{botPed},
{botPed},
{botPed},
{botPed},
}

You mean that I should distribute the bots in various tables? I have thought about that but I didn't think it would make sense because I'd have to loop through two tables anyway.

Link to comment

With these instructions, I think, if you calculate in client side, you should only calculate peds, which are in the streamed zone.
Use an if branching with isElementStreamedIn(), or if you really want to reduce the peds table, you should use "onClientElementStreamedIn" and "onClientElementStreamedOut" events and a table, where you push and remove the peds. Good to know, that if you restart the resource while you have peds already in the srteamed zone, they will not trigger this event, so you need to loop peds table with getElementsByType("ped",root,true), where the last param choose you only the streamed peds, then you can add to the table.
Anyway, making AIs in Mta is very interesting, have a good luck with this!

  • Thanks 1
Link to comment
2 hours ago, Awang said:

With these instructions, I think, if you calculate in client side, you should only calculate peds, which are in the streamed zone.
Use an if branching with isElementStreamedIn(), or if you really want to reduce the peds table, you should use "onClientElementStreamedIn" and "onClientElementStreamedOut" events and a table, where you push and remove the peds. Good to know, that if you restart the resource while you have peds already in the srteamed zone, they will not trigger this event, so you need to loop peds table with getElementsByType("ped",root,true), where the last param choose you only the streamed peds, then you can add to the table.
Anyway, making AIs in Mta is very interesting, have a good luck with this!

Thank you so much! Totally forgot about those events.

Link to comment
  • Moderators

You can reduce data with sending only updates from the table instead of a copy of the table.

As triggerEvents always reach their targets in order. So when they join, you send them the whole table and after that you send them only updates.

<Tested>

 

Using a single timer on serverside, will also help you to send multiple updates at once. If you use multiple timers then the amount of triggers will increase.

A check rate of 150 ms would be fine. (but of course there will be only be triggers when there are updates)

 

It might use more memory/cpu at the end, but it gives you the ability to reduce data and having your workflow starting from one function.

  • Thanks 1
Link to comment

Thank you all for that. Have another doubt here and I don't want to spam the scripting section.

Let's say I want a ped to move/jump, so I'll have to send some type of data to all the players. What is more performance-friendly: setElementData so everyone can get the info when the data is changed for the client or just triggerClientEvent for each player? It's only an example, this is in no way what I'm doing.

So...

This?

-- SERVERSIDE

function setBotWalking (bot)
	triggerClientEvent (root,"setBotWalking",bot)
 	botlist[bot] = {"walking"}
end

-- CLIENTSIDE 
addEvent ("setBotWalking",true)
addEventHandler ("setBotWalking",root,function()
  setPedControlState (source,"forwards",true)
end)

...or this?

-- SERVERSIDE

function setBotWalking (bot)
  setElementData (bot,"state","walking")
end


-- CLIENTSIDE

addEventHandler ("onClientElementDataChange",root,function(dataname,oldvalue)
  if getElementType (source)=="ped" and dataname == "state" and getElementData (source,dataname)=="walking" then
  setPedControlState (source,"forwards",true)
  end
end)

 

Edited by MadnessReloaded
Link to comment
  • Moderators

Element data

- Uses less data

- Slow

- Target players can't be specified.

- Latent variant not available.

 

Trigger event.

- Uses much more data

- Much faster

- Target players can be specified.(data reduction)

- Latent variant available. (Not blocking the network)

 

To be honest, I prefer to use the latent trigger, even though it might be very annoying for players with slow internet. But it works for me best, because the players will not teleport so much.

 

  • Thanks 1
Link to comment
On 19/1/2018 at 23:22, IIYAMA said:

Element data

- Uses less data

- Slow

- Target players can't be specified.

- Latent variant not available.

 

Trigger event.

- Uses much more data

- Much faster

- Target players can be specified.(data reduction)

- Latent variant available. (Not blocking the network)

 

To be honest, I prefer to use the latent trigger, even though it might be very annoying for players with slow internet. But it works for me best, because the players will not teleport so much.

 

Thank you for that, that'll help me optimize my server by a lot.

Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...