Globalstep timer causes insane lag delay

Post Reply
User avatar
Nininik
Member
Posts: 567
Joined: Thu Apr 06, 2023 01:55
GitHub: nininik0
IRC: nininik
In-game: nininik
Location: CA, Team thunderstrike headquarters
Contact:

Globalstep timer causes insane lag delay

by Nininik » Post

What happens is that any action i do only happens after the bird noise plays. If there isn't isn't any noises playing my actions are delayed AF.
Here is the code:

Code: Select all

local function checkPlayerPos(player)
    local pos = player:get_pos()
    if not pos then
        return false
    end
    return true
end

local function playNightNoise(player)
    if not checkPlayerPos(player) then
        return
    end
    local pos = player:get_pos()
    local milli = minetest.get_timeofday() * 24000
    if pos.y >= -5 and (milli >= 19000 or milli <= 4000) then
        minetest.sound_play("owl", {gain = 1.0, pos = pos, to_player = player:get_player_name()})
    end
end

local function playWindNoise(player)
    if not checkPlayerPos(player) then
        return
    end
    local pos = player:get_pos()
    minetest.sound_play("wind", {gain = 1.0, pos = pos, to_player = player:get_player_name()})
end

local function playTweetNoise(player)
    if not checkPlayerPos(player) then
        return
    end
    local pos = player:get_pos()
    local millihour = minetest.get_timeofday() * 24000
    if pos.y >= -5 and millihour >= 5000 and millihour <= 18000 then
        minetest.sound_play("birds", {gain = 1.0, pos = pos, to_player = player:get_player_name()})
    end
end

local function scheduleNoises()
    for _, player in ipairs(minetest.get_connected_players()) do
        minetest.after(math.random(5, 40), function()
            playTweetNoise(player)
        end)
        minetest.after(math.random(5, 35), function()
            playNightNoise(player)
        end)
    end
    minetest.after(30, function()
        for _, player in ipairs(minetest.get_connected_players()) do
            playWindNoise(player)
        end
        scheduleNoises()
    end)
end

-- Call the scheduling function to start playing the noises
scheduleNoises()


I also have a skybox changing code for space or caverealms DM caves

Code: Select all

local function setPlayerSky(player)
    local pos = player:get_pos()
    if pos.y < -4000 then
        player:set_sky({r = 15, g = 0, b = 0}, "plain") -- Set sky to dark red
    elseif pos.y > 4000 then
        player:set_sky({r = 0, g = 0, b = 0}, "plain") -- Set sky to BLACK
    else
        player:set_sky(nil, "regular")
    end
end

minetest.register_globalstep(function(dtime)
    for _, player in ipairs(minetest.get_connected_players()) do
        setPlayerSky(player)
    end
end)
↯Glory to Team Thunderstrike!↯
↯T.T.S.↯

User avatar
Nininik
Member
Posts: 567
Joined: Thu Apr 06, 2023 01:55
GitHub: nininik0
IRC: nininik
In-game: nininik
Location: CA, Team thunderstrike headquarters
Contact:

Re: Globalstep timer causes insane lag delay

by Nininik » Post

In fact i don't know how to fix this
↯Glory to Team Thunderstrike!↯
↯T.T.S.↯

User avatar
Blockhead
Member
Posts: 1668
Joined: Wed Jul 17, 2019 10:14
GitHub: Montandalar
IRC: Blockhead256
In-game: Blockhead Blockhead256
Location: Land Down Under
Contact:

Re: Globalstep timer causes insane lag delay

by Blockhead » Post

