Next Topic – Coding Tip of the Week
About Tip of the Week
Basically, I came up with this after I used a couple of really neat tricks to code efficiently in E-Pad, and they are definitely worth sharing. Every week or two, I will post a cool little coding trick for y’all to work with. About these ideas:
- They can be very simple and sometimes obvious, while other times they may be a little more far-fetched.
- Sometimes these tips require tinkering with code in the JSON file, other times they don’t. When that is the case, you can always do it with my Shortcut, through a link to an open-sourced project, or you can send me a link to add the trick into. Others can also help with that as well.
- I will teach you how to get the trick, how to use it, and some use cases for the trick, so that way, you can actually apply these to your projects!
- I keep these tips fresh and original, so if I cannot come up with any, then I just haven’t had an idea. Read more below.
Once again, from an AE topic – You can earn ePoints through this. Here’s how:
- Suppose it’s been 3-4 weeks and I have not had an idea. You must first wait for me to announce this, but I will post a cue to suggest an idea. The first unique, original, or brilliant idea holds a reward of 20 ePoints. Way to code!
- Help others in need! I will be reading through every reply of this topic. Any time you successfully help somebody else understand a concept, you will be rewarded with 5-15 ePoints (this varies on the tip and the explanation). @HopscotchRemixer and leads, this one’s for you, adopt this currency system lol
- The One-Up : This one has never been seen before. Go ahead, I encourage you to 1UP me on my own tips. Make it better, easier to access, more efficient, or give another use case for the weekly (or biweekly) tip. ePoint rewards vary.
- Show your skills! First five to tag me with a project get ePoints for how well they used the skill and how impressive the project is. ePoints vary.
This Week's Tip
Multiplying by Conditionals
Now this is a fun one. I am using it in E-Pad and already used it in Finders Keepers. Difficulty: 4/5
The Fundamental Mechanic – How Conditionals Work
Here’s a snippet of code from my open-sourced demo project:
If you play the project, you can see exactly how conditionals work. If your finger is on the left side of the screen, the text will display 1 because the statement is true.
This is the foundation of our whole concept. We can manipulate this to any condition and set it to any number, which is what I have done.
Usage Example 1 – Your Typical Conditional
Here, you can see the last game rule in my full concept.
Photo edited using Workflow for iOS 11
Try to notice that the only conditional that really matters is the one in the parameter. It’s like an if block or when rule. If those would be run, then this will be one. (Try to remember that phrase )
For our example: If both Value 1 and Value 2 are equal to one, then the “outmost” conditional is true. Seen in the Fundamental Mechanic, the number value outputted by the conditional is one. If either is not equal to one, then the outer conditional outputs a zero.
When it’s true: The Turn Degrees block tells Robo to rotate ( 1 × -360 ), or 360 degrees clockwise. The conditional outputs a 1, and it is multiplied by -360. I think you’re starting to get it by now.
When it’s false: The Turn Degrees block tells Robo to rotate ( 0 × -360 ), or zero. Since the conditional was false, it outputs a zero and Robo does not turn.
Important Note: We multiply the conditional 9 out of 10 times because then you can set the output to be either your number or nothing. Example: Turn “nothing at all” or “a full circle”. This eliminates the use of an if block and helps keep your code in less space. It may be hard to read at first, but you will get used to it. It may not be the best with a simple statement like this, but it is far more useful for managing clones. See the next example.
Usage Example 2 – Grouping
This is another basic conditional. In fact, it’s simpler than the first. But here is a more practical way of using it.
The Y Position is managed by modulo, or remainder. See the folder “Full Picture” for an explanation on that
This method increases the readability of the code. You can see an easy-to-understand explanation in the image itself. Here’s a comparison:
The top code is read: If Clone Index is greater than 3, add 512 to x.
The bottom code is read: Offset the clone index and divide it into groups of 3, then multiply the group ID by 512.
Both pieces of code add the same number for clones 1-6, but the top one is much easier to read through.
The bottom code is more efficient with more groups, but take “Finders Keepers” for example. The “Singleplayer” and “Multiplayer” buttons are one object. I add some number to set the object to the right side if it belongs to multiplayer (clones 4-6).
Usage Example 3 – Adding it Up (Lists)
This may look complicated at first, I know. But, try to look at as “groups” of conditionals.
Excuse the fact that I had to cover up a duplicate if Clone Index = 4. I ended up needing that room
I’ll explain a little more than the given description, but here’s how it goes:
There are 3 groups, right? You can see that all of them add up.
Each group has a conditional multiplied by a number.
Group 1: If (Clone Index = 1 or 3) output 23
Group 2: If (Clone Index = 2 or 5) output 8
Group 3: If (Clone Index = 4 or 6) output 17
In our example, each group will either give its output or zero. Also in our example, only one group can be true.
Here are the possible outcomes
Group 1 True: The equation is [ ( 1 × 23 ) + ( 0 × 8 ) ( 0 ×17 ) ] = ( 23 + 0 + 0 ) = 23
Group 2 True: The equation is [ ( 0 × 23 ) + ( 1 × 8 ) ( 0 ×17 ) ] = ( 0 + 8 + 0 ) = 8
Group 3 True: The equation is [ ( 0 × 23 ) + ( 0 × 8 ) ( 1 ×17 ) ] = ( 0 + 0 + 17 ) = 17
This is how much space it would take to write the same code. It is fine for those who are new or still gaining experience. But, this technique is for those who are seeking a new challenge. Minifying code like this is super helpful in large projects that would otherwise be filled with far too many if blocks. (If blocks because the “if statement” is still there).
Final Result of Our Code:
Robos only turn when both switches are on, and colors applied successfully. Clones 1 and 3 are normal, 2 and 5 are orange, and 3 and 6 are blue.
In the end, it’s your decision. If you’re not ready for it, don’t use it. If you need to tidy your code in a large project, this is one method of doing that.
Rate my service by quality of teaching not by if you would use it:
Feedback appreciated as always
The First 1UP goes to @MISSION_IMPOSSIBLE, congratulations!
MISSION_IMPOSSIBLE reminded me of a glitch that allows you to place operators in the wrong spot. Here’s how you can use this to get conditionals in the multiplication operator.
- Have the following set up: One “check once if” block with a conditional, one “multiply” operator.
- Begin to drag the conditional to the multiply. Start quickly tapping the original parameter, then click the multiply slot. When the multiply slot has a teal outline, drop the conditional onto it. It should show up. This trick can also be used with other draggable items.
See post 20 or tag MISSION_IMPOSSIBLE for additional help.
+10 ePoints and a !