GMS 2 [HELP] - Energy System (mobile games)

Discussion in 'Programming' started by GDK, Oct 10, 2019.

  1. GDK

    GDK Member

    Joined:
    Jul 10, 2017
    Posts:
    58
    I'm looking at potentially adding some type of "Energy System" to my game OR features/mechanics that are linked to "real-time" cool downs much like how buildings/upgrades work in Clash of Clans.

    So I'm guessing there will need to be some sort of server-side / networking element to it. If so...
    1. Is it relatively complex to implement?
    2. How should I go about setting it all up?

    **I have no idea what it involves, so please ask me questions if my request is too vague or point me in the right direction**
     
  2. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,742
    There's a lot of functions that get the current time from the OS:
    https://docs.yoyogames.com/source/dadiospice/002_reference/date and time/index.html

    For instance, current_second is a read-only variable that contains the current second since the Unix Epoch(?). It doesn't matter what it measures from, it's a number that goes up once per second.

    So... to get a real-time cooldown on an action: store the timestamp it was initiated. Regularly, check the current time. If any event is as long in the past as its real-time cooldown, the cooldown ends. So for energy, you'd store the "last time you got an energy" timestamp, and whenever the current time exceeds this time with [as much real time you want between refills], add an energy unit and update the "last energy time" timestamp.

    This should also be persistent between reboots, so you can store these to a file when the game ends, and upon load, add as much energy as the player is entitled to depending on time difference (and adjust the stored times accordingly).


    Now, this all sounds super simple, right? What you need to be aware of is that the player can trick this system by changing their OS time. So it's not as foolproof as a system using a server. But if you can live with that (and assuming most players won't realize this), it's pretty straightforward to implement.
     
    GDK likes this.
  3. Carcophan

    Carcophan Member

    Joined:
    Aug 4, 2019
    Posts:
    53
    Say we did have access to a server, would it be the same principal, just using the servers OS time, versus the devices? Since the user will not be able to change the servers TS?
     
    Yal, GDK and SnotWaffle Studios like this.
  4. GDK

    GDK Member

    Joined:
    Jul 10, 2017
    Posts:
    58
    @Yal Thanks, Ill take a look at those docs and see how I go. Definitely gives me something to go ahead with but Ill be coming back here if I strike any issues :p
     
  5. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,742
    Good point! Come to think of it, there probably is a bunch of services that give you the current UTC timestamp if you query them - a lot of modern OSes synchronize their time with a server somewhere and there's probably at least one free alternative.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice