Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-28-2012, 08:24 AM
scriptless scriptless is offline
Banned
Join Date: Dec 2008
Location: N-Pulse
Posts: 1,412
scriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to behold
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:

Reply With Quote
  #2  
Old 01-28-2012, 08:26 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
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.
__________________
Quote:
Reply With Quote
  #3  
Old 01-28-2012, 08:31 AM
scriptless scriptless is offline
Banned
Join Date: Dec 2008
Location: N-Pulse
Posts: 1,412
scriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to behold
Quote:
Originally Posted by fowlplay4 View Post
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 View Post
PHP Code:
echo(test().hello); 
Now that is awesome!!


Thanks alot.

Would rep+ you but I must share the love first, lol.
Reply With Quote
  #4  
Old 01-28-2012, 08:39 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
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);
  }

__________________
Quote:
Reply With Quote
  #5  
Old 01-28-2012, 08:42 AM
scriptless scriptless is offline
Banned
Join Date: Dec 2008
Location: N-Pulse
Posts: 1,412
scriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to behold
Quote:
Originally Posted by fowlplay4 View Post
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.
Reply With Quote
  #6  
Old 01-28-2012, 08:58 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Quote:
Originally Posted by scriptless View Post
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.
__________________
Quote:
Reply With Quote
  #7  
Old 01-28-2012, 09:03 AM
Tricxta Tricxta is offline
The Muffin Man
Tricxta's Avatar
Join Date: Oct 2010
Location: Australia
Posts: 563
Tricxta is a jewel in the roughTricxta is a jewel in the rough
that's cool :0
Reply With Quote
  #8  
Old 01-28-2012, 09:13 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
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
__________________
Quote:
Reply With Quote
  #9  
Old 01-28-2012, 07:29 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
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 View Post
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.
__________________
Reply With Quote
  #10  
Old 01-28-2012, 11:48 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
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.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 08:14 PM.


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