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: 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
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
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.
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.
I love what you are doing guys as a non coder I didn’t understand what the use of this script can anyone explain.
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.
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?
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
Wait, is it necessary to use this method anymore now that we have Settings object? Does this method behave differently than Settings object?
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?
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.
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