Can loops make my code more efficient?

0 favourites
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • The above code is working just fine. However, I'm wondering how I can make it more efficient. I'm still learning about loops, and I'm curious if I can improve its efficiency using a loop! I dont know how to start, Any suggestions?

  • That's working fine? Because each time the pick all would cancel the initial condition. Maybe you didn't notice because all the actions are the same.

  • You could reduce the number of events here; fewer lines of code would be more efficient to work with (maybe not the efficiency you're after though!)

    Touch: On tap gesture on TagText

    ---> TagText: TAGOrder_Text is between 1 and 3

    ------> System: Pick all TagText ... TagText Set font colour to rgbEx(0,0,0)

    ------> ... TagText Set font colour to rgbEx(0,0,0)

  • That's working fine? Because each time the pick all would cancel the initial condition. Maybe you didn't notice because all the actions are the same.

    Yes it is wokring fine, I am toggling text colors, displaying only the most recently clicked text as active. The active text should have a color other than rgbEx(0,0,0), while the rest should be rgbEx(0,0,0).

  • You could reduce the number of events here; fewer lines of code would be more efficient to work with (maybe not the efficiency you're after though!)

    Touch: On tap gesture on TagText

    ---> TagText: TAGOrder_Text is between 1 and 3

    ------> System: Pick all TagText ... TagText Set font colour to rgbEx(0,0,0)

    ------> ... TagText Set font colour to rgbEx(0,0,0)

    Initially, I thought it would be quite challenging, assuming we would need to use a loop. However, the solution you provided is much easier and efficient. Thank you. It works.

    Just one doubt, generally what should I put for the value? I understand that in my specific case, since I have 3 TagText items, I should put Lower: 1 and Upper: 3. However, when it comes to the the value, I put 1 and it works fine.

  • The value is what you're checking is between 1 and 3.

    In this case, it's the variable of the text object, which is TagText.TAGorder_Text

  • Thank you. I have a question: Is there an alternative expression similar to "between" that allows me to specify certain values like 1, 2, 4, 20, 31...

    I tried using the bellow based on a suggestion in a post,

    but it didn't work. Then I referred to the manual and discovered this, I followed the same steps,

    but it still doesn't work. What am I missing?

  • I'm not understanding what exactly you're aiming for here, but maybe just use a subevent and make it an "or" block?

  • Please check the below image, hope it now makes sense. :)

    Basically, what I'm asking is how to use the "&" and "|" OR operators in the expression while comparing.

  • When using “compare instance variable” you can only compare it against one value. Basically whatever the expression you write evaluates to will be the value you’re comparing the variable against.

    Now, if you want to do a general picking condition look at system->pick by evaluate. With that you can do do what you’re after.

    System->pick by evaluate

    Object: sprite

    Expression: sprite.var=1 | sprite.var=2

    Hopefully that will give you the general idea. I can’t do screenshots atm.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you so much, this is exactly what I was looking for, and now I understand what I was doing wrong. 😊

    I just have one follow-up question. it works perfectly with the OR "|" operator, but not with the AND "&" operator.

    (If the scenario is, out of all the available objects, if few of them have the instance variable, like

    Text.var =37 & Text.var =1 & Text.var =8
    

    then it should be executed, but not sure why it is not working.

  • You could add parenthesis. (Sprite.var=2)&(sprite.var=3)

    I think at least. It’s an order of operations thing or something. But for and I’d just add multiple instance compare conditions. That acts as an and

  • It doesn't work because it's impossible that the same value is 37, 1 and 8 at the same time.

  • You could add parenthesis. (Sprite.var=2)&(sprite.var=3)

    I think at least. It’s an order of operations thing or something. But for and I’d just add multiple instance compare conditions. That acts as an and

    (Text.var =37) & (Text.var =1) & (Text.var =8)
    

    it still doesn't work, maybe C3 somehow intentionally not allowing this to happen?

  • It doesn't work because it's impossible that the same value is 37, 1 and 8 at the same time.

    Here we pick by comparison so theoretically, it will pick all the objects where at the value the instance variable matches.

    This way it works

    (Text.var =37) | (Text.var =1) | (Text.var =8)
    

    This way it doesn't

    (Text.var =37) & (Text.var =1) & (Text.var =8)
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)