Switch Action

Discussion in 'Buildbox 3.0' started by Kanishk Sachdeva, Oct 4, 2018.

  1. Kanishk Sachdeva

    Kanishk Sachdeva Avid Boxer

    Joined:
    Jul 10, 2018
    Messages:
    217
    Likes Received:
    16
    What are the possible uses for switch action ? Can it be used to switch character color like in BB2 there was an option for Character Animation Overlap to change character color or character Animation Replace to change character color ?
     
  2. Codifie

    Codifie Avid Boxer

    Joined:
    Apr 17, 2018
    Messages:
    364
    Likes Received:
    190
    Switch is used for a zig-zag style type pf play, basically switches the direction of character. Its a touch move like Joystick or Swipe.
    If you are wanting to switch colors there is a Set Color node in BB3 that works perfectly and allows you to Set Color of character or any object based on actions.
     
  3. DanFarfan

    DanFarfan Avid Boxer

    Joined:
    Sep 22, 2018
    Messages:
    101
    Likes Received:
    42
    To OP,
    Yes. Switch has many, many applications.

    Here is the javascript code that comes inside Switch.
    var _switched = false;

    function init(){
    this.emitSignal('A', true);
    this.emitSignal('B', false);
    }

    function update(dt){
    }


    function signal(name, value){
    if(value==true){
    if(_switched){
    this.emitSignal('A', true);
    this.emitSignal('B', false);
    } else{
    this.emitSignal('A', false);
    this.emitSignal('B', true);
    }
    _switched = !_switched
    } / if switched
    } // signal

    Notice it has nothing to do with motion or color or scale or music or, or, or.
    What it does have to do with is sending true or false over the "connector" to 2 other components.
    Those other components are where the either / or action is taken - motion, color scale, anything.

    Imagine your game has a vehicle that instead of a single speed ( boring ;-) it has idle, first gear, second gear...
    You can use the Switch component to make ONLY the sound that goes with the current gear play (true) and all others are quiet.(false)

    Good luck!
    @DanFarfan
     
  4. Kanishk Sachdeva

    Kanishk Sachdeva Avid Boxer

    Joined:
    Jul 10, 2018
    Messages:
    217
    Likes Received:
    16
    Though I don't understand the code but I still got the gist of what you are trying to explain.
     
  5. DanFarfan

    DanFarfan Avid Boxer

    Joined:
    Sep 22, 2018
    Messages:
    101
    Likes Received:
    42
    Well, one thing I noticed is code should always be written in Courier font...
    so that spaces are respected.

    lol
    Let me try again... I will add more comments.

    // this is better.. more readable :)
    // A variable declared outside all functions is available to all functions.
    // By available, I mean it can be seen by and used in all functions.
    // We'll see in a little bit what this variable means when it is true
    // and when it is false.
    //
    var _switched = false;

    // The init function is called (once) by Buildbox after Buildbox loads the component successfully.
    // It is important to realize that there are some kinds of errors you can make in your code
    // that result in Buildbox NOT calling the init() function.
    // For testing, especially in the beginning, it's helpful to place a log( ) statement
    // so you get visual confirmation when the component is loaded. I use entry and exit
    // log messages when I'm concerned about a function completing properly.
    //
    function init(){
    log('init start');
    this.emitSignal('A', true);
    this.emitSignal('B', false);
    log('init end');

    } // init

    // buildbox calls this function 60 times per second.
    // It's not used for this example.
    function update(dt){
    }

    //Buildbox calls this function when the component connected
    // to this guys input has sent information from his output to this component's input
    // (i.e. the graphical display that shows all the components and how they connect.
    //
    function signal(name, value){
    if(value==true){
    // here's where _switched comes in
    // _switched remembers if "A" is on and "B" is off
    // or if "A" is off and "B" is on.

    // every time the component that feeds into the input of this component
    // sends us value = true,
    // the components that are connected to this component
    // (known to this component as "A" and "B")
    // change from true to false.. whatever that means to the running program.
    // As I described before it could mean that sound clip A plays instead of B.
    // or the other way around.
    if(_switched){
    // it is important to understand that if (_switched) and if(_switched == true) are identical.
    // The one used here is a shortcut way to write it.
    // This part of the code (until the else) will execute when the _switched variable is true.
    //
    // the emitSignal is the command that sends information from
    // this components output to another components input.
    // (again, the lines that connect components in the mindmap)
    // in this case, the component connected to the "A" output is going to
    // get the equivalent of signal("A", true);
    // See the function signal(name value) up above in this code?
    // That has to be in every script in every component so the components can communicate
    //
    this.emitSignal('A', true);
    this.emitSignal('B', false);
    } else{
    // this code is executed when that if statement above finds _switched equals false.
    // Notice it switched which gets false sent to it and which gets false.
    this.emitSignal('A', false);
    this.emitSignal('B', true);
    }
    // and finally, this command changes the value of _switched.
    // if it is true now, it becomes false, and vice versa.
    // That's what the "!" is. It's called the "not operator."
    _switched = !_switched;
    } / if switched
    } // signal


    Good luck !
    @DanFarfan
     
    LBPToo and Kanishk Sachdeva like this.
  6. Kanishk Sachdeva

    Kanishk Sachdeva Avid Boxer

    Joined:
    Jul 10, 2018
    Messages:
    217
    Likes Received:
    16
    Thanks a lot, it makes sense now . I really appreciate you took time to explain it again .
     
  7. DanFarfan

    DanFarfan Avid Boxer

    Joined:
    Sep 22, 2018
    Messages:
    101
    Likes Received:
    42

Share This Page