Howdy, Stranger!

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

Having trouble modifying a variable in state.history.variables [SOLVED]

edited April 2014 in Help! with 1.x
I'm trying to modify a variable using a custom macro and it is not working, not even a little bit. I figured out (thank you, console.log) that if I do this...
<<nobr>><<set $translation = {
isEnabled: false,
textToDisplay: "Translation object initiation failed!"
}>>
<<initTranslationObject $translation>>
<<display 'Beginning'>><<endnobr>>
...that the value passed to the initTranslationObject macro is not the object stored in $translation, but rather the string "$translation". Armed with that knowledge and having read about state.history.variables on this forum, I made this definition for the initTranslationObject macro:
macros['initTranslationObject'] = {
handler: function (place, macroName, params, parser) {
"use strict";
var objName = params[0].slice(1); // Removing the $
state.history[0].variables[objName] = {
isEnabled = false;
textToDisplay = "Translation object successfully initialized.",
};
}
};
Unfortunately, this is somehow broken and prevents the script passage from being incorporated into things. I do not understand. :( I submit myself to thy greater knowledge. Halp!

Comments

  • You've botched this object literal:

    state.history[0].variables[objName] = {
    isEnabled = false;
    textToDisplay = "Translation object successfully initialized.",
    };
    Inside of an object literal: the colon separates property names from their values, not the equal sign, and the comma is used to separate property/value pairs, not the semi-colon.

    IOW, it should look like this:

    state.history[0].variables[objName] = {
    isEnabled: false,
    textToDisplay: "Translation object successfully initialized."
    };
  • I sure did! Apparently I suck at JavaScript when there's no syntax highlighting. Thanks for helping me with my noob mistakes.
  • Crowbeak wrote:

    I sure did! Apparently I suck at JavaScript when there's no syntax highlighting. Thanks for helping me with my noob mistakes.


    So this script grabs a variable from twine and makes it usable in the javascript by removing the $ that is required in twine.

    So how do you translate or pass back that variable after its been altered in the script back to twine? I can't seem to find a basic macro script example of how to do this.

    Are variables created in a script local to themselves?



  • When I pass the variable from Twine to the macro, only the name of the variable (including the $ sigil) is passed, not the actual JavaScript object that holds the variable's value. By removing the sigil, I can use it as a key for accessing the object of that name in state.history[0].variables in JavaScript. Basically, I'm just getting the variable name from Twine so I can modify the underlying JavaScript object of the same name. There's no need to pass anything back because I'm not returning a value to assign to the variable, just modifying the variable directly.
  • Crowbeak wrote:

    When I pass the variable from Twine to the macro, only the name of the variable (including the $ sigil) is passed, not the actual JavaScript object that holds the variable's value. By removing the sigil, I can use it as a key for accessing the object of that name in state.history[0].variables in JavaScript. Basically, I'm just getting the variable name from Twine so I can modify the underlying JavaScript object of the same name. There's no need to pass anything back because I'm not returning a value to assign to the variable, just modifying the variable directly.


    Okay so when you talk about the javascript object of the same name do you mean- since javascript is the foundation of twine(?) and any objects declared in twine is rooted back to javascript underneath as well, therefore an object declared or created in twine will also exist in javascript form as well when created. When wanting to refer to it in a script to do so as per your example and removing the sigil from the variable name?

    So any changes to values in a script/javascript to variables or values like in your example you are literally updating/changing the same variable and thus have no need to pass the update back to twine? is that what you were saying or have i misinterpreted you completely?

    Concerning the term object literal;
    [quote]You use literals to represent values in JavaScript. These are fixed values, not variables, that you literally provide in your script.
    from https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Literals

    For clearer definition, an object literal is in a manner of speaking is like a label- VehicleType being a label eg. VehicleType: "jeep"?

    In the following example in twine if you had <<set $Transport ={VehicleType: "Jeep"}>> are both $Transport and VehicleType object literals?

    Would you refer to $Transport as the parent or parent object when referring to it or it is the object property and what is assigned to it is object properties. All of which are object literals? i don't know why its called an object literal if all objects are the same anyway.

    Sorry if I asked too many questions just trying to get the terminologies correct.






  • Dazakiwi38 wrote:


    Okay so when you talk about the javascript object of the same name do you mean- since javascript is the foundation of twine(?) and any objects declared in twine is rooted back to javascript underneath as well, therefore an object declared or created in twine will also exist in javascript form as well when created. When wanting to refer to it in a script to do so as per your example and removing the sigil from the variable name?

    So any changes to values in a script/javascript to variables or values like in your example you are literally updating/changing the same variable and thus have no need to pass the update back to twine? is that what you were saying or have i misinterpreted you completely?

    You have interpreted me correctly; that is exactly what I meant.

    Regarding your (?) about JavaScript being the "foundation" of Twine, I would argue that "one of the building blocks" is a better way to put it. There are multiple things going on under the hood of Twine. JavaScript is the building block that handles the variables in the Twine game, though.

    [quote]
    Concerning the term object literal...

    I think you'll understand this one better if I explain some basic things first, so here goes:
    • Objects: In JavaScript, pretty much everything is an object. Numbers, variables, functions, you name it.
    • Properties: These are pieces of information stored within the object. An array object, for example, has a length property that tells you how many other objects (strings, numbers, etc.) are stored in the array. Every property has a name and some kind of value to which the name refers.
    • Accessing Properties: There are two ways to access the properties of an object. One way is called dot notation. For example, to access the property called 'length' for an array called 'myArray': myArray.length . You can also use... uh... I think it's called bracket notation. Example: myArray['length'] . The former notation requires you to know the exact name of the property; the latter allows you to use a string stored in a variable, as I did above (I took the variable name passed to the macro from Twine, stripped the sigil off of it because that's a Twine thing and not a JavaScript thing, and used that string with bracket notation to call up the appropriate variable).
    This brings me to object literals. An object literal is a way of declaring an object by explicitly defining all of its property names and their values. In plain JavaScript, it looks something like the following:
    var dog = {
    smell: "bad",
    ageInDogYears: 28
    }
    This snippet of code creates a variable dog, which is an object having two properties. One property is dog.smell and its value is a string containing the letters "bad". The other is dog.ageInDogYears and its value is the number 28.

    So in your <<set $Transport ={VehicleType: "Jeep"}>> example, you are creating or updating a variable called $Transport in Twine (and state.history[0].variables.Transport in the underlying JavaScript) which is an object with only one property: Transport.VehicleType, the value of which is a string containing the letters "Jeep".

    As a related side note, in JavaScript, it's conventional to name variables and properties starting with a lowercase letter.

    [quote]
    Sorry if I asked too many questions just trying to get the terminologies correct.

    Do not apologize! Questions lead to answers; answers lead to understanding; understanding leads to \o/ EUREKA!
Sign In or Register to comment.