9. If, else if and else
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
endfunction run()
return describe_charge(45)
endThe 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.
global powered = false
function can_open(): boolean
if not powered
return false
end
return has_inventory("key")
endreturn 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.
