#Unoptimised

1 messages · Page 1 of 1 (latest)

polar summit
#

Have you tried profiling this to check what is specifically causing the script tick issue?

polar summit
# past pond wdym?

A command exists in the game called /script profilerof which gives you an overview which native calls are being overused, causing script tick lag.

polar summit
past pond
#

oh i see

polar summit
#

In-game, you'll want to run that command within a defined time for best practice. Start the profile using /script profiler start and play through the motions of a player loading and unloading a plot.

Then, run /script profiler stop and make note of the output. The output should contain a file path that can be put into the extension basically.

past pond
#

unfortunatley

#

nothing bad found

polar summit
#

What function was the highest?

past pond
#

world.getPlayers

#

i did again

#

and found

#

THIS

polar summit
past pond
#

how do i read this btw?

#

like what do the numbers mean

polar summit
#

how much time is taken for the function to resolve

#

uncollapsing them reveals the line it was called on

past pond
#

is 57ms bad?

polar summit
#

how long were you running it, 30 seconds?

#

either way, 57ms is not bad at all

#

world.getPlayers is a bit more concerning

past pond
#

this is it

polar summit
#

that prob explains the tick rate being elevated outside of the spike

#

not sure where the spike is coming from without seeing the full codebase

#

how often does that spike occur?

#

and are we sure it's the plot system

past pond
#

the spike is happening when i leave

polar summit
#

I see, structures are still fairly performance draining but interesting that it's being shown as a tick spike.

polar summit
#

I guess I underestimated that. My guess it's when a structure is being placed.

polar summit
#

You can improve it by simply adding caching measures.

past pond
#

ill try optimise this ig

#

idk how

polar summit
#

Not sure if you can improve structure performance at all.

past pond
polar summit
#

As for everything else like native calls for players, you can do something super simple like saving joined players to an external playerList variable and then removing them when they leave. This way getPlayers is only ever called like 20 times in a session all-together.

past pond
polar summit
#

Let me find an example

#
import { world, Player, system } from "@minecraft/server";

let playerList = [];

/**
 * @returns {Player[]} 
 */
export function getPlayerList() {
    return playerList;
}

world.afterEvents.worldLoad.subscribe(() => {
    playerList = world.getAllPlayers();
});

world.afterEvents.playerSpawn.subscribe(ev => {
    if (!ev.initialSpawn) return;
    playerList = world.getAllPlayers()
});

world.afterEvents.playerLeave.subscribe(ev => {
    playerList = world.getAllPlayers();
})```
#

@past pond this saves the player list only on join and leave then you can call that function to return the cached objects.

#

It’s considered faster because directly calling the function every tick just takes more time to resolve than if it’s stored in memory.