26. Protect important rewards with a guard
For valuable or repeatable conversations, a guarded function is a stronger reward boundary than relying on menu appearance alone. It can check whether the reward was already issued and whether the player already carries the item.
func grant_key() -> void:
if bool(Runtime.variables.get("archive_key_given", false)):
return
if not HasInventory("archive_key"):
await AddInventory({"itemId":"archive_key", "count":1})
await SetVariable({"name":"archive_key_given", "value":true})Put this inside a script extending PCAScriptBase, then expose the function so the dialogue can call it:
1. In the Script Designer, open Functions as Nodes and choose Add Function.
2. Leave Expose as Action and Visual Node checked. Set Function Name to grant_key and Display Name to Give Archive Key Once.
3. Do not add parameters for this function. Choose Use Definitions, then save the script/project.
4. In the dialogue Action node, choose Call Script Function and select Give Archive Key Once.
Use Definitions saves the callable definition; it does not run the function. The reward happens when the game reaches its Call Script Function action.
If you switch the reward branch to this function, remove the old unconditional Add Inventory step. Continue to the acknowledgement Line after the function completes.
The first flag checks whether Elias has already given the reward. The inventory check avoids adding another copy when a different route has already supplied the same item.
This is an optional strengthening of the example. The basic graph already hides the request after a successful loan. Use the guard when other routes, scripts or replay behavior could reach the reward.
