Modules/Common code scripts?



  • Is there a way to include common code scripts?

    such as require or some other way?



  • 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.



  • 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.



  • 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.



  • 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.


  • brainCloud

    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.



  • @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.



  • 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.


Log in to reply