Skip to reading
PCA Script Programming

9. If, else if and else

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
Code example
function describe_charge(charge: number): text
    if charge >= 80
        return "Fully charged."
    else if charge >= 30
        return "Enough for one attempt."
    else
        return "The battery is flat."
    end
end
Code example
function run()
    return describe_charge(45)
end

The first true branch runs. The remaining branches are skipped. else covers everything left over. One end closes the entire decision; a second closes the function.

Put specific conditions before broad ones. If you test charge >= 30 first, a charge of 90 satisfies it and never reaches a later >= 80 branch. You can nest another if inside a branch, but a few clear returns often make a puzzle script easier to maintain.

Code example
global powered = false
function can_open(): boolean
    if not powered
        return false
    end
    return has_inventory("key")
end

return ends the current function. A false return value is still just data: a caller has to inspect it. Returning false does not automatically cancel an unrelated action list. There is no switch statement or ternary operator in PCA Script 1.0; use an else if chain or a dictionary lookup.

Page 16 of 357Permalink
Go to page