programming (general)

  • Steven from this forum made a cap for me.

    In it he made a "switch" with a global variable.

    But very efficient.

    If pressed 'm'

    set variable to 1 - variable

    I always did:

    if variable = 0 and 'm' is pressed (...)

    if variable = 1 and 'm' is pressed (...)

    I like how efficient this is and I'm sad I did not come up with this myself.

    What is my point? I hope someone can comment on this, in any way.

    For example: is this new for you?

  • the light bulb just came on when i tested the code in my head.

    that...is cool.

    well done steven.

  • There's always: on m pressed add 1 to global

    global modulus =0 do stuff

    global modulus =1 do stuff

  • There are tonnes of ways to switch a variable from 0 to 1! Here's another, using conditions in expressions, which is one of my favourite "hidden" features:

    m = 1 ? 0 : 1

    Conditional expressions work like this: condition ? result_if_true : result_if_false

    So as you can see when m is 1 it's set to 0, and vice versa.

  • Is that feature implemented in C2?

  • Yup, try it!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Just did! and it's awesome :D

  • <img src="http://images.encyclopediadramatica.ch/8/83/Epic_reaction.jpg" border="0" />

  • Shame it doesn't work with text variables :)

  • I always used 1 and minus 1, so when I switched the value I just multiplied it by -1. Still, this method is awesome =D

    Hey Ashley, is that in CC as well?

  • Shame it doesn't work with text variables :)

    don't know about construct, but most languages let you do that:

    stringvar == "The Right Answer" ? "good job!" : "aww, you're lame"

  • In C2 it says

     Type mismatch
    = does not work with 'integer' and 'string' (use & to build strings)
  • rogerty, in answer to your original question. Being able to recognize and appreciate an elegant solution may be a sign that you have the capacity to create them as well. I think the more you see, and the more you try to think in that direction the more often you will come up with elegant solutions to your problems. Over the years I've come up with code I thought was clever or elegant now and then, but I still see something simple like this from time to time, and wonder why I hadn't thought of it myself. The particular example you asked about was one such case when I first saw it.

  • Here is another one:

    Automatically loop-count x from 1 to n repeatedly

    x = (x % n) + 1

    EDIT:

    In C2 it says

     Type mismatch
    = does not work with 'integer' and 'string' (use & to build strings)

    In CC it works when the testvar is a number, e.g.

    Text: Set text to global('test') = 0 ? "Value is zero" : "Value is non-zero"
  • In C2 it says

     Type mismatch
    = does not work with 'integer' and 'string' (use & to build strings)

    Oops, looks like a bug. Will try have that fixed for next build.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)