Howdy, Stranger!

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

Referencing code between rooms in Snowman

edited April 2016 in Help! with 2.0
Hi everyone. Just got started with Twinery. Tried to search for this but couldn't find it. I am using Twinery 2.0.11 with the default packaged Snowman 1.3.0 format.

How do I reference a variable/function defined from another room?

I am thinking of having an initial "room" which will just define my code's functions and variables. Say an init room. Inside this I will have various functions like getPlayerHp, getPlayerInventory, damagePlayerHp and want them to be referable from other points in my story.

In a few tutorials the "state" and "state.history" objects are referenced but when I try to use this in my code it states it is not defined.

Is there an object I can use as a global hook as a unifying reference point?

Is there anything wrong with assigning most of my unifying code to say the "window" object?

Thanks

Comments

  • re: reference a variable
    Snowman's documentation is brief but it does include how to reference story variables.
    s, which is a shorthand for window.story.state
    The following first shows how to assign a value to a story variable named health and then how to output the value in a passage.
    <% s.health = 100 %>
    
    health: <%= s.health %>
    

    re: reference a function
    Custom JavaScript should be placed within the dialog accessed via the Edit Story Javascript menu item.

    All functionality defined in the Story Javascript area has a local scope which means that it can not be referenced from other passages, one common method used to increase the scope to global is to attach that code to the global window object.

    I suggest using a Namespace like the one defined in the MDN JavaScript Object Management page to store your custom Javascript, the only change you will need to do is to attach that namespace to the window object.

    a. Define the Namespace and a test function:
    /**
     * S namespace.
     */
    if (typeof S == "undefined") {
    	var S = {
    		test: function() {
    			console.log('S.test: called');
    		}
    	};
    	
    	// Raise namespace scope to Global.
    	window.S = S;
    };
    
    b. Call test function within a passage:
    <% S.test() %>
    
Sign In or Register to comment.