Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   return, function, and object types? (https://forums.graalonline.com/forums/showthread.php?t=134265641)

scriptless 01-28-2012 08:24 AM

return, function, and object types?
 
So I was playing with some script and came up with this:

PHP Code:

function onCreated() {
  
temp.obj2.hello "world";
  
temp.obj temp.obj2;
  
player.chat temp.obj2.hello;


Which output's:
PHP Code:

hello 

But for some reason this doesn't work. Maybe I am doing it wronge?

PHP Code:

function onCreated() {
  
temp.obj2 test();
  
player.chat temp.obj2.hello;
}

function 
test() {
  
temp.obj.hello "world";
  return 
temp.obj;


Which output's:
PHP Code:




fowlplay4 01-28-2012 08:26 AM

You have to create a TStaticVar then assign it a variable. I.e:

PHP Code:

function onCreated() { 
  
temp.obj2 test(); 
  echo(
temp.obj2.hello);
  
temp.obj2.destroy();
  
  
// this also works
  
echo(test().hello);


function 
test() {
  
temp.obj = new TStaticVar();
  
temp.obj.hello "world"
  return 
temp.obj


You can write some neat looking code when you return object references.

scriptless 01-28-2012 08:31 AM

Quote:

Originally Posted by fowlplay4 (Post 1682955)
You have to create a TStaticVar then assign it a variable. I.e:

PHP Code:

function onCreated() { 
  
temp.obj2 test(); 
  echo(
temp.obj2.hello);
  
temp.obj2.destroy();
  
  
// this also works
  
echo(test().hello);


function 
test() {
  
temp.obj = new TStaticVar();
  
temp.obj.hello "world"
  return 
temp.obj


You can write some neat looking code with it.

Ah, I remember using TStaticVar() once about a year ago. Forgot all about it. Indeed, yes you can write some pretty neat code.

Quote:

Originally Posted by fowlplay4 (Post 1682955)
PHP Code:

echo(test().hello); 


Now that is awesome!!


Thanks alot.

Would rep+ you but I must share the love first, lol.

fowlplay4 01-28-2012 08:39 AM

Bonus: If you want to send an object and it's vars to the client-side you can do it like this:

PHP Code:

function onCreated() {
  
temp.example = new TStaticVar(); // not necessary in this example
  
temp.example."hello ";
  
temp.example."world";
  
temp.example."!";
  
temp.example.b." <3";
  
temp.data temp.example.savevarstoarray(false);
  
findplayer("fowlplay4").triggerclient(this.name"data"temp.data);
}

//#CLIENTSIDE

function onActionClientside() {
  if (
params[0] == "data") {
    
temp.data params[1];
    
temp.example = new TStaticVar();
    
temp.example.loadvarsfromarray(temp.data);
    echo(
temp.example.temp.example.temp.example.temp.example.b.a);
  }



scriptless 01-28-2012 08:42 AM

Quote:

Originally Posted by fowlplay4 (Post 1682957)
Bonus: If you want to send an object and it's vars to the client-side you can do it like this:

PHP Code:

function onCreated() {
  
temp.example = new TStaticVar(); // not necessary in this example
  
temp.example."hello ";
  
temp.example."world";
  
temp.example."!";
  
temp.example.b." <3";
  
temp.data temp.example.savevarstoarray(false);
  
findplayer("fowlplay4").triggerclient(this.name"data"temp.data);
}

//#CLIENTSIDE

function onActionClientside() {
  if (
params[0] == "data") {
    
temp.data params[1];
    
temp.example = new TStaticVar();
    
temp.example.loadvarsfromarray(temp.data);
    echo(
temp.example.temp.example.temp.example.temp.example.b.a);
  }



Curiosity, why do we need savevarstoarray(false)? And no need for TStaticVar(); except after clientside triggered..? Interesting.

fowlplay4 01-28-2012 08:58 AM

Quote:

Originally Posted by scriptless (Post 1682958)
Curiosity, why do we need savevarstoarray(false)? And no need for TStaticVar(); except after clientside triggered..? Interesting.

triggerclient doesn't support objects, so it converts the object into the string representation of itself (often it's .name value).

PHP Code:

function onCreated() { 
  
temp.example = new TStaticVar("a silly test object");
  
temp.example."hello "
  
temp.example."world"
  
temp.example."!"
  
temp.example.b." <3"
  
findplayer("fowlplay4").triggerclient(this.name"data"temp.example); 
  
temp.example.destroy();
}

//#CLIENTSIDE

function onActionClientside() { 
  if (
params[0] == "data") { 
    
temp.data params[1];
    echo(
"data: " temp.data); // echos "a silly test object"
  



So you have to package the object up and send it as an array/string then rebuild it on the client-side which those two built-in functions do nicely.

Also the new TStaticVar() on the server-side wasn't necessary because you weren't passing it between two functions so when you call savevarsintoarray it's able to find the a, b, c, and b.a vars.

Tricxta 01-28-2012 09:03 AM

that's cool :0

fowlplay4 01-28-2012 09:13 AM

More Bonus: Chaining function calls, and that'll pretty much be it for me on this topic.

PHP Code:

function onCreated() {
  
a().b().c().d();
}

public function 
a() {
  echo(
"a");
  return 
this;
}

public function 
b() {
  echo(
"b");
  return 
this;
}

public function 
c() {
  echo(
"c");
  return 
this;
}

public function 
d() {
  echo(
"d");
  return 
this;


If you're familiar with jQuery it does a lot of that. I've wrote a couple systems that use it as well:

Basic ActiveRecord
http://forums.graalonline.com/forums...hp?t=134262561

CuteGS2 GUI Builder
http://forums.graalonline.com/forums...hp?t=134263993

cbk1994 01-28-2012 07:29 PM

Just be careful to destroy TStaticVars when you're done with them or you'll leak memory.

edit: maybe this is not necessary?

Quote:

Originally Posted by Stefan (Post 1544225)
you need to call destroy() on the player object after you have used it, when you do new TServerPlayer() it is not automatically destroyed like TStaticVars()

Apparently they destroy themselves, although in the past it's seemed that they don't.

edit 2: per Skyld, they'll be destroyed eventually if there are no references to them.

WhiteDragon 01-28-2012 11:48 PM

Note that the garbage collection cycles happen when you move your mouse out of the client, and can lock up the client for up to 30 seconds collecting as little as 1000 TStaticVars. (This was a cause of a bug that took me ages to work out.) In the case you are working with a dynamic number of TStaticVars, it's best to manually destroy when you're done with them if you can.


All times are GMT +2. The time now is 01:14 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.