Blocksworld Wiki
Register
Advertisement
Block
This article needs more links to other articles. Please help improve this article by adding links that are relevant to the context within the existing text.
(October 2020)


A Toggle System in Blocksworld

A toggle system means that an object can switch between different states using only a single conditon. Using the Blocksworld programmation system (and with some thinking ;), this process can easily be done.

Before Starting

First, you have to understand the basic working of the Blocksworld's programmation system. Blocksworld use an "event-oriented" programmation working with icons. So, each icon represent an event. It can be a condition (bump, in water, etc.), also called an "if", or a reaction (explode, freeze, etc.), called a "then". And depending on what side of the "does" button you put it, it becomes a condition (left) or a reaction (right).

Let's now try to reproduce this situation: IF my block is blue, THEN turn to red.

  [Blue colored icon] does-> [Red colored icon]

Or, simplified:

  [Red] -> [Blue]

Basic States

States become very importants when coming to make advanced builds, like an airplane or a car. Sometimes, we will have to know if an object is on water or on the ground. For exemple, let's take a boat. So what we want to do is building a boat that will transform itself into a car when coming to the outside of the water. For doing this, we will need States.

States are often stocked on a single block, and are using colors or, more discret, textures. Also, we will use the Model functions. For that, just tap on an icon that has an arrow on the corner (like bump or ground) to apply it for the whole model.

First, let's put this programmation on a block:(P.S.: No need to build a boat! It's only an exemple!)

  [In Water] -> [Red]
  [On Ground] -> [Blue]

And there we go! We build our first states!

Q.: Why are we putting this? We can simply put In Water condition on each action blocks!

A.: Yes. But when we will have a lot of conditions regrouped together, it will be very long to copy them on each different blocks. Also, when coming to make some condition changes, you will have to modify only one block instead of 10! That will be way more efficient! Also, it's faster for the engine to read complicated conditions on only one block instead of 10. And finally, it's well organized like this.

Now, we want to "tell" other parts of the boat (like motors of flaps) on with state we are. So, let's put something that is avaible for everyblocks at the same time: SIGNALS! For that, just ADD these lines:

  [Red] -> [A signal]
  [Blue] -> [B signal]

Now, we know that the A signal means that the boat is on the water, and that the B signal means on the ground.

Q.: Why are we using all these lines? [In water] -> [A signal] will be way more usefull!

A.: Of course. But it's going to be way more clear when comming to complicated things, you will see ;)

Toggles

Toggle systems are VERY usefull. It means that for only one condition, an object can act two different ways. And for sure, it will use States.

First, let's take this example: Our car, when hitting the button "L", will toggles between hovering mode (floating) and wheel mode (on the ground). Now, let's put our "state indicator" block on a world. You don't know how to make a toggle system, right? Let's remembering that L will indicate a state and L will indicate another state. Weird, isn't it? So if we resume, it should do this:

  [L] -> [Red]
  [L] -> [Blue]

And... It clearly doesn't work. Why? Because the L condition needs an ADDITIONAL condition to work. This special condition will be the actual state! Now, let's put this programmation instead: (P.S.: "//" means a comment. Not to use on the programmation!)

  [On Play] -> [Blue]
  [L] [Blue] -> [Red]
  [L] [Red] -> [Blue]

Hit play and press the button L. Now, we see that it's switching between Blue and Red only with a single condition, the button L. But it's going to fast, right? So let's just put some "wait" functions.

P.S.: Underscore means the power, speed or delay, depending on the function. Delays are counted in seconds. If there are no underscores, it means it's on default.

  [L] [Blue] -> [Wait_1] [Red]
  [L] [Red] -> [Wait_1] [Blue]

So now, it takes 1 second between each state's changes. That means you have to press less than one second to make the state change only once. The problem is that it will keep switching if you don't release your finger, and that is the biggest mistake that pretty everyone does. But don't worry! On the sections below, I will teach you how to make it perfectly!

Code Priority

In the script-reading engine of Blocksworld, there are priorities. But don't worry!, it's very simple! Open a world and put a block on. Then, program this and it play:

  -> [Blue]
  -> [Red]

Now, wich color appears? Blue, Red or both?

And yes, only the Red color will show up. That means that when you have two similar actions (especially colors and textures), the last line will have priority on the under one, and going on. Keep this in mind, it will be usefull.

Using Default State

Now, using what we just learned, we are able to make a default state. This is a state that will always come back if there's no "true" conditions. It is used as a NOT function that will take place of an inverted condition (if we only used two complementary states). For exemple, what happen if I want the "in air" condition? It doesn't exist, right? And it's complimentary is "in water". Using the basic states and the scripting priority, we are able to setup a default state. Place a block near the water and put this programmation:

  -> [Red]
  [In Water] -> [Blue]

Hit play and see what happen when the block is on the water and when it's not touching it. Nice, hein? So that script will make the block turn Blue if it's in water. ELSE, it will turn red. So yes, it's primary an ELSE function (if something, then do... Else, do...), but because we only have 2 states, it more specificly act like a NOT function (if something, do... If not, do...).

Q.: Why are we putting -> [Red] on the first line? We can just put [In Water] -> [Blue] so it will turn blue when it will be in water, right?

A.: No. The problem is that the block will turn blue only once, it will not come back to it's original color. That's why we put -> [Red] on the first line.

Applying More States

