Globalvars Manager

Discussion in 'BBAssets' started by Steddyman, Nov 4, 2018.

  1. Steddyman

    Steddyman Boxer

    Joined:
    Sep 3, 2018
    Messages:
    51
    Likes Received:
    29
    Hi Everyone

    The attached package implements Global Variables to allow you to easily share data across assets in the world without having to rely on signal callbacks. This allows the package to be fast and flexible without the complexity of needing entry points to return values.

    To use it, import the GlobalVars.bbasset and drag it into the Start scene of your World above the Camera object and below the World object, as per the following screenshot:

    [​IMG]

    Once it is in your scene, you can get a reference to it from any script using the following code:
    Code:
    let entity = this.scene().find('GlobalVars')[0];
    let gm = entity.component('GlobalsManager');
    
    Then to store a variable of any type use:
    Code:
    gm.setValue("counter", 0);
    
    The following example function increments a global counter value every time the mouse is pressed:
    Code:
    function touchBegan( point ){
       let _counter = gm.getValue("counter");
       log("Value of counter = " + _counter);
       _counter = _counter + 1; 
       gm.setValue('counter', _counter);
    }
    component.touchBegan = touchBegan;
    
    The attached ExampleConsumer.bbasset contains a GlobalConsumer script with the above example code to show you how to use this asset in your own scripts.

    Also, although I haven\t experimented with this, there should be nothing preventing you from storing functions in the GlobalVars asset. This could be used to pass functions to other scripts for callback.

    Enjoy
     

    Attached Files:

    nyamuk91, DanFarfan and LBPToo like this.
  2. DanFarfan

    DanFarfan Avid Boxer

    Joined:
    Sep 22, 2018
    Messages:
    101
    Likes Received:
    42

    Well done, Steddyman. There are many ways to use this. Globals is one of the first things I did. :)

    A couple of notes.
    1) I've been putting my global assets in the start scene (after the "Level Section Start"). It didn't dawn on me to try above there.
    One behavior I found is that (where I placed it) the script modules eventually STOP getting the update() call from the BB software., even though the code / object / node / component stays active (usable for direct function calls) This was confusing and annoying. Perhaps your placement solves that! Not sure.
    If in fact the call to update() in your global asset works without interruption, it opens the door to any number of time-based behaviors.

    2) You can store functions as the global variable values. I have done that. Works fine. The only caveat, of course, is developers must correctly address the function (i.e. use fully qualified address).

    (note: I've only seen the code in your post)
    3) suggestion: If speed is what you're offering, consider adding an additional function,

    gm.addValue('counter', 1);

    Then I don't need to get AND set (but I still can if I need to)
    And of course this allows going up and down :) :

    let new_health = gm.addValue('health', delta_strength);

    (Note: for the new coders in the community, when a Javascript function, returns a value, but the command to execute the function ignores it -- as in the case of the gm.addValue('counter', 1);
    Javascript doesn't mind at all. Use the return value, ignore it, no matter.


    Keep up the good work!

    @DanFarfan
     
    LBPToo likes this.
  3. LBPToo

    LBPToo Avid Boxer

    Joined:
    Nov 1, 2016
    Messages:
    233
    Likes Received:
    141
    I love what @Steddyman did with this. I wanted the additional addValue functionality for myself that @DanFarfan suggested so I added it in. If anyone else needs this capability, here is the modified version.

    The function name for adding is now "addToValue". If you downloaded the earlier modified version I put up here the name was addValue.
     

    Attached Files:

    Last edited: Nov 8, 2018
  4. Steddyman

    Steddyman Boxer

    Joined:
    Sep 3, 2018
    Messages:
    51
    Likes Received:
    29
    Ahh.. sorry I didn't get it at first but do now. You mean incrementValue?

    You will have to be careful to ensure the variable is a number, because if it is a string it will you append it to the end.
     
  5. LBPToo

    LBPToo Avid Boxer

    Joined:
    Nov 1, 2016
    Messages:
    233
    Likes Received:
    141
    Yep, as you know, I agree and changed it to "addToValue".
     
  6. wesam_badr

    wesam_badr Miniboss Boxer

    Joined:
    Oct 10, 2015
    Messages:
    1,065
    Likes Received:
    479
    I love what you are doing guys as a non coder I didn’t understand what the use of this script can anyone explain.
     
  7. LBPToo

    LBPToo Avid Boxer

    Joined:
    Nov 1, 2016
    Messages:
    233
    Likes Received:
    141
    Good question. It is not obvious unless you've run into the need as a coder. What this basically does is allow the different objects and nodes within those objects to share data. Say a node in one object stores a value that another object needs to know about, This node stores the values set by one object and the other object can retrieve it, or visa-versa. Also, two nodes in one object can also share the information. What this does is provides a "global"(all objects can access it) storage container to share information between objects and nodes. Yikes, I hope that's clear enough.
     
    wesam_badr likes this.
  8. nyamuk91

    nyamuk91 Boxer

    Joined:
    Aug 15, 2018
    Messages:
    95
    Likes Received:
    20
    This is awesome!! Now, any idea how to make a "World only" global variable? So basically, I want to re-initialize the global variable to some other value in each new world. Right now, the only way I could think of is by having a separate object that does the initialization in each world. This is obviously not good if there's a huge number of worlds.

    Edit: Ok turns out the Global Var is indeed "World only". So, this brings to my next question, how can we send the value of these global vars from a world to another world?
     
    Last edited: Dec 31, 2018
  9. Steddyman

    Steddyman Boxer

    Joined:
    Sep 3, 2018
    Messages:
    51
    Likes Received:
    29
    Hi. That's easy enough, just place them in the Settings object that was new to beta 3.
     
    nyamuk91 likes this.
  10. nyamuk91

    nyamuk91 Boxer

    Joined:
    Aug 15, 2018
    Messages:
    95
    Likes Received:
    20
    One more question, do you know any way to make a variable tied to a world, instead of Object? Since script are tied to object, this seems impossible :confused:
     
  11. nyamuk91

    nyamuk91 Boxer

    Joined:
    Aug 15, 2018
    Messages:
    95
    Likes Received:
    20
    Wait, is it necessary to use this method anymore now that we have Settings object? Does this method behave differently than Settings object?
     
  12. isac cabrera

    isac cabrera Boxer

    Joined:
    Nov 8, 2018
    Messages:
    50
    Likes Received:
    8
    hi guys!! how could I store this increment value to the game, for example, I would like to increment an actor's speed by a UI button, it works inside of the game, but when the game is closed and opened again this data is lost, so, is there any way this increment value can be stored?
     
  13. PunkPuffin

    PunkPuffin Avid Boxer

    Joined:
    Sep 27, 2018
    Messages:
    285
    Likes Received:
    195
    You need to use settings to persist data between sessions
     
    isac cabrera likes this.
  14. isac cabrera

    isac cabrera Boxer

    Joined:
    Nov 8, 2018
    Messages:
    50
    Likes Received:
    8
    Thanks man!! could you tell me how I must use it? or a reference where I can learn?
     
  15. PunkPuffin

    PunkPuffin Avid Boxer

    Joined:
    Sep 27, 2018
    Messages:
    285
    Likes Received:
    195
    From memory all you need to do to set a value is
    Settings.abc = 1;

    And to get a value

    let x = Settings.abc;

    The rest should be taken care of internally.
     
    Antropy and isac cabrera like this.
  16. isac cabrera

    isac cabrera Boxer

    Joined:
    Nov 8, 2018
    Messages:
    50
    Likes Received:
    8
    Man!! thanks a lot it will help so much!!
     
  17. jmp909

    jmp909 Boxer

    Joined:
    Feb 2, 2020
    Messages:
    28
    Likes Received:
    4
    hi @Steddyman,

    any benefit of using this bit
    Code:
    _proto = Object.getPrototypeOf(this);
    _proto.setValue = function(name, value) {...
    
    over this pattern..

    Code:
    // GlobalsManager script
    function setValue(name, value) { ... }
    component.setValue = setValue;
    
    // or just component.setValue = function(name, value) { .....
    
    // usage (as usual)
    let entity = this.scene().find('GlobalVars')[0];
    gm = entity.component('GlobalsManager');
    gm.setValue("counter", 0);

    thanks
    J
     

Share This Page