Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Solved
  • Unsolved
  • Users
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Darkly)
  • No Skin
Collapse
brainCloud Forums
  1. Home
  2. Cloud Code
  3. Modules/Common code scripts?

Modules/Common code scripts?

Scheduled Pinned Locked Moved Solved Cloud Code
script
7 Posts 5 Posters 3.0k Views 6 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jason Jarvis
    wrote on last edited by
    #1

    Is there a way to include common code scripts?

    such as require or some other way?

    1 Reply Last reply
    0
    • JonathanJ Offline
      JonathanJ Offline
      Jonathan
      wrote on last edited by Paul Winterhalder
      #2

      Unfortunately not really.

      You can call cloud code scripts from cloud code scripts (via the bridge.callScript() method).

      Otherwise, people have also just copied and pasted shared code at the top of their scripts.

      1 Reply Last reply
      0
      • JonathanJ Offline
        JonathanJ Offline
        Jonathan
        wrote on last edited by Jonathan
        #3

        You could also create a Global Property that houses your shared code.

        Properties can be set on the Design | Custom Config | Global Properties page.

        b9b4069f-603c-4d2b-aea5-ed58440ddc13-image.png

        function getFuzz() {
            return 'Fuzz';
        }
        
        function addTwoNums( a, b ) {
            return a + b;
        }
        

        And use the bridge to get the shared code via bridge.getGlobalProperty("sharedScripts").

        Example:

        var response = {};
        
        // First Load the shared scripts into a string object
        var sharedScripts = String(bridge.getGlobalProperty("sharedScripts"));
        
        // Output to the log - helps for debugging. Debug log is only written to when run from API explorer or CC editor
        bridge.logDebug("Loading shared code...", sharedScripts);
        
        // And now evaluate the functions so that they become part of the script.
        // Any errors in the code being evaluated will be reported as errors as if they
        // occurred in the script itself. This affects the line numbers the error is reported upon.
        eval(sharedScripts);
        
        // Now you can use any of the functions included...
        response.getFuzz = getFuzz();
        response.sum = addTwoNums( 5, 10 );
        
        response;
        

        This has been tested with up to 30k bytes of code.
        That said, isn't an officially sanctioned approach. We don't recommend putting more than 20-25kb in the shared code string.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jason Jarvis
          wrote on last edited by Jason Jarvis
          #4

          hmm. thanks for the reply. After working on Beast Brawlers using GameSparks, we found that there were coding patterns that we were able to reduce redundant coding.

          We used "modules" to handle data management of specific db collections. Such as Currency, we used a currency module to have common functions. Such as AddCurrency(type, amount, detailsOnWhy).

          This way we had a common code flow when the currency was added. So when our different points of currency added

          • Lootbox
          • End of play
          • store purchase of in-game goods
          • cash purchase
          • live opts tools
          • season rewards from clan achievements
          • daily quests
          • progression rewards
          • reward videos
          • new accounts
          • purchasing skins
          • purchasing in-game goods
          • Special event rewards
          • External tournament rewards(we used Amazon GameOn)

          so in our workflow as a sample

          EventHook for XYZ
          requireOnce("CurrencyModule")
          Currency.Add( player, Currency.Type.Gold, 100, "GamePlay-SecondPlace");

          I'm concerned about the idea that either I would have to copy and paste common code to the head of any hook isn't viable. If I need to change the code it's not viable to go through every point.

          The global properties sound interesting, but if it's not built for lots of modules and this kind of use. Since I need to run eval every time I'm concerned about the jit time. I'm concerned I would break the server pretty quick. Is there any kind of memory cache in BrainCloud. GS had a memory cache where we could store js objects such as pseudo-classes. Sorry, I'm not trying to do too many comparisons. I'm just trying to figure out how to bring over some of my tools I did for myself without smashing the entire system 😄

          Although currency is really simple. However, in my case, I would rather run a region territory control module and break that up into two modules. one to manage territory data so that the board can be broken into different games and the other being the actual game logic.

          The 20 to 25k is fine. GameSparks I think we may have had a 10k limit when we started.

          I really appreciate your time in helping me understand BrainCloud.

          C 1 Reply Last reply
          2
          • Paul WinterhalderP Offline
            Paul WinterhalderP Offline
            Paul Winterhalder
            brainCloudAdmin
            wrote on last edited by
            #5

            Hmm, brainCloud has recently added a feature that allows you to cache JSON objects - which may get you along the path of what you want. See Set and GetSessionCacheObject() - https://getbraincloud.com/apidocs/apiref/#cc-bridge-setsessioncacheobject

            (of course, those aren't actually js classes).

            brainCloud does also cache the scripts themselves (as well as global properties). I'm not sure about what caching may be in the Rhino JS compiler on top of that.

            Performance-wise, the jit time interpreting the scripts has never been an issue - the times when cloud code scripts have had performance challenges have generally been in excessive/inefficient API usage (which of course involves a lot of database traffic) and unnecessary allocations (especially in loops).

            Anyway, keep the questions coming. We're happy to assist where we can!

            Paul.

            1 Reply Last reply
            0
            • J Jason Jarvis

              hmm. thanks for the reply. After working on Beast Brawlers using GameSparks, we found that there were coding patterns that we were able to reduce redundant coding.

              We used "modules" to handle data management of specific db collections. Such as Currency, we used a currency module to have common functions. Such as AddCurrency(type, amount, detailsOnWhy).

              This way we had a common code flow when the currency was added. So when our different points of currency added

              • Lootbox
              • End of play
              • store purchase of in-game goods
              • cash purchase
              • live opts tools
              • season rewards from clan achievements
              • daily quests
              • progression rewards
              • reward videos
              • new accounts
              • purchasing skins
              • purchasing in-game goods
              • Special event rewards
              • External tournament rewards(we used Amazon GameOn)

              so in our workflow as a sample

              EventHook for XYZ
              requireOnce("CurrencyModule")
              Currency.Add( player, Currency.Type.Gold, 100, "GamePlay-SecondPlace");

              I'm concerned about the idea that either I would have to copy and paste common code to the head of any hook isn't viable. If I need to change the code it's not viable to go through every point.

              The global properties sound interesting, but if it's not built for lots of modules and this kind of use. Since I need to run eval every time I'm concerned about the jit time. I'm concerned I would break the server pretty quick. Is there any kind of memory cache in BrainCloud. GS had a memory cache where we could store js objects such as pseudo-classes. Sorry, I'm not trying to do too many comparisons. I'm just trying to figure out how to bring over some of my tools I did for myself without smashing the entire system 😄

              Although currency is really simple. However, in my case, I would rather run a region territory control module and break that up into two modules. one to manage territory data so that the board can be broken into different games and the other being the actual game logic.

              The 20 to 25k is fine. GameSparks I think we may have had a 10k limit when we started.

              I really appreciate your time in helping me understand BrainCloud.

              C Offline
              C Offline
              Chris Brooks
              wrote on last edited by
              #6

              @Jason-Jarvis We too are migrating from GameSparks and the lack of Modules is surprising. This will force a pretty big change to the way we were doing things as well.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Ali Raza
                wrote on last edited by
                #7

                Similar situation here, i had built a micro framework on GameSparks to cover cases that almost every cloud driven game require. This saved huge amount of time working on new games and at the same time sharing code among existing features.
                Please consider this requirement a must have for any serious game development.

                1 Reply Last reply
                0

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Solved
                • Unsolved
                • Users