Sugarcube link with script problem

Hi,

changin from harlow to sugarcube, at online editor. Seems like i have problem to port harlow to sugarcube.

On StoryInit i have

<<set $bafarians to new Map([[
	"name", "Bafarians",
	"origin", "xxx", 
	"charasteristic", "yyy",
	"description", "zzz"
]])>>

On tpl_speciesInfoScreen i have

''Race:'' <<print _tempSpecie.name>>
''Origin:'' <<print _tempSpecie.origin>>
''Charasteristics: '' <<print _tempSpecie.charasteristic>>
''Description:'' <<print _tempSpecie.description>>

and the link to get tpl_speciesInfoScreen with correct data is

<<link "bafarians" "tpl_speciesInfoScreen">>
	<<script>>
		<<set _tempSpecie to $bafarnians)>>
	<</script>>
<</link>>

Error. <>: bad evaluation: Unexpected token ‘<<’

Im bit confused, did i miss something while readin from harlow to sugarcube guide?

1 Like

You have a couple of problems in your code there.

First, you’re creating a Map object incorrectly. If you’re going to use a Map like that, then the code should be like this:

<<set $bafarians to new Map([["name", "Bafarians"], ["origin", "xxx"], ["charasteristic", "yyy"], ["description", "zzz"]])>>

See the Map() constructor documentation for details.

That said, rather than using a Map object, you’d probably be better off using a generic object instead, like this:

<<set $bafarians = { name: "Bafarians", origin: "xxx", charasteristic: "yyy", description: "zzz" }>>

Generic objects are a lot easier to work with than Maps in most cases.

Next, you have three problems with this code:

<<link "bafarians" "tpl_speciesInfoScreen">>
	<<script>>
		<<set _tempSpecie to $bafarnians)>>
	<</script>>
<</link>>

The first problem is that you’re attempting to use a SugarCube macro within a <<script>> macro. The only thing which should be inside of a <<script>> macro is JavaScript code, thus there is no need for that <<script>> macro to be there at all.

Second is that you’re setting the value of a temporary variable (_tempSpecie) just before you go to another passage, which clears out all of the temporary variables. Also, there’s an extra “)” after “$bafarnians”.

If you need to do something like that, then your code should be more like this:

<<link "bafarians" "tpl_speciesInfoScreen">>
	<<set $tempSpecie = "$bafarnians">>
<</link>>

and then, in the “tpl_speciesInfoScreen” passage, the code would look like this:

<<set _tempSpecie = State.getVar($tempSpecie)>><<unset $tempSpecie>>\
''Race:'' <<print _tempSpecie.name>>
''Origin:'' <<print _tempSpecie.origin>>
''Charasteristics: '' <<print _tempSpecie.charasteristic>>
''Description:'' <<print _tempSpecie.description>>

That uses the State.getVar() method to turn the string “$bafarnians” into the value of the variable $bafarnians, and then uses the <<unset>> macro to get rid of the $tempSpecie variable, since you no longer need it.

Hope that helps! :grinning:

1 Like

Morning HiEv.

Ok, i think i got it now. Next time ill work with sugarcube without moving Harlowe street.

Thanks, EK

HiEv,

just for curiosity, how long have you worked with Twine & sugarcube? You seem very brainy with those. Luck for the community, you are also very helpfull.

Rock on dude.

1 Like

I’ve worked with Twine since late 2017, but I’ve had a variety of experiences working with JavaScript and HTML prior to that (dating back to 1996, even before CSS existed). I’ve also learned literally a dozen different programming languages (not counting all of the various versions of those languages).

Anyways, thanks, I enjoy helping other people do the thing I love. :smiling_face_with_three_hearts:

2 Likes