Page 1 of 1

Call function from another extension in same zone as1

Posted: 14 Jul 2011, 09:34
by kotbegemot
Is it possible to call function from one extension to another in one zone ?

Posted: 14 Jul 2011, 10:00
by rjgtav
hi. Yes it is possible. You need to use the handleInternalRequest function of an extension (that is similar to the handleRequest) or you can use your own function.
To acces another extension, you can use the zone.getExtension("extensionName") method, and store that extension reference in a var.
Finally, to call a function of that extension, you do extension.myFunction(myParam) where extension is a reference to the extension.

Here.s an example code for a zone extension(written on mobile so may have some errors):

Code: Select all

var zone = _server.getCurrentZone()
var myExtension = zone.getExtension("myExtension")
myExtension.doMethod()
Theres a chapter on www.smartfoxserver.com/docs which talks a little about this.

EDIT

Its the chapter 6.11

Posted: 14 Jul 2011, 10:06
by kotbegemot
VERY VERY VERY THANKS :) !!!!

Posted: 14 Jul 2011, 11:09
by kotbegemot
Are you sure what its must be work on 1.6 SFS ?

Posted: 14 Jul 2011, 11:28
by rjgtav
Well, i've only tested it with SFS PRO 1.6.9 and it works

Posted: 14 Jul 2011, 12:04
by kotbegemot
But SFS 1.6.9 have no getParentZone() method ....

Its method have only SFS 2x

Posted: 14 Jul 2011, 12:38
by rjgtav
Sorry i wrote that on mobile and there i dont have access to the docs xD. It is getCurrentZone() instead of getParentZone()

Posted: 14 Jul 2011, 12:45
by kotbegemot
Im try this too:

In main Extension:

Code: Select all

var zone = _server.getCurrentZone();
var myExtension = zone.getExtension("miniGameShariki")
trace('MY EXTENSION = ' + myExtension);
myExtension.test() 
In caller extension:

Code: Select all

function test() {
	trace(' !!!!!!!!!!!!!!!!! TEST !!!!!!!!!!!!!!!!!!!!!!!!!!');
}


Server was restarted and after restart i have this error:

[whoismore.as]: MY EXTENSION = it.gotoandplay.smartfoxserver.extensions.JavascriptExtension@cb396

Error in extension [ whoismore.as ]: TypeError: Cannot find function test. (whoismore.as#3808) Internal: 2231 -- Line number: 2230 in file: whoismore.as

Posted: 14 Jul 2011, 13:20
by kotbegemot
I make works only this:

Code: Select all

var zone = _server.getCurrentZone();
var targetExtension = zone.getExtension("miniGameShariki")
var responce = targetExtension.handleInternalRequest("Hello");
trace('RESPONCE: ' + responce);

Code: Select all

function handleInternalRequest(obj)
{
	// Simply print the name of the event that was received
	//trace("Event received: " + evt.name)	
	message = String(obj);
	
	if (message.equals("Hello"))
	{
		return "Hello to you!"
	}
	else
	{
		return "Unrecognized message."
	}
	
	
	//var result = String( + " Received ");
	return result;
}

Posted: 14 Jul 2011, 14:11
by rjgtav
Ups :oops: It looks like i mixed java and as... sorry... Yes with as it is only possible by the handleInternalRequest. The other way is only for java classes...