Old project, new life, variable problem

I’m picking up a project I had started working on, and gotten fantastic help here some years back. I haven’t touched it due to life getting in the way, but I’m brushing the dust off of it. I updated my installed versions of Twine; Sugarcube is version 2.36.1.

The below variable, ‘reqdays’, is what’s throwing the “Bad evaluation: unexpected string”. If I comment it out, everything else works.

I think I’ve figured out what I’m trying to do here (track the career paths a player can choose), but not sure what the issue is with it. I ran the file through SugarValidator and it found no errors. The two comments were added by me for this and are not part of the actual file. Eventually other careers will be added such as barrista, personal trainer, mechanic, and whatnot.

<<set $career to {
	waitstaff: {
		reqdays: {                     /* required work days */
		optdays: {},                   /* optional work days, empty for now */
		starttime: 16,
		endtime: 23

I know it has to be stupidly obvious, but what is wrong?

Sugarcube’s TwineScript is (according to the documentation) “essentially, JavaScript with an extra spoonful of sugar on top to make it a bit nicer for the uninitiated”.

In JavaScript, round brackets () are used for ordinary arithmetic grouping (like (1 + 2) * 3), curly brackets are used to write Map objects (like {name: "Bilbo", age: 111}), and square brackets are used to write arrays (like [1, 1, 2, 3, 5]).

In your code, you say set $career to { waitstaff: ... } which is clearly a map, and that’s fine.

Inside that, you say { reqdays: ..., optdays: {}, starttime: 16, endtime: 23} which is also clearly a map, and that’s fine.

Inside that, you say {'Tuesday', 'Friday', 'Saturday', 'Sunday'} but although this starts and ends with curly brackets, it does not contain the key: value items a map expects. Instead, it has a sequence of ordinary values like an array would.

So, if you change this to ['Tuesday', 'Friday', 'Saturday', 'Sunday'] (square brackets instead of round brackets) it’ll probably work.

On the other hand, the Sugarcube docs say that it supports Set objects, but it does not describe how to write them, and the relevant JavaScript documentation doesn’t mention it either, so I don’t know how one might use them in Sugarcube.

A minor correction to what Screwtape said, but key/value pairs shown within curly brackets are not a Map object, they’re just a generic Object.

While Object and Map objects are similar in JavaScript, an actual Map object would be created using the Map() constructor, which was not used in the OP’s code. On the other hand, using curly brackets { ... } like in the OP’s code means that the value is actually a generic Object. For further differences between the two see “Objects vs. Maps”.

That said, Screwtape is correct that you apparently want to have the value for the key “reqdays” be an array, thus, instead of using curly brackets, you should use square brackets around that list of strings to make them into an array.

Actually, it does explain it there. You can create an empty Set object by doing simply new Set() (e.g. <<set $someSet = new Set()>> in Twine/SugarCube code), or you can create a Set with elements in it by doing something like new Set([1, 2, 3, 4]) (though this latter method isn’t supported by Internet Explorer, if that at all matters to you). See Screwtape’s link above for documentation on Set objects, if you think you might need them.

Enjoy! :slight_smile:

1 Like

IIRC, that should be fixed by ES-6 Shim, which is included with SugarCube v2.

Doh! I knew it was something silly.

I was googling around looking up the various curled braces, as that appears to be what I had started playing around with.

Thanks everyone for the quick response and clarification on this. I think I’ve got more cobwebs to brush off then I thought. :smiley: