I need help with object

Twine Version: 2.6.2
Sugarcube Version: 2.36.1

I’m trying to make a player object which will be accessed and modified both during character creation and along the game, this is my current code:

<<set $player = {
    name: "",
    money: 0,
	debuffs: [],
	buffs: [],
	spells: [],
	books: [],
	journal: [],
	inventory: [],
	/* Stats */
	determination: 0,
	intelligence: 0,
	dextery: 0,
	social_skills: 0, 
	strenght: 0,

	/* Skills */
	/* Dextery */
	bow: 0,
	throwing: 0,
	two_handed: 0,
	polearm: 0,
	crossbow: 0,
	gunpowder: 0,
	stealth: 0,
	painting: 0,
	/* Social Skills */
	negotiation: 0,
	non_verbal: 0,
	leadership: 0,
	acting: 0,
	music: 0,
	/* Determination */
	faith: 0,
	mental_strenght: 0,
	mental_resistance: 0,
	/* Strenght */
	athletism: 0,
	unarmed: 0,
	constitution: 0,
	/* Intelligence */
	animals : 0,
	humanities: 0,
	medicine: 0,
	engineering: 0,
	perception: 0,

	/* Others */
	hp: 0,
	max_hp: 0,
}>>

When I tried to print the values, such as $player.hp I get the following error:

Error: <<print>>: bad evaluation: State.variables.player is undefined

What am I doing wrong and how to fix it?

Your code works fine for me as is. Are you sure the code you pasted is the same as the one in your game? If so, in what passage are you initializing $player? Generally, you should initialize your variables in the StoryInit passage. If you’re already using it, make sure it’s spelled StoryInit exactly, with that capitalization.

2 Likes

This is embarrassing, but after you told me it’s working for you, I wondered if the issue was something else, and indeed it was:

I had on my JavaScript section

Config.saves.autosave = true;
Config.saves.autoload = true;

So the game was loading an automatically made save from before and that’s why it was breaking :melting_face: , I’ll comment it out until I’m finished, shouldn’t have thought THAT forward when I started to code the game. Thank you very much for the help.

1 Like