Global Variable Solution

Discussion in 'Buildbox 3.0' started by LBPToo, Oct 27, 2018.

  1. LBPToo

    LBPToo Avid Boxer

    Joined:
    Nov 1, 2016
    Messages:
    233
    Likes Received:
    141
    I saw a question on Discord about Global Variables in BB3 and since I've devised a method to "simulate" globals I thought I'd share. I've hesitated to do this because it's very difficult to explain, but here goes.

    I first create an object in the first scene that has no other purpose than to store global variables. I name it “Global Entity”. You could use any another object, like the actor to store the variables too, I just wanted to keep them separated.

    Anyway, in the global variable object I create a node just for storing global variables. Say call it "Get-Set Globals". I put the variables at the top as a 'var' and initialize them as necessary at the declaration or in the init function of the object. I then use the signal function to get and set the values

    Say the global object has the "var saveArmPosition=0" declared.

    Now let’s say you have a rotating arm you want to keep the current position of that arm to be accessed by other objects. It needs the variable to be global so many objects can access and set it. In the rotating arm object you have a variable 'var armPosition', its value gets set in the init function of the object, say to its starting rotation of 20. Now in the init function you also do the following code:

    Code:
    /*Get the entity of the global storage object*/
    
    let globalEntity = this.scene().find('Global Entity')[0];
    
    /* Tell the global entity to store the current state of arm Position*/
    let comp = globalEntity.component('Get-Set Globals');
    comp.signal('SetArmRotation',armPosition);
    
    /* the node “Get-Set Globals” will now receive the message to set its global variable “saveArmPosition” */
    
    /*In the node “Get-Set Globals” you have the following code*/
    
    function signal(name, value)
    {
      If (name == ‘SetArmRotation’) saveArmPosition = value;
    }
    
    Now the global is set to the starting rotation of the arm.

    That’s how you save the global variable. Now let’s say in another object named “Matching Rotation Arm” which has a node name “Match Rotating Arm” you want to get that variable to use to set the arm to match the other one. The variable in this node is “rotationToMatch”.

    Now in the init function(or update if it makes sense) of ‘Match Rotating Arm’ you do the following call:
    Code:
    let globalEntity = this.scene().find('Global Entity')[0];
    let comp = globalEntity.component('Get-Set Globals');
    
    /* pass in the entity to the signal function so it has access to its nodes*/
    comp.signal('GetArmPosition', this.entity());
    
    In the node “Get-Set Globals” is the following code:

    First I have a function:

    Code:
    function returnArmPosition(theEntity)
    {
    let comp = theEntity.component(‘Match Rotating Arm');
    comp.signal(‘currentArmPosition’, saveArmPosition);
    }
    
    function signal(name, value)
    {
      if ( name == GetArmPosition) returnArmPosition(value);
    }
    
    After that, back in the node “Match Rotating Arm” of the object “Matching Rotation Arm” in the signal function:

    Code:
    /* now the “Match Rotating Arm” signal function gets the “global” saved arm position to use to initialize or update its arm position to match the first one */
    
    function signal(name, value)
    {
      if ( name == ‘currentArmPosition’) rotationToMatch = value;
    }
    
    Whew, I hope I didn't make any mistakes, and if you're not a coder already and understand the way nodes work, you're not going to get this. If anyone has a better, simpler way to do globals, let me know.

    Good luck.

    Bill
     
  2. terravexo

    terravexo Boxer

    Joined:
    Sep 21, 2018
    Messages:
    8
    Likes Received:
    0
    Very clever stuff Bill, thanks. It works beautifully.

    Indeed the plan now is to store the current actor's location in 3 global variable, although maybe only update them every couple of seconds or so.

    This will then allow lots of enemies to have basic "path finding AI" which wouldn't always be accurate, but that's the plan in the first place. This could be lots of fun! :)

    Paul
     
  3. LBPToo

    LBPToo Avid Boxer

    Joined:
    Nov 1, 2016
    Messages:
    233
    Likes Received:
    141
    Last edited: Nov 15, 2018
    nyamuk91 and terravexo like this.
  4. terravexo

    terravexo Boxer

    Joined:
    Sep 21, 2018
    Messages:
    8
    Likes Received:
    0
    Thanks Bill @LBPToo ,

    That looks very impressive. I do think global variables lend themselves to something just like this. I've been having a bit of a play and am getting there with something similar already, but I can see why you're not overly keen on sharing it, there's a considerable learning curve involved which you'd like to be rewarded for with unique gameplay, which I'm sure you will. :)

    @Steddyman has a very easy to manage solution there, I think for some reason yours popped up first on a Google search, however it's nice to know where his solution may have found some inspiration, I'll most definitely be using it!
     

Share This Page