Team Spartan Cookies & Milk Forums
  • Issue Tracker
  • Donations
  • Search
  • Member List
  • Help

Register

Team Spartan Cookies & Milk Forums Garry's Mod Starfall v
1 2 3 4 5 … 18 Next »
Spooky action at a distance, remote callbacks (server only, client doesnt work?)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threaded Mode
Spooky action at a distance, remote callbacks (server only, client doesnt work?)
21-09-2025, 06:10 PM,
#1
Caprica Offline
Valued donator and member
****
Posts: 76
Threads: 24
Joined: Aug 2020
Spooky action at a distance, remote callbacks (server only, client doesnt work?)
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

Find
Reply
22-09-2025, 03:28 AM,
#2
The17thDoctor Offline
TSCM Average Enjoyer
****
Posts: 1,100
Threads: 249
Joined: Oct 2015
RE: Spooky action at a distance, remote callbacks (server only, client doesnt work?)
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.

The17thDoctor: Server take me to the most handsome person
srcds_linux: I cant send people to themselves...
Website Find
Reply
22-09-2025, 03:14 PM,
#3
Caprica Offline
Valued donator and member
****
Posts: 76
Threads: 24
Joined: Aug 2020
RE: Spooky action at a distance, remote callbacks (server only, client doesnt work?)
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.
Find
Reply
« Next Oldest | Next Newest »


  • View a Printable Version
Forum Jump:


Users browsing this thread:
Contact UsTeam Spartan Cookies & MilkReturn to TopLite (Archive) ModeMark all forums readRSS Syndication

Powered By MyBB, © 2002-2026 MyBB Group.