So now, we are also able to put as many states as we want! As an example, let's take an agent car. We want it to have 3 states: on ground, in air and in water. Take a block and script this:

  -> Red
  [On Ground] -> Blue
  [In Water] -> Green
  /* Be carefull here: the Bump and Ground functions only works when the block is in motion. */

There we go: If it's In Water, it will turn Green. ELSE, if it's On Ground, it will be Blue. ELSE, it will be Red, that means In Air.

Combining States

If we are using only colors or only textures, there will be only one state showing at a time. So even if the car is on the water and touching the ground, it will only show that it's in water because of scripting priority. But if we MIXED colors and textures together, we will be able to show 2 different states at a time. For example, let's put that the In Water and it's invertion, In Air, will take colors and the Ground function will take texture:

  -> [Red] [No Texture]
  [In Water] -> [Blue]
  [On Ground] -> [Transparent]

There we go. We are now combining two states that uses their NOT function (invertion). So 1: IF the block is In Water, turn Blue. IF NOT, turn Red. 2: IF the block is On Ground, become Transparent (or whatever the texture). IF NOT, stay with No Texture. Using this, we now can use two Independent states. That means that, using Signals for each state, we can do specific actions and we can combine the states on the conditions. For example, IF In Water AND On Ground, do something. IF In Water and NOT On Ground, do something.

Toggle System with Combinated States

Let's go back to the first chapter: Our goal was to make a more efficient toggle system that will not keep switching between the states. For that, we are going to take ALL the concepts we just learned and apply them. Using our minds, we will be able to find a solution.

First, we want to have a toggle system. But, it needs another state on the conditions. And this state will be the Tap function. So, we will do a state with his complimentary. To summerize all that, here's what we are going to do:

  [On Play] -> [Red]
  -> [No Texture]
  [Tap] -> [Texture]
  [Tap] [No Texture] [Red] -> [Blue]
  [Tap] [No Texture] [Blue] -> [Red]

And yep, it's simple as that. When we Tap the block, it will change state once, and then will wait until we release our finger to be ready to change state again. That's the best and smaller state Toggle System you can have.

Going Further

With all the concepts you just learned, we will be able to go further than a 2 states toggle system.

More Than One State

We will add more states! We will simply take back our simple toggle system, but add another line with another color, so the system will handle 3 states.

  [On Play] -> [Red]
  -> [No Texture]
  [Tap] -> [Texture]
  [Tap] [No Texture] [Red] -> [Blue]
  [Tap] [No Texture] [Blue] -> [Yellow]
  [Tap] [No Texture] [Yellow] -> [Red]

The only problem we can see with that is that the states will always turn around in the same order, so that when the block is red and tapped, it will never become Yellow. But, you will be able to add as much states as you want to send different signals!

Returning to Default

In this system, we will only use 3 states. In this system, we will be able to always go back to one state after each 2 presses. E.g. If my block is yellow, when tap it will become red, then yellow again, blue, yellow, red, etc. So it always return to yellow, it's default state. As you can see, we will need two blocks for this. Here are the codes:

  /*First Block*/
  [On Play] -> [Yellow]
  -> [No Texture]
  [Tap] -> [Texture]
  [Tap] [No Texture] [Yellow] [D Signal] -> [Red]
  [Tap] [No Texture] [Yellow] [C Signal] -> [Blue]
  [Tap] [No Texture] [Red] -> [Yellow]
  [Tap] [No Texture] [Blue] -> [Yellow]
  [Red] -> [A Signal]
  [Blue] -> [B Signal]
  /*Second Block*/
  [On Play] -> [Red]
  [A Signal] -> [Red]
  [B Signal] -> [Blue]
  [Red] -> [C Signal]
  [Blue] -> [D Signal]

This is it! Now, the block, when red or blue and tapped, it will always return to the default state, yellow, which can be interpreted as "off". So, red will be "on" and blue will be "inverted-on". This system already contain Signals for sending states.

2 Conditions, 3 States, Returning to Default

Now, the problem with the "single condition returning to default state toggle system" is that it always go by red or blue. So if it was blue but now it's yellow and you want to put blie again, you will need to make it turn red before it goes blue again. We can for sure arrange this by adding another button, for exemple, which will be the second condition. So, you wil have two buttons: L and R. L will make the block turning Red and Yellow, R will do Blue and Yellow. When the block is red and you press R, it will turn back to Yellow. When you press L and it's blue, it will also turn back to Yellow. So then, you can decide to directly go to one state without turning the opposite color. We will only need one block for this. Here's the code:

  -> [No Texture]
  [L] -> [Texture]
  [R] -> [Texture]
  [L] [No Texture] [Yellow] -> [Red]
  [R] [No Texture] [Yellow] -> [Blue]
  [L] [No Texture] [Blue] -> [Yellow]
  [R] [No Texture] [Red] -> [Yellow]
  [L] [No Texture] [Red] -> [Yellow]
  [R] [No Texture] [Blue] -> [Yellow]

There we go. Try it!

If you want, you can change the conditions, textures and colors of any of the systems. BUT BE CAREFUL! If you made changes, make sure you change all of them in the right order! For exemple, if you want to change Red for Purple, be sure to change ALL the red colors to purple, or it will failed.

This course and concept were created by Lolgab123. Please leave any comments if you have questions! Also, visit me on Youtube(aka Gab St-Pierre) and Wikia or search for Lolgab123 builder on Blocksworld!

Screenshots

Will be soon added

Advertisement