1. The space forums use a separate registration system from the main virtual world. To comment on these forums, you need to create a separate forum account.
    Dismiss Notice
  2. Hey there, if you want some direct help without hopping inworld - try our Discord channel at https://discord.gg/mptfycQ
    Dismiss Notice

Quick Networking Example for Scripts

Discussion in 'HOWTOs' started by Adam, Apr 19, 2017.

Share This Page

  1. Adam

    By:AdamApr 19, 2017
    Developer
    Staff Member Moderator

    Joined:
    Aug 21, 2014
    Messages:
    711
    Likes Received:
    143
    This is just a quick example which networks a browser surface - it's designed to be used with a Text Field. The unitypackage for this assembled item is floating around, but the code can be read as a quick guide to using script raw networking functions.

    Code:
    -- Network sharding functions
    callURL = function()
        local urlif = Space.Host.GetReference("URLBar");
        local browser = Space.Host.GetReference("Browser");
        local url = urlif.UIInputField.Text;
        Space.Network.SetShardProperty("ScreenURL", url);
        Space.Network.SendNetworkMessage("URLBox", {uri = url});
        browser.Browser.SetURL(url);
    end
    
    onURLUpdate = function(url)
        local urlif = Space.Host.GetReference("URLBar");
        local browser = Space.Host.GetReference("Browser");
        local url = Space.Network.GetShardProperty("ScreenURL");
    
        browser.Browser.SetURL(url);
        urlif.UIInputField.Text = url;
    end
    
    -- Subscribe to network messages we're interested in
    Space.Network.SubscribeToNetwork("URLBox", onURLUpdate);
    
    -- Try load any URLs if it exists already
    local urlif2 = Space.Host.GetReference("URLBar");
    local browser2 = Space.Host.GetReference("Browser");
    local url2 = Space.Network.GetShardProperty("ScreenURL");
    
    if url2 != nil then
        browser2.Browser.SetURL(url2);
        urlif2.UIInputField.Text = url2;
    end
    
     
    Mike (CNDG) likes this.
  2. Adam

    By:AdamApr 19, 2017
    Developer
    Staff Member Moderator

    Joined:
    Aug 21, 2014
    Messages:
    711
    Likes Received:
    143
    (May not be the worlds cleanest code example - the url parameter in onURLUpdate appears to be being ignored, it should have the new value actually -- SetShardProperty's event may arrive after the SendNetworkMessage - the order of these is not guaranteed to be consistent.)
     
  3. Mike (CNDG)

    By:Mike (CNDG)Apr 19, 2017
    New Member

    Joined:
    Apr 2, 2017
    Messages:
    15
    Likes Received:
    16
    Hey Adam,

    Still wrapping my head around the Network messaging, and set up a basic example of two cubes. One of which you click to send a message, and one that receives a message and logs that a message has been received.

    Cube 1
    Code:
    local this = Space.Host.ExecutingObject;
    
    function evtClick()
     Space.Network.SendNetworkMessage("MyCustomEvent", {msg = "Hello"});
    end
    
    this.SubscribeToEvents();
    this.OnMouseDown(evtClick);
    
    Cube 2
    Code:
    local evtMessage = function(msgInfo)
     Space.Log("Event MyCustomEvent Handled");
    end
    
    Space.Network.SubscribeToNetwork("MyCustomEvent", evtMessage);
    
    With the clicking of the first cube, the second cube should receive the event and log "Event MyCustomEvent Handled" surely? However, Cube 1 does log that it's sending a message ("Sending userscript: cube / msg => Hello"), but Cube 2 doesn't seem to be receiving anything.

    Am I missing something?

    Regards,
     
    Last edited: Apr 19, 2017
  4. AshaSekayi

    By:AshaSekayiApr 20, 2017
    Member

    Joined:
    Dec 20, 2016
    Messages:
    49
    Likes Received:
    5
    From my testing, you can't get objects to directly talk to each other ala the SL LSL method. So, you are using the wrong methods for what you want to do.

    So, this is what I've done to get a similar communication functionality. Save your key/pairs in a shard property.

    Code:
    -- CUBE 1
    local scriptedObject = Space.Host.ExecutingObject;
    scriptedObject.SubscribeToEvents();
    
    local testMsg = "Hello";
    
    local updateTextUI = function()
        Space.Network.SetShardProperty("testProp", testMsg);
    end
    
    scriptedObject.OnMouseDown(updateTextUI);
    Code:
    -- CUBE 2
    local scriptedObject = Space.Host.ExecutingObject;
    scriptedObject.SubscribeToEvents();
    
    local textValue = Space.Host.GetReference("Text");
    local retrieved;
    
    local updateTextUI = function()
        retrieved = Space.Network.GetShardProperty("testProp");
    end
    
    local onUpdateMethod = function()
        updateTextUI();   
        if retrieved ~= nil then
            textValue.UIText.Text = "Retrieved msg: " .. retrieved ;
        end                 
    end
    
    scriptedObject.OnUpdate(onUpdateMethod);
    


    -- "Text" used with GetReference is a UItext canvas object used to display the test text inworld. After you add UI > Text to your scene, drag the text object to your Object References section in your script.
    -- Keep in mind that the docs say that shard properties are erased when all people exit that shard.
    -- You can't test Network methods inside the Unity editor. You'll need to do your testing on Creator.
     
    Mike (CNDG) likes this.