Team Spartan Cookies & Milk Forums
Spooky action at a distance, remote callbacks (server only, client doesnt work?) - Printable Version

+- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk)
+-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html)
+--- Forum: Starfall (https://teamscm.co.uk/forum-7.html)
+--- Thread: Spooky action at a distance, remote callbacks (server only, client doesnt work?) (/thread-3805.html)



Spooky action at a distance, remote callbacks (server only, client doesnt work?) - Caprica - 21-09-2025

It is entirely possible to trigger a callback on a different starfall chip (if both are on servers).
Below is  code that sends a function via channels, and in a different starfield chip I do a callback, and it will call the function (on the original chip).
I have found it very useful. Though you cannot do the same with net (see bottom).



    hook.add( "playerbuttonup", "hookname", function( ply, key )

        if ( key == input.KEY_8 ) then

            print('SenderServer trigger')

            if ( channels.canSend() ) then

                local command = {}
                command["callback"] = function(response)

                    print('SenderServer ' .. response)
                    serverResponse = response

                end

                channels.sendPrivate(command)

            end

        elseif ( key == input.KEY_7 ) then

            print(serverResponse)

        end

    end )

--ENTIRELY SEPERATE STARFALL--
channels.listenPrivate( 'ReceiverTest', function(chip, received)

        print('ReceiverNet received')

        received["callback"](time.curTime())

    end )


--NET Configuration--

In this scenario, the callback doesnt work, as the function is not sent. If you print the table out the function is missing, i do not know if it was intentional. If it was possible to do this, it would be very cool as you could access variables directly. 



if ( SERVER ) then

    hook.add( "net", "hookname", function( length, player )

        local received = net.readTable()

        received["callback"](time.curTime())

    end )

    hook.add( "playerbuttonup", "hookname", function( ply, key )

        if ( key == input.KEY_8 ) then

            print('SenderServer trigger')

            if ( channels.canSend() ) then

                local command = {}
                command["callback"] = function(response)

                    print('SenderServer ' .. response)
                    serverResponse = response

                end

                channels.sendPrivate(command)

            end

        elseif ( key == input.KEY_7 ) then

            print(serverResponse)

        end

    end )

elseif ( CLIENT ) then


    hook.add( "playerbuttonup", "hookname", function( ply, key )

        if ( key == input.KEY_9 ) then

            print('SenderClient trigger')

            if ( net.start() ) then

                local command = {}
                command["callback"] = function(response)

                    print('SenderClient ' .. response)

                end

                net.writeTable(command)

                net.sendToServer()

            end

        end


    end )

end




RE: Spooky action at a distance, remote callbacks (server only, client doesnt work?) - The17thDoctor - 22-09-2025

It seems normal that the second approach with net won't work.

When you store a function as a variable, you're storing a reference to that function, not the function itself.

When the net library sends a table to your client, the most probable behaviour that happens is that when it sees that function reference, it just discards it.
Why ? Because that function only makes sense in the context of the server. Even if you were to send the reference to the client, it would be pointing to a random part of your computer's memory (which is very probably not gmod's memory).

To better illustrate, let's take this simple code here :
--@shared

function a()
print("Hello world!")
end

local realm = "???"
if SERVER then realm = "SERVER" end
if CLIENT then realm = "CLIENT" end

print(realm .. " : " .. tostring(a)

When I run this starfall, I get this result :
SERVER : function: 0xec9efc7a
CLIENT : function: 0x8088096aeae87c2a

As you can see, the function is stored at wildly different places on server vs. on client, and that's the same function !

This doesn't happen with channels because although the starfall instances seem separate, it's all ran by GLua under the hood, so the reference does resolve and is able to be executed.

Aside from that it's interesting to see what could be done with that trick.


RE: Spooky action at a distance, remote callbacks (server only, client doesnt work?) - Caprica - 22-09-2025

Im finding it useful on the server side

Ultimately what im trying to do is have a client communicate with another server chip that controls the shields/tanspoter/etc where a callback is nice.
So you press the button it sends a command and the shield controller can send the result back (e.g. you tried to raise the shield but subsystem health is 0).

I know you can do callbacks after a fashion using ids, but then you need to make timers, and have a grand unified hook for net.