Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Equivalent of Datamapping in SugarCube Question

edited January 2016 in Help! with 2.0
Hello,

I was wondering if there is a way to do datamapping in SugarCube (the latest version). I found what someone called the equivalent of it, but it didn't show any usage examples or whether or not it works with IF statements. So I tried it myself using the following code.

This was the object's variable.
<<set $testobject to {name: "test object", description: "This is an object which I am using to test SugarCube's datamapping abilities.", acquired: "You grab the object, participating against your will in the test.", carried: false, acquired: false}>>


This was the code that was supposed to pull the object's description up.
<<if $testobject.acquired == false>>
	<<click "Test Object">>
		<<set $testobject.acquired to true>>
		<<set $testobject.carried to true>>
		<<set $selectedobject to $testobject>>
		<<goto "item acquired">>
	<</click>>
<</if>>


This is the code that was recommended/called the equivalent. i.e., what I was going off of.
<<set $person to {name: "bob", age: 18}>>

Your name is: <<print $person.name>> and you are <<print $person.age>> years old.

Comments

  • actually.. silly me, I may realize what was causing the problem. I was accidentally defining two "acquired" variables.... let me see if that works.
  • edited January 2016
    Ahhh... yes, that was it. My bad. Thanks, anyways. Though, if there is a better way to do this, I would love to know.
  • edited January 2016
    Those are simply generic objects, which will work for what you're doing. Harlowe's datamaps are actually just JavaScript Map objects with some syntactical sugar wrapped around them (similarly, datasets are Set objects).

    You do have an issue, however. In the following line, you're assigning a $testobject reference to $selectedobject:
    […]
    		<<set $selectedobject to $testobject>>
    […]
    
    Because of the way the story history works, references are unsafe and will break at some point (specifically, both objects will be cloned and will no longer refer to the same object; each will have its own copy of the original).


    Edit: Also. Instead of the following:
    	<<click "Test Object">>
    		[…]
    		<<goto "item acquired">>
    	<</click>>
    
    I'd suggest either:
    	<<click "Test Object" "item acquired">>
    		[…]
    	<</click>>
    
    Or:
    	<<click [[Test Object|item acquired]]>>
    		[…]
    	<</click>>
    
    You're not doing anything which requires <<goto>>.
Sign In or Register to comment.