Forum Discussion
Game of the watercans - SL360 + easy JS
Hi Everybody, here is an example of easy javascript used for a little game.
There are 3 cans. Can 1 is full, has 12 liters.
Can 2 (7 liter) and 3 (5 liter) are empty. You can pour the water from one can, into another, till its full.
Goal is to have 1 liter in 1 can.
Sounds not too bad? Can you think of a way to calculate this?
If you pour c1 into c2, only 7 liters will enter c2. 5 liters will remain in C1. Easy step one.
for every next step we need to know the following:
- how much water is in each can?
- how much water can be poured into another?
- What is the result? how much water is in each can?
when all the cans have content, there are 6 possibilities.
C1-->C2, C1-->C3, C2-->C1, C2-->C3, C3-->C1 and C3-->C1. (--> pours into)
So what do we need?
In JS we need variables (var) to calculate each of the 6 possibilties.
We need a drag and drop trigger, to move the cans and execute one of the scripts.
We need triggers to move the cans back in place after every step.
Here are the 3 variables
volume for can 1,2,3.
The C1 has 12 liters,
C2, C3 are empty, 0.
Next are the triggers.
So after each drop, 2 of the variables will have changed. We trigger to 'jump this slide' when this happens.
Did you know, you only reset the objects, the variables will not be reset!! It doesnt matter which can you move. All cans will be reset to startposition. I suppose you can remove one of these triggers, because any of the other 2 will have changed after each pour.
Object triggers:
Group 1 is can 1 with water, etc.
You can clearly see the logic, we needed 6 possible actions. we have 6 scripts which calculate how much water is in the cans you use.
Here is the script for C1 --> C2.
lines 2-7 will be the same for all the 6 scripts.
For those new to js in SL360. You need to retrieve the value of the variables from the player and rename them for usage in the script-shell.
This is what lines 5-6-7 do. We take the volumes of each can and name them can1vol, can2vol and can3vol.
lines 15-17 we export the new values back into the player.
lines 9-13 is where the magic happens.
L9: We create a var for how much room there still is in c2. It cannot be more than 7 liters.
so. 7 liter - whats inside now.
L10: Now we create the var transfer. 'Math.min' takes the lowest value of the named vars.
In this case 12 liters in C1, C2 is empty, to it can take 7 liters.
The lowest is 7, this will be the transfer value.
L12: newCan1 is the next variable. we take the volume in c1, 12 - the transfer value 7.
L13: newCan2 is the next, we take the volume in C2, 0 + the transfer value 7.
L15-17 we export back to the player, accordingly for the newCan values. (only C1 and C2 have changed, so new values to them. C3 didnt change.
Copy the script to all of the 5 remaining triggers and change lines 9-13.
Think clearly, can1 has 12 liters and C3 has 5 liters of space. so we use those values to change line 9.
in line 10 we change for each outcome how much is in the can you pour from and how much is in the can we drop.
We only change the vars that are in use, make sure you trigger the minus and plus to the right can in lines 12-13. :-D |
Make sure you use the newCan values for the correct cans.
In the project you will find all the scripts.
I have to excuse myself for not properly finishing the project, I still need to add states of the quantity of water in each can. Now c1 will always be full,
below each can, the values of how much is in there, works.
Hope anyone will find this usefull.
Have a great day!
Related Content
- 27 days ago