HMTL5 Toggle

Hello. I currently use two buttons to toggle Selfishness skill on/off, but would very much like to turn this on and off by use of a quick alias. This is what I have, but I can't seem to get the values to change.


Comments

  • I assume you're using 1 to indicate selfishness is up, and 0 to indicate that it's down?

    If that's the case, two comments:

    The >= operator isn't the correct one to use (use == or === to check that it's equal to a specific value). If you have selfishness up and the variable is set to 1, then >= 0 is still going to be true (because 1 is greater than 0), so it will always send the "selfishness" command.

    Use boolean type variables (true and false) rather than an integer (1 and 0) for representing yes/no conditions, it's what they were designed for.
  • Hello, thank you for the reply. I was using the example they have in the manual. I really have no idea what I'm doing. I was able to get it to where selfishness would enable with this, but I guess it doesn't change the variable value for the reasons you mentioned. Was I close, or is there a specific format?

    Also, I seem to have issues with getting my settings to save to the server and load in with consistency. Wasn't sure where to mention it so just throwing it in here to let someone know. Thanks for you help.
  • You are very far. There's no shame into learning though.

    So, not only what @Antonius‌ said (btw, you are partially wrong, try 
    if(0==false){console.log("ok")}
    so you will see why you should mainly use === to avoid errors.). 

    @Kalam‌

    You forgot the brackets, and var number does not mean anything in your code. At least.
    image
  • edited November 2014
    If you want to make a toggle, there are two steps involved.

    The first is that you need a variable to store the state of the toggle (whether selfishness is up or not). Right now, you're doing that using the "client variables", but there's actually a much easier way: just use a normal JavaScript variable. After that, it's a simple matter of writing a little bit of script to toggle it.

    So here are the two steps:

    (1) You need to declare the variable somewhere before you can use it. So go to your reflexes, find the function called "onLoad", and add this to it:
    var selfishness = false;
    So every time you log in, the client is going to make a variable called "selfishness" and sets it to false (because you don't have selfishness up).

    (2) You need to make an alias that acts as a toggle. There's a sort of "trick" to this using Boolean data types (this is just a fancy way of saying variables that store true or false).
    if (selfishness) {
        client.send_direct("generosity");
    } else {
        client.send_direct("selfishness");
    }
    selfishness = !selfishness
    This probably looks confusing, but it's really pretty simple! So let's break it down.

    First you have "if (selfishness)", which is just a very common shorthand way of writing "if (selfishness == true)", which is just a way of asking "is selfishness set to true right now?". Incidentally, this shorthand is available in just about every programming language, not just JavaScript. Note the double equals, a single equals is used for setting the value of a variable, if you want to know whether something is equal to something else you have to use a double equals (or a triple equals, but don't worry about that for now).

    Then you have the first branch of the if, the thing that happens when the selfishness variable is true: it sends "generosity" to the game (because you have selfishness up already). If that condition isn't met ("else"), i.e. the selfishness variable isn't true, then it sends "selfishness" to the game (because you don't have selfishness up).

    And then you have the strange-looking line at the bottom. This is just saying "whatever the selfishness variable is, reverse it, and save that as the new value of selfishness". So if the variable was true before, now it's false, and vice versa. This is how you write a toggle variable.

    This script does have a minor problem in that, because selfishness takes balance, if you did this alias twice in a row quickly, the selfishness variable would be wrong (since it would try to send the other command the second time you did the alias, but you would be off balance). It's a minor problem because if it ever happens and you do the alias and nothing happens, you can just do the alias one more time and it will fix itself. The same thing will also happen if you toggle selfishness with the regular commands or a different alias/trigger rather than with this alias, but the same solution also works (just use the alias one more time whenever it doesn't work). The only way to actually solve that is to toggle the variable using triggers for the actual selfishness and generosity lines in the game, which is a little bit more complicated and not really necessary.



    (As an aside, I also disagree pretty seriously that you should most always use "===" over "==". That prevents you from doing incredibly useful things like treating undefined variables as false, which can end up resulting in a lot more work to do simple things. Both double equals and triple equals have their place. If anything, particularly when working on such simple coding as is typically involved in Achaea, I think double equals is usually the right choice.)
Sign In or Register to comment.