Nininik wrote:
Sun Mar 31, 2024 01:16
In fact i don't know how to fix this
I would start with what's most obvious, which is the globalstep. Farlands Reloaded has its own function that changes the skybox according to the biome, but that runs only every 2 seconds. The globalstep will instead be something like every 0.09 seconds if everything is running on time, and it gets worse the more globalsteps you have.
/˳˳_˳˳]_[˳˳_˳˳]_[˳˳_˳˳\ Advtrains enthusiast | My map: Noah's Railyard | My Content on ContentDB ✝️♂

User avatar
Nininik
Member
Posts: 567
Joined: Thu Apr 06, 2023 01:55
GitHub: nininik0
IRC: nininik
In-game: nininik
Location: CA, Team thunderstrike headquarters
Contact:

Re: Globalstep timer causes insane lag delay

by Nininik » Post

yes but there has to be a way to not use globalsteps or smh
↯Glory to Team Thunderstrike!↯
↯T.T.S.↯

User avatar
Blockhead
Member
Posts: 1668
Joined: Wed Jul 17, 2019 10:14
GitHub: Montandalar
IRC: Blockhead256
In-game: Blockhead Blockhead256
Location: Land Down Under
Contact:

Re: Globalstep timer causes insane lag delay

by Blockhead » Post

Nininik wrote:
Mon Apr 01, 2024 13:51
yes but there has to be a way to not use globalsteps or smh
Well yes, actually. Just take a look at the top and the bottom of the file that I linked and you can see how.
/˳˳_˳˳]_[˳˳_˳˳]_[˳˳_˳˳\ Advtrains enthusiast | My map: Noah's Railyard | My Content on ContentDB ✝️♂

User avatar
Nininik
Member
Posts: 567
Joined: Thu Apr 06, 2023 01:55
GitHub: nininik0
IRC: nininik
In-game: nininik
Location: CA, Team thunderstrike headquarters
Contact:

Re: Globalstep timer causes insane lag delay

by Nininik » Post

Blockhead wrote:
Mon Apr 01, 2024 15:06
Nininik wrote:
Mon Apr 01, 2024 13:51
yes but there has to be a way to not use globalsteps or smh
Well yes, actually. Just take a look at the top and the bottom of the file that I linked and you can see how.
ye but what abt the noises?
↯Glory to Team Thunderstrike!↯
↯T.T.S.↯

User avatar
LMD
Member
Posts: 1396
Joined: Sat Apr 08, 2017 08:16
GitHub: appgurueu
IRC: appguru[eu]
In-game: LMD
Location: Germany
Contact:

Re: Globalstep timer causes insane lag delay

by LMD » Post

This is premature optimization (look the term up online to know what it is and why it's generally considered bad practice).

A cheap globalstep - and everything presented so far looks very cheap - is not a performance concern. If you want to find out what the actual performance problem is, use jitprofiler.
My stuff: Projects - Mods - Website

User avatar
Mantar
Member
Posts: 589
Joined: Thu Oct 05, 2017 18:46
Contact:

Re: Globalstep timer causes insane lag delay

by Mantar » Post

Runs setPlayerSky for every player, but only does it every 2 seconds

Code: Select all

local timer = 0 local limit = 2
minetest.register_globalstep(function(dtime)
    timer = timer + dtime
    if timer < limit then return end
    timer = 0
    for _, player in ipairs(minetest.get_connected_players()) do
        setPlayerSky(player)
    end
end)
Lead dev of Exile, git repo: https://codeberg.org/Mantar/Exile

User avatar
Nininik
Member
Posts: 567
Joined: Thu Apr 06, 2023 01:55
GitHub: nininik0
IRC: nininik
In-game: nininik
Location: CA, Team thunderstrike headquarters
Contact:

Re: Globalstep timer causes insane lag delay

by Nininik » Post

what about the bird noises? yall seem to only care about the skybox switch.
↯Glory to Team Thunderstrike!↯
↯T.T.S.↯

User avatar
LMD
Member
Posts: 1396
Joined: Sat Apr 08, 2017 08:16
GitHub: appgurueu
IRC: appguru[eu]
In-game: LMD
Location: Germany
Contact:

Re: Globalstep timer causes insane lag delay

by LMD » Post

Nininik wrote:
Mon Apr 01, 2024 21:17
what about the bird noises? yall seem to only care about the skybox switch.
You tell me after you've profiled.
My stuff: Projects - Mods - Website

User avatar
Mantar
Member
Posts: 589
Joined: Thu Oct 05, 2017 18:46
Contact:

Re: Globalstep timer causes insane lag delay

by Mantar » Post

Oh, well for that I would plug it into a globalstep with a 30 second timer rather than using minetest.after().
Lead dev of Exile, git repo: https://codeberg.org/Mantar/Exile

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests