How to make sine numbers in global variables?

0 favourites
  • 8 posts
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • Hey all, i'm wondering how to make sine numbers using global variables.

    For Example

    the start number is 0 and the ending number is 100, it should act like a cycle, 1,2,3,4,5,6,7,8,9,10,11,12... 100 limit is reached, then 100,99,98,97,96,95,94,93...0. Repeat 1 to 100.


  • Well, if that's all you need to accomplish:

    Global variable Num

    Global variable Cycle

    If Cycle = 0

    -- If Num < 100

    ---- Num = Num+1

    -- Else

    ---- Cycle = 1

    Then the opposite for Cycle = 1. It's not very fancy but it gets the job done!

  • Thanks, i will try your idea!

  • Global number n = 0        // counter
    Global number min = 0      // min value of the cycle
    Global number max = 100    // max value of the cycle
    Global number phase = 1    // phase of the cycle
    +Start of Layout
      -> set n to min
    +Every Tick
      -> set phase to (n=min) ? 1 : phase
      -> set phase to (n=max) ? -1 : phase
      -> set n to clamp(n+phase,min,max)
  • Holy wow. o_o

    THAT... is the fancy version.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Alternatively, if you want the silky smoothness of sine, you could just set your variable to:

    (sin(x + 270) + 1) * 49.5 + 1

    This will oscillate between 1 and 100. x can be any value you want, such as a variable you iterate by yourself, or you can take the game time and have a nifty one line solution.

    EDIT: Here's a Google Graph for the Hell of it

  • Yann thanks! it's even fancier code. Make sure don't use "min" and "max" because these names are reserved for c2 expression system and linkman2004 I don't get why sin + "x" isn't working, it appears to be invalid, did you mean we should to put an existent variable?

  • Yeah, x is simply a stand in for a variable of your choosing. For example, you could use:

    (sin((time * 1000) + 270) + 1) * 49.5 + 1

    Where "time" is the actual system expression for elapsed time in seconds - multiplying this value by 1000 gets you milliseconds. Multiplying "time" by a higher number will increase the speed of oscillation while multiplying by a lower number will decrease the speed.

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