Author Topic: Pointer to function within class  (Read 299 times)

Rhytz

  • Newbie
  • *
  • Posts: 30
  • Karma: +13/-7
    • View Profile
    • Rhytz's Website
Pointer to function within class
« on: January 31, 2017, 03:18:32 pm »
A lot of LU functions take a string or pointer to a callback function. However, there seems to be no way to point to a function within a class.

Take the code below for example:
Code: [Select]
class Example {
mainSocket = null;

constructor(port){
mainSocket = NewSocket( this.ReceiveData );
mainSocket.SetNewConnFunc( this.Connected );
mainSocket.Start( port, 64 );
::print("Loaded example class and opened socket");
}

function Connected( socket, clientID, clientIP, clientPort ){
::print("Socket connected");
}

function ReceiveData( socket, clientID, data ){
::print("Received data");
}
}
ExampleClass <- Example(2345);

The socket is opened and can be connected to. However, the functions Connected and ReceiveData never get called. Script generates no errors. I've tried to make every possible reference to these functions I could think of, but nothing happens. Am I missing something, or is this just impossible to do?
Meet everyone on the Liberty Unleashed Discord || Check out my website/portal!

Theremin

  • Full Member
  • ***
  • Posts: 152
  • Karma: +48/-18
  • Worst Server Owner
    • View Profile
    • Visit my YouTube channel
Re: Pointer to function within class
« Reply #1 on: February 06, 2017, 08:20:28 am »
I was searching the forum for stuff and happened to step into this:
Nice tutorial, especially the environment part. :) Everyone dealing with classes in their scripts should check that bit out.

I would also like to add that you can access everything in the root table if you prefix the slot name with :: . This will work in every environment (=scope), allowing you to use 'global variables' safely. In fact, you'll have to prefix LU native functions with :: if you want to access them safely within a class. Forgetting :: from a native call is a common mistake.
Maybe this will solve your issue? ;)

Rhytz

  • Newbie
  • *
  • Posts: 30
  • Karma: +13/-7
    • View Profile
    • Rhytz's Website
Re: Pointer to function within class
« Reply #2 on: February 06, 2017, 11:09:20 am »
Thanks Theremin, I will experiment some more. Too bad the original videos in that thread were removed, would be interesting to see what they were about.

If I could get this working it would greatly improve for example the websdk script to make it more dynamic.
Meet everyone on the Liberty Unleashed Discord || Check out my website/portal!

 

© Liberty Unleashed Team.