Using "or" in a variable declaration, but allowing false values to compute

  1. What do you want to achieve? I want to set a variable to be equal to one value if it exists (even if it’s false), or another value as a default backup.

  2. What is the issue? Using the or keyword in variable declarations will use the second value if the first is equal to false.

local rotClamp = shakeParams["RotationClamp"] or Config.SHAKE_ROTATION_CLAMP_DEFAULT

In this example, when shakeParams[“RotationClamp”] == false, the default is set instead (currently true)

I don’t want to run this code for every variable, as it’s long and hard to read

local rotClamp, cameraMotionBlurEnabled, cameraMotionBlurMax, uiMotionBlurEnabled

    if shakeParams["RotationClamp"] == nil then
        rotClamp = Config.SHAKE_ROTATION_CLAMP_DEFAULT
    else
        rotClamp = shakeParams["RotationClamp"]
    end

(… on and on for each variable)

  1. What solutions have you tried so far? I’ve looked for anyone trying to achieve a similar functionality, but haven’t found anything.

Thanks! :smiley:

Something like this should work:

local rotClamp = shakeParams["RotationClamp"] == nil and Config.SHAKE_ROTATION_CLAMP_DEFAULT or shakeParams["RotationClamp"]

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.