ShadowMario / FNF-PsychEngine

Engine originally used on Mind Games mod
Apache License 2.0
1.15k stars 2.22k forks source link

How to use a single random integer multiple times. #14194

Closed Average-FNF-Modder closed 8 months ago

Average-FNF-Modder commented 8 months ago

Describe your problem here.

Hi. I'm a relatively newbie coder, trying to improve songs with new art, event charts and such in the FNF Sour demo. Cause the editor is locked, I'm doing everything in Lua, and editing the Json's manually. For that one sick part in Pulp, I want to do a fancy light show where 30% opacity screen flashes of random colors go across the screen. For this, I've implemented the 'getRandomInt((min), (max), true)' function. It looks cool, but sometimes none of the 3 get there values hit and nothing happen's on the beat hit, which I don't want. I'm unsure of how to strutcure my code to allow one getRandomInt() to apply to multiple outcomes, so I'm coming here. Attached below is a chunk of my code that handles the random flashing. If anyone knows how to do this it would be greatly appreciated.

P.S. Bonus points if you can somehow figure out how to prevent the same color from looping twice in a row, I have no clue where to even start on that...

function onBeatHit() --I included the function onBeatHit() to, but ignore the debug print lol.
    luadebugmode = true
    debugPrint(''..curBeat..'')
--line 40
    if curBeat == 134 and curBeat <= 198 then
        triggerEvent('Add Camera Zoom', 0.3, 0)
    elseif curBeat >= 134 and curBeat <= 198 then
        triggerEvent('Add Camera Zoom', 0.08, 0.075)
        if getRandomInt((1), (3), true) == 1 then --This is the part.
            cameraFlash(game, '0x4dff0000', 0.5, true)
            cameraFlash(hud, '0x4dff0000', 0.5, true) --Oh yeah also I can't seem to get the cameraFlash() function to work on the hud, it's weird.
            cameraFlash(other, '0x4dff0000', 0.5, true)
        elseif getRandomInt((1), (3), true) == 2 then
            cameraFlash(game, '0x4d0000ff', 0.5, true)
            cameraFlash(hud, '0x4d0000ff', 0.5, true)
            cameraFlash(other, '0x4d0000ff', 0.5, true)
        elseif getRandomInt((1), (3), true) == 3 then
            cameraFlash(game, '0x4d00ff00', 0.5, true)
            cameraFlash(hud, '0x4d00ff00', 0.5, true)
            cameraFlash(other, '0x4d00ff00', 0.5, true)

Are you modding a build from source or with Lua?

Lua

What is your build target?

Windows x64

Did you edit anything in this build? If so, mention or summarize your changes.

Well... I didn't. The FNF Sour team might've, Idk. The custom menus probably required source because I don't see any scripts for it, but that seems to be the extent of the team's changes if I had to guess.

SrBagel commented 8 months ago

Try using a local variable. For example:

function onSongStart()
    local RandomNumber = getRandomInt(1, 3)
    if RandomNumber == 1 then --If your random number is one it will triger this line
        debugPrint('RandomNumber = 1')
    elseif RandomNumber == 2 then --If your random number is two it will triger this line
        debugPrint('RandomNumber = 2')
    elseif RandomNumber == 3 then --If your random number is three it will triger this line
        debugPrint('RandomNumber = 3')
    end
end

I'm not 100% sure about your other issues... All your code looks right, but I could be wrong.

SrBagel commented 8 months ago

I think I got something that can prevent looping. It worked in all my testing. Here is an example:

local RandomNumber = nil;
local LastRandomNumber = nil;
function onBeatHit()
    RandomNumber = getRandomInt(1, 3, LastRandomNumber) --Exclude the last generated number
    if RandomNumber == 1 then --If your random number is one it will triger this line
        debugPrint('RandomNumber = 1')
    elseif RandomNumber == 2 then --If your random number is two it will triger this line
        debugPrint('RandomNumber = 2')
    elseif RandomNumber == 3 then --If your random number is three it will triger this line
        debugPrint('RandomNumber = 3')
    end
    LastRandomNumber = RandomNumber
end
Average-FNF-Modder commented 8 months ago

Thank you bro, huge help.

Average-FNF-Modder commented 8 months ago

I'll try to figure out the camHUD glitch some other way.