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, we have two Skype groups for space early users - if you're creating content, or need help, feel free to join in either the the beginners or advanced users groups.
    Dismiss Notice

Script Snippets [LUA]

Discussion in 'General' started by Mike (CNDG), Apr 3, 2017.

Share This Page

  1. Mike (CNDG)

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

    Joined:
    Apr 2, 2017
    Messages:
    15
    Likes Received:
    16
    Hi there,

    I'm in the process of getting hands dirty with the scripting runtime component, and decided to share the little snippets I create in hopes they help others getting into it (Make sure you share yours too!)

    Simple Light Toggle
    The first is a simple 'Click to toggle the object light'.
    For some reason, SLight.Enabled setter wasn't working, so this one changes the intensity depending on the on/off state.

    Create a Sphere, add a light and a Script Runtime, and copy the script below.

    Code:
    local this = Space.Host.ExecutingObject;
    local state = 1;
    
    -- Toggle Light --
    local evtClick = function()
        if(state == 0) then
            this.Light.Intensity = 3.0;
            Space.Log("Light Switched On");
            state = 1;
        else
            this.Light.Intensity = 0.0;
            Space.Log("Light Switched Off");
            state = 0;
        end
    end
    
    -- Initiate Object --
    this.SubscribeToEvents();
    this.OnMouseDown(evtClick);
    

    Regards
     
    Adam likes this.
  2. Serp

    By:SerpApr 3, 2017
    New Member

    Joined:
    Oct 10, 2016
    Messages:
    5
    Likes Received:
    1
    Code:
    local split = function(str,sep)
        local array = {};
        local reg = string.format("([^%s]+)",sep);
        for mem in string.gmatch(str,reg) do
            table.insert(array, mem);
        end
        return array;
    end
    
    for parsing strings since lua doesn't have one built in. Returns an array.
    I would like to build a library of useful functions like this.
     
    Mike (CNDG) likes this.
  3. Adam

    By:AdamApr 3, 2017
    Developer
    Staff Member Moderator

    Joined:
    Aug 21, 2014
    Messages:
    630
    Likes Received:
    113
    I'm actually in the process of adding library support with EP10 - then you can make a bunch of .lua files which are auto-imported at the top of your scripts for defining functions. So building up libs is a good idea.
     
    AshaSekayi and Mike (CNDG) like this.
  4. Mike (CNDG)

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

    Joined:
    Apr 2, 2017
    Messages:
    15
    Likes Received:
    16
    Changing Object Colour
    Code:
    -- Parameters:  Key, Red, Green, Blue, Alpha
    Space.Host.ExecutingObject.Renderer.Material.SetColor("_Color", 1.0, 0.5, 0.5, 1.0);
    
    -- Quick Function
    function SetColor(R, G, B, A)
        Space.Host.ExecutingObject.Renderer.Material.SetColor("_Color", R, G, B, A);   
    end
    
    SetColor(1.0, 0.5, 0.5, 1.0);
    
    Space Material Class Reference: http://space-files.s3.amazonaws.com/docs/client-scripting/class_s_material.html
    Unity Reference: https://docs.unity3d.com/ScriptReference/Material.SetColor.html


    The 'Key' parameter points to the Shader's Property Name (Standard has _Color and _EmissionColor )

    Regards,
     
    Last edited: Apr 4, 2017