Self Variables

Help spread the word!Share on FacebookShare on TumblrTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditFlattr the author

This plugin introduces the concept of “Self Variables” for your events.

Similar to Self Switches, each event can hold its own set of self-variables, which you can use to keep track of things like how many times you’ve interacted with the event, or the last position of the player that this event has seen, or anything else that you would like each event to keep track of on their own.

An event can have as many self-variables you want as long as you find an appropriate name for them.

Unlike self-switches, which only hold a true or false value, self-variables can hold any kind of information. Because you can access them using script calls, they can be used in conditional branches and other parts of your event to create intricate events that interact with the player or the game itself!

Download

Plugin: download here (right-click, save-as)

Related Plugins

Installation

Download ths plugin and place it in the “plugins” folder in your project’s “js” folder. Then open your Plugin Manager (F10), double-click an empty row, and select the HIME_SelfVariables plugin.

Once it is in your list of plugins, turn the plugin on.

Usage

To assign a value to a self-variable, use the script call

this.set_self_variable(NAME, VALUE)

Where the NAME is the name of the self-variable you want to use, and the VALUE is some value that you want to assign.

For example, it could be a number

this.set_self_variable("check_times", 1)

Which would set the variable called “check_times” to 1. You can then get the value of this self-variable using the script call

this.get_self_variable("check_times")

Which will return the value 1.

The value of the variable can be anything you want, including numbers, strings, objects, booleans, functions, and so on. There is no restriction to what you would like to store, as long as you’re consistent.

Working with other events

Notice that the only two pieces of information you provide are

1. the variable name
2. the value (when setting a value)

This is because we assume you are operating on the current event, on the current map. You may also choose to operate on other events by specifying event ID’s and map ID’s in your script calls.

this.set_self_variable(NAME, VALUE, EVENT_ID)
this.set_self_variable(NAME, VALUE, EVENT_ID, MAP_ID)

For example if you write

this.set_self_variable("test", 3, 4)

It will set the self-variable “test” to value 3, for event 4.
If you specify event 0, it will mean the current event.

If you write

this.set_self_variable("test", 3, 4, 2)

Then you are setting the self-variable “test” to 3, for event 4, on map 2.

Similar script calls are available for getting self-variables:

this.get_self_variable(NAME, EVENTID)
this.get_self_variable(NAME, EVENTID, MAPID)

Working with self-variables and numbers

Unlike regular variables, you don’t have a nice editor dialog to work with. So if you wanted to manipulate them, you will have to use some script calls.

For example, let’s say you wanted to keep track of the number of times you’ve interacted with the event. The first time you interact with it, you would say

this.set_self_variable("interact_count", 1)

The next time you interact with it again, you would start by getting the value of the self-variable, increase it by 1, and then assigning that to the variable.

var oldCount = this.get_self_variable("interact_count") || 0;
var newCount = oldCount + 1;
this.set_self_variable("interact_count", newCount);

Note the extra part in that first line. This is a shortcut in case that variable never had a value to begin with. This may be useful if you’re strictly working with numbers.

Working with self-variables and strings

You can store strings (or “text”) inside self-variables as well. For example, you could say

this.set_self_variable("myName", "tester")

And the variable called `myName` will now hold the value “tester”.

If you wanted to check the value of this variable equals “tester, you could something like

this.get_self_variable("myName") === "tester"

Which will return true or false depending on what the value of the variable is.

 

Help spread the word!Share on FacebookShare on TumblrTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditFlattr the author

You may also like...

34 Responses

  1. Ekop says:

    Wouldn’t it be shorter to use;
    this.set_self_variable(“counter”, this.get_self_variable(“counter”) + 1);
    instead of;
    var oldCount = this.get_self_variable(“counter”);
    var newCount = oldCount + 1;
    this.set_self_variable(“counter”, newCount);
    Or will this can possibly cause an error?

  2. Trent says:

    Can I use self variables with Common events in any way?

    • Hime says:

      It is possible, depending on how you use the common event.
      Self-variables are associated with a particular event, based on the event ID and map ID which is used to uniquely identify an event (under the assumption that the combination is unique).

      Now, what happens with common event is if a map event calls a common event, the common event will have the same event and map ID as the calling event.
      So if you had a common event that increments self-variable “Call Times” by 1 everytime it’s called, and you had 3 events calling it, each event would have its OWN “Call Times” variable incremented each time.

      If the common event is called by any other method, such as an item use or something, the behavior is undefined. Meaning, I don’t know what will happen, because it is not intended to be used for that.

      • Trent says:

        Thanks for the great reply. I think this will work perfectly for me.
        I wanted to use it in common events to clean up the amount of self variables used my other events.

        Thanks again.

  3. Zack Wood says:

    Thank you for this. I’m using it in conjunction with Hidden Choices to hide a choice if you have previously selected it (so that you could theoretically go through the dialogue over and over choosing each choice one by one).

    So, the first time a player selects a choice, I added this:
    Script: this.set_self_variable(“Chose1”, 1)

    Then, when checking whether to hide the choice next time I added this:
    If: Script: this.get_self_variable(“Chose1″===1)
    Plugin Command: hide_choice 1

    But, the choice still appears the second time. What am I missing here?

    • Zack says:

      I got ti! It should be:
      Script: this.get_self_variable(“Chose1″)===1
      Right? Woops, sorry! I’m learning this whole script call thing for the first time with this plugin.

  4. Pisces says:

    Omg! Thank you!! This is exactly what I needed to make an ABS combat for my rpg!

  5. Bob says:

    Hi, i’m using your plugin for a tree-chopping event.
    so I set it up as:
    this.set_self_variable(“tree_hp”, 10)
    then on action button:
    var A = this.get_self_variable(“tree_hp”)
    var B = var A – 1
    this.set_self_variable(“tree_hp”, B).
    So how do I make a new event page that activate when value of “tree_hp” is less than or equal to 0?

  6. Anonymous says:

    Doesn’t work.
    TypeError. Cannot read property ‘setValue’ of undefined.

    • Hime says:

      You will need to provide more information. How are you using the plugin?

      • Anonymous says:

        Whoops, nevermind. It works after I start the playtest as new game rather than continuing from a save file.
        Anyway, what was the script call to set the number to a random value again?
        Eg: this.set_self_variable(“dice”, Math.floor(Math.random()*6+1))) // doesn’t seem to work.
        Thanks for your help!

  7. Feufoll says:

    Awesome Plugin, just what i was searching for ! But can we use other things than fixed numbers ?

  8. Feufoll says:

    Awesome plugin, just what i was searching for ! But can we make the self variable equal to the place X of the event, or the place Y and other variable things ?

    • Hime says:

      Yes. You will need to be able to reference the event in order to do this.
      For example, you might say

      this.set_self_variable(“eventX”, $gameMap.event(this._eventId).x)

  9. Aitor Rosell Torralba says:

    Is possible to set selfvariables to the actors?, for example to create new stats like Charism, or replace the default stats, whit something like STR, VIT, INT, WIS, DEX….

  10. Satoya says:

    Hi, could you tell me how to use this together with Custom Page Conditions?
    I tried using temporal variable to check conditional branch but this is not working.

  11. Josh Bundy says:

    This is an amazing script, but I can’t seem to get it to work with conditional branches. I set my variable name and value as such:

    this.set_self_variable(“GACTION”, 0)

    Then I try to use it in a conditional branch like so:

    this.get_self_variable(“GACTION”) === “6”

    What am I doing wrong.

    Thank you for the help and the awesome plugins.

    • Hime says:

      0 is a number. “6” is a string.
      You need to compare numbers with numbers, and strings with strings.
      this.get_self_variable(“GACTION”) === 6

      • Josh Bundy says:

        Thank you for the fast reply! I tried that before, with no luck. I got the error “Cannot read property ‘X’ of undefined”. I also tried comparing the strings listed in the script help, with the same error.
        I set using: this.set_self_variable(“myName”, “tester”)
        I did the branch with: this.get_self_variable(“myName”) === “tester”
        What am I missing?

  12. CookieNinja says:

    Hey, this is an awsome plugin that sure helps to tidy up the database. I tried to use this together with Conditional Choice Text.

    This is the sciptcall I used:

    choice_text(1, "Rookie Fight(check)", "this.get_self_variable("Rookie_Fight") === 1")

    But when I playtest and try to trigger the event it returns Syntax error: Unexpected identifier. Is this an error on my part or is it a compatibility error? I know this line is causing the error because without it everything run exactly the way it is supposed to.

Leave a Reply

Your email address will not be published.

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax