Results 1 to 3 of 3

Thread: Better script sharing - function.bind

  1. #1
    Join Date
    Feb 2017
    Posts
    178

    Better script sharing - function.bind

    So, from what I understand, this may be a limitation with Moonsharp apparently, but it's worth asking.

    there are two things that I want to showcase/request.
    - I would love to be able to alter functions of an object on another object's script.
    - in so doing, being able to bind the "self" definition to another object. I come from a javascript background and it's VERY useful to be able to define what "this" (in lua that's "self") is when you're assigning a function.

    Code:
    function onCollideEnter(obj)
       local newPickupFunc = function(player)
          print(player.color)
          self.onPickup = nil -- set the onPickupEvent on the owner of this function (bound to 'obj' below) to nothing
       end
       obj.onPickup = newPickupFunc.bind(obj) -- assign the function to the onPickup event while defining what 'self' within that function be 'obj'
    end

  2. #2
    There is no equivalent to this in lua. The closure of a function is created with it, and only passed variables will ever change (Without the debug library)

    To achieve this effect, you could do something along the lines of

    Code:
    do
      local META
      META = {
        __call = function(t, ...)
          return t[1](t[2], ...)
        end,
        __index = function(t, k)
          return ({
            bind = function(newSelf)
              return setmetatable({t[1], newSelf}, META)
            end
          })[k]
        end
      }
      function Bindable(func)
        return setmetatable({func, self}, META)
      end
    end
    function onCollideEnter(obj)
      local newPickupFunc = Bindable(function(self, player)
         print(player.color)
         self.onPickup = nil -- set the onPickupEvent on the owner of this function (bound to 'obj' below) to nothing
      end)
      obj.onPickup = newPickupFunc.bind(obj) -- assign the function to the onPickup event while defining what 'self' within that function be 'obj'
    end
    The fact that functions cannot be shared between scripts isn't really a moonscript limitation, however it would have to be specially handled, as the closure would have to be dynamically built whenever we were handing it over. There is no right way to do this, so I dont blame the devs from avoiding it.

    If you want events to change during the game, I would recommend handling everything in global - Make an Object class of your own in the style of an eventemitter, then emit on it using the onObjectPickup event in global. For the collision events, you can place

    Code:
    function onCollisionEnter(...)
      Global.call("onObjectCollisionEnter", self, ...)
    end
    as shims to also handle them in global

  3. #3
    The fact that functions can't be shared between scripts is *exactly* a moonsharp limitation, see below
    https://groups.google.com/d/msg/moon...M/YTLlc-6ZCAAJ

    So we're pretty much limited to mashing everything into one object (or Global) script if you want to have a managed event calling system sadly.

Similar Threads

  1. Replies: 4
    Last Post: 01-19-2017, 07:04 PM
  2. Replies: 6
    Last Post: 08-17-2016, 12:28 PM
  3. Call a global function from a local script
    By enigma in forum Scripting
    Replies: 2
    Last Post: 08-09-2016, 02:14 AM
  4. Object Function Using Global Function
    By LordAshes in forum Scripting
    Replies: 6
    Last Post: 07-11-2016, 05:18 AM
  5. Replies: 2
    Last Post: 04-28-2015, 02:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •