Slow running thunkable when creating long if do chains

I am building an app which includes an ‘if do, if else do’ block. But there are so far 32 ‘if else do’ conditions in it and there will be a lot more.
So far I have found thunkable running slow and I think it’s as a result of the amount of conditions. Is this likely the case? Am I better off splitting it into smaller function blocks and running the ‘if else’ conditions within there, then putting each function block in the button click block?

1 Like

Yes, it’s likely that all of your conditional (if/else) statements are slowing things down. Honestly, if I you add more than about 5 if/else blocks to a particular section of code, I’d stop and think about how you can code that more efficiently.

Why do you need so many conditionals? What are you trying to achieve? What do your blocks look like?

There are often ways to simplify code using things like Any Component blocks or math formulas that can dynamically check values rather than statically having to enter every possible value your users might encounter.

Hmm. Well I wanted to create a chess game from scratch. As much to teach myself a new skill as anything else.
I am doing this by defining the individual pieces as buttons. When the user clicks the piece it reveals another button that user can click to move to that square or attack an enemy piece. But these blocks only appear under certain circumstances.
I have already simplified it from having individual “movement” and “attack” buttons for each piece on the board. To now where the same “movement” and “attack” blocks move to their appropriate places depending on which piece is selected.
If there is an even simpler way that I do not understand I would be happy to learn.
I did try with the any component blocks and duplicating buttons/rows to create the chess board but to be honest I got confused as to how to define each part of the chess board. I’m a complete novice to things like this I’m afraid.

Yeah, so you’re taking a very complex project (making a chess game) and trying to adopt a very complex method (cloning and any component blocks). It’s not something I can describe in a few sentences or necessarily point you to a specific tutorial although there are certainly topics here about cloning and any component blocks if you search for them.

As far as a general algorithm for a chess game goes, that’s going to be easy to find online and most likely will offer a very efficient way to refer to each square, piece, move and interaction between pieces. The question is whether you want to create it from scratch or borrow an existing method for doing some of this.

Yeah I understand it probably isn’t something easy.
The main reason I wanted to create from scratch was
a) to learn something new which I hope to be able to take to other apps, and
b) I want control over each stage of the game for plans I may have down the line. This might be possible by using other algorithms as you suggest though, I just don’t know as yet.

But yeah I’ll do a bit more reading on ‘any components’ and look for them algorithms. Thank you

Would you say using a canvas would be a good idea? To detect collisions between black and white pieces?

NO. The canvas is really not production-ready. I avoid it. Plus, you don’t need to detect any collisions at all. You just wait until each piece is in its new location and then update your data source and check for any interactions between pieces. Ideally, nothing is actually moving at all in your project, ever. If you want the illusion of movement, there might be a way to animate that.

You’re also going to find that – if you do use the canvas – drag & drop support is not available. The canvas is really weakened without it because you can’t use it for games like chess and you can’t detect swipes for other purposes.

Ok no problem. I won’t try the canvas.
So I’ve been trying with any component blocks as you suggested all day and I can sort of see a murky path through but it isn’t becoming clear just yet. I might have to put up with the slow thunkable for now because I can’t get my head round it. I have included my blocks below if you can see anything or point anything out. The first picture is hard to read anything but I have included it so you see the amount of coding I have for just 4 of the simple pieces.


Pic 2 is what I have coded to happen when selecting a piece.

Pic 3 is how the user can get that piece to move to the next available square. I also set up a rule that tells the code when it’s usual movement rules can not apply. When I tried using the any component blocks I was asking a new button’s visibility to be true and to move to the "component"s X. The new button became visible but would not move to selected component’s X. This new button’s function is meant to be something the user can click to select the square they want to move to.


Pic 4 is how the user can attack a valid enemy piece. This code works. But like I say it is running quite slow while building it. You have since told me that it is because of the amount of if else conditions I am putting in. However, having very limited experience I can’t see another way. Well, I kind of can see another way which is presumably the way you are trying to direct me but I can’t seem to make that happen.

The screenshots are so tiny and they are low resolution so the forums can’t display a larger version of them like it does for many screenshots.

What I recommend is this:

  1. Take a screenshot of one section of re-used blocks. What I mean by that is if you have three or four if/else blocks stacked on top of each other, just show us that one section.

  2. Tell us what you expect those blocks to do.

I guarantee someone on the forums will be able to help you simplify your blocks.

Ok. I think I may have figured out how to do what you were suggesting. What do you think of these blocks?

I’d also need this:

It’s also helpful to know what algorithm you’re following because then I can read that step in the algorithm and see if your blocks will actually achieve that.

It looks like you’re going the Canvas route… more power to you! It’s not something I’m going to be able to offer a lot of help with because my idea of how to program chess is by storing piece locations in rows and columns of a database which exactly correspond to the squares on a chessboard. And that allows you to do math to use just a few blocks for every possible move.

Um I may misunderstand but I don’t think it’s a canvas. I’ve cloned a button called black pawn and one called white pawn.
I’ve put an ‘any button’ component in. When user clicks any button it gets the component ID (is how i understand it), it compares this components text with “BPawn” or “WPawn”. If text is BPawn it will reveal another button called “Move1” at that components Y minus 40 pixels(?).
User can then click this new button to move that component. It is working. Now I need to figure out how to the attack work

1 Like

Oh sorry! I saw x and y and just assumed you were using the canvas.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.