The classic "undefined" error in SugarCube!
Don't worry, it's an easy fix. I'll guide you through the process.
Understanding the error
The error message tells you that:
- The error occurred in a
<<set>> macro (a SugarCube directive to set a variable).
- The "bad evaluation" means SugarCube couldn't evaluate (read/execute) the expression you provided.
- The specific issue is that
State.variables.playerVar.fame.level is undefined.
In SugarCube (which runs on top of JavaScript), State.variables is the object that stores all your game's variables. Think of it like a big container where all your game's data lives.
The dot notation (playerVar.fame.level) means you're trying to access a nested property:
playerVar is a property of State.variables.
fame is a property of playerVar.
level is a property of fame.
The error says that at some point in this chain, something doesn't exist yet.
Why this happens
When you altered the variables, you likely:
- Either deleted or renamed part of the structure (e.g.,
fame or level).
- Or never initialized
playerVar.fame.level before trying to access/modify it.
- Or overwrote
playerVar or fame with a value that isn't an object (e.g., a number, string, or null).
How to fix it
Here's a step-by-step debug process:
1. Check the current structure of playerVar
Add this temporary code somewhere in your game (e.g., in a passage or a debug section):
<<script>>
console.log(State.variables.playerVar);
<</script>>
Run the game and open your browser's developer console (usually F12 or Ctrl+Shift+I). Look at the output:
- If
playerVar is undefined, you need to initialize it first.
- If
playerVar is an object (e.g., {}), check if it has a fame property.
- If
fame exists but isn't an object (e.g., it's null or a number), that's your problem.
Example output scenarios:
Good:
{
"fame": {
"level": 5,
"points": 120
},
"health": 100
}
Bad (missing fame):
{
"health": 100
}
Bad (fame isn't an object):
{
"fame": 0,
"health": 100
}
2. Initialize the missing part
Based on your findings:
- If
playerVar is undefined, add this to your StoryInit passage (or a passage that runs early in your game):
<<set $playerVar = {}>>
<<set $playerVar.fame = { level: 0, points: 0 }>> // Or whatever default values you need
- If
fame is missing inside playerVar:
<<set $playerVar.fame = { level: 0, points: 0 }>>
- If
fame exists but level is missing:
<<set $playerVar.fame.level = 0>>
Note: In SugarCube, $foo is shorthand for State.variables.foo. So $playerVar === State.variables.playerVar.
3. Fix the broken <<set>> statement
Now, find the specific <<set>> line causing the error. It probably looks like:
<<set $playerVar.fame.level = 5>> // or some other value
This is fine if playerVar.fame already exists. If not, SugarCube chokes because it can't create nested objects on the fly (unlike JavaScript's {} syntax).
Rewrite it safely:
<<set $playerVar.fame = { level: 5 }>> // THIS WILL OVERWRITE ANY EXISTING `fame` DATA
Or, better (preserves other fame properties if they exist):
<<set $playerVar.fame ||= {}>> // Ensure $playerVar.fame is an object (or create it)
<<set $playerVar.fame.level = 5>> // Now this will always work
The ||= operator means "set this if it's undefined/null".
4. Test & Verify
Run your game again. The error should be gone. Double-check that:
- Your variable is updating correctly.
- Saving and loading the game retains the value (SugarCube handles this automatically, but always test!).
Prevent future issues
To avoid this:
- Always initialize nested variables early (e.g., in StoryInit).
- Use
console.log to inspect your variables when something breaks.
- When setting deep properties, ensure parent objects exist:
<<set $foo.bar.baz = 123>> // Unsafe
// Safe way:
<<set $foo ||= {}>>
<<set $foo.bar ||= {}>>
<<set $foo.bar.baz = 123>>
That's it! You should now have your SugarCube variables back in shape.
Happy tweaking!