Lua Script - Scan the Rings only once?

Status
Not open for further replies.

SonicX8000

Graphic & Sprite Modder.
Judge
Using MI's ringcounter script which basically tells you how many rings are in a level with a console command. I have it altered so it displays on the HUD rather than in the console.

In case you need a screenshot...
srb2rings.gif

This is a very early screenshot by the way.

example.bmp

This is what I currently have it set as.

Now the current issue I am having with this script... is that if there is MANY rings and I do mean MANY rings. The game lags quite badly. Some levels to note are CEZ2 & AGZ and SS3, SS6, SS7 and SS8.

I figure I needed a mapload hook so I took a look at Inu's Boss Hud Script to get a idea of what to do so the that the script will only scan the rings just once much like how his script scans the bosses once... and alas... it seems that I haven't got a clue of what I'm doing.

This is what's currently in the script by the way.
Code:
local ring_list = {} //stash rings & coins

local function h(v, p)
    local rings = 0 // final ring count
    for mobj in thinkers.iterate("mobj")
        if mobj.type == MT_RING // regular rings
            rings = $1 + 1 // add one to final ring count
        elseif mobj.type == MT_COIN // regular rings
            rings = $1 + 1 // add one to final ring count
        elseif mobj.type == MT_SUPERRINGBOX and mobj.threshold != 68
            rings = $1 + 10 // add ten to final ring count
        end
    end
    local p_ringslash = v.cachePatch("RNGSLASH")
    
    v.draw(72, 56, p_ringslash) //ring slash
    v.drawNum(111, 56, rings) //ring count
end
hud.add(h, "game")

local function hook_load() //let's not lag the game if the map has many rings
    -- Old table must be completely unset!
    ring_list = {}

    -- Go through mobj thinkers just once
    for    mobj in thinkers.iterate("mobj")
        -- Find all rings
        if mobj.type == MT_RING
        elseif mobj.type == MT_COIN
        elseif mobj.type == MT_SUPERRINGBOX and mobj.threshold != 68
            -- And add it to the ring list
            table.insert(ring_list, mobj)
        end
    end
end
addHook("MapLoad", hook_load)
Any help with this if possible will be nice, thanks.

Oh yea forgot to mention one more thing. I don't want this script to draw the "rings left in map counter" in both Nights & Special Stages and in Match/CTF/RACE/etc. since it would look very odd.
 
Last edited:
Code:
local ring_list = {} //stash rings & coins

local function h(v, p)
    local rings = 0 // final ring count
    for _,mobj in ipairs(ring_list) -- Using the ring_list
        if not mobj.valid then continue end -- Avoiding no-longer-valid objects
        if mobj.type == MT_RING // regular rings
            rings = $1 + 1 // add one to final ring count
        elseif mobj.type == MT_COIN // regular rings
            rings = $1 + 1 // add one to final ring count
        elseif mobj.type == MT_SUPERRINGBOX and mobj.threshold != 68
            rings = $1 + 10 // add ten to final ring count
        end
    end
    local p_ringslash = v.cachePatch("RNGSLASH")
    
    v.draw(72, 56, p_ringslash) //ring slash
    v.drawNum(111, 56, rings) //ring count
end
hud.add(h, "game")

local function hook_load() //let's not lag the game if the map has many rings
    -- Old table must be completely unset!
    ring_list = {}

    -- Go through mobj thinkers just once
    for    mobj in thinkers.iterate("mobj")
        -- Find all rings
        if mobj.type == MT_RING
        or mobj.type == MT_COIN -- You need ors here, not elseifs
        or mobj.type == MT_SUPERRINGBOX and mobj.threshold != 68
            -- And add it to the ring list
            table.insert(ring_list, mobj)
        end
    end
end
addHook("MapLoad", hook_load)

This should fix the script to not lag. Figuring out how to make it not render in other game modes should be a simple fix after this. Check the dynamic constants in the Lua documentation thread for the variables you'll need to check.

Although... listing current ring out/rings left in the stage is kind of confusing. Maybe it should store the count on level load, so the number always represents the total ring count in the level?
 
RedEnchilada said:
This should fix the script to not lag. Figuring out how to make it not render in other game modes should be a simple fix after this. Check the dynamic constants in the Lua documentation thread for the variables you'll need to check.
That fixed it, thanks. I'll take a look at the documentation thread and see how it goes.

RedEnchilada said:
Although... listing current ring out/rings left in the stage is kind of confusing. Maybe it should store the count on level load, so the number always represents the total ring count in the level?
That was a idea at first, but if the map were to be played during co-op at least, I don't think it would count the total for all the players whom have the rings. Perhaps I can add another ring count that counts the total rings for all the players much like how the special stages and nights mode does.

Anyway, thanks.

*EDIT*
Got the script the way I want it. Thanks for all who helped.
 
Last edited:
Status
Not open for further replies.

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top