Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-18-2015, 05:00 PM
khortez khortez is offline
PrototypeX
khortez's Avatar
Join Date: Dec 2008
Posts: 91
khortez will become famous soon enough
TStaticVar()

so i think i get it, but i'm not sure and looking for some outside output. Anyone want to explain TStaticVar() to me? It's usage.. downsides of using it. anything really. thanks in advance for any help
Reply With Quote
  #2  
Old 03-19-2015, 10: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
It's useful for when you want to create an object to pass to another function or script. It also provides a clean object slate that doesn't have all the predefined Graal variables.

PHP Code:
function onCreated() {
  
temp.noobj.3;
  
temp.noobj.5;
  
  echo(
temp.noobj.x SPC temp.noobj.y);
  
test(temp.noobj);

  
temp.obj = new TStaticVar();
  
temp.obj.3;
  
temp.obj.5;

  
test(temp.obj);

  
// make sure you destroy them when you're done with them 
  // as they aren't always garbage collected properly...
  
temp.obj.destroy();
}

function 
test(obj) {
  if (
obj.|| obj.y) {
    echo(
obj.x SPC obj.y);
  } else {
    echo(
"no x or y detected");
  }

echos

3 5
no x or y detected
3 5
__________________
Quote:
Reply With Quote
  #3  
Old 03-19-2015, 11:36 AM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Noobject. Liking the term.
__________________
Reply With Quote
  #4  
Old 03-20-2015, 12:37 PM
khortez khortez is offline
PrototypeX
khortez's Avatar
Join Date: Dec 2008
Posts: 91
khortez will become famous soon enough
thanks, i think i understand it a lot better now. So last things i can think of to ask is, I've seen code with the same format. but they looked different. does it act the same? being a blank object and everything? And could I actually use this to say.. pass a GUI through a function? (just brainstorming possibilities)

I was looking for another sample, but i'll just post the one that i do have:


PHP Code:
function onCreated(){
  
temp.panel = new TDrawingPanel();
  
//more temp.panel stuff


the second one i remember was completely different. same format but customized name.

thanks
Reply With Quote
  #5  
Old 03-20-2015, 09:55 PM
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
Hmm, I know I have used this before. But I think I lacked some understanding. Is there anyone that can explain any differences between using a TStaticVar() vs something like savevarstoarray() and loadvarsfromarray() ? In the past I have used both of them to preserve variables such as temp.obj.var.
Reply With Quote
  #6  
Old 03-21-2015, 02:36 PM
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
Hmm, I know I have used this before. But I think I lacked some understanding. Is there anyone that can explain any differences between using a TStaticVar() vs something like savevarstoarray() and loadvarsfromarray() ? In the past I have used both of them to preserve variables such as temp.obj.var.
loadvarsfromarray and savevarstoarray is used for serialization. Useful for when you want to send an object over a method that doesn't support them or save it to a file.

e.g. triggers

PHP Code:
function onCreated() {
  
temp.ex.123;
  
temp.ex.234;
  
temp.ex.345;
  
temp.data temp.ex.savevarstoarray(false);
  
findplayer("fowlplay4").triggerclient("weapon"this.name"data"temp.data);
}

//#CLIENTSIDE

function onActionClientSide() {
  if (
params[0] == "data") {
    
temp.data params[1];
    
temp.ex.loadvarsfromarray(temp.data);
    echo(
temp.ex.a SPC temp.ex.b SPC temp.ex.c); // echos 123 234 345
  
}

__________________
Quote:
Reply With Quote
  #7  
Old 03-21-2015, 07:47 PM
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
loadvarsfromarray and savevarstoarray is used for serialization. Useful for when you want to send an object over a method that doesn't support them or save it to a file.

e.g. triggers

PHP Code:
function onCreated() {
  
temp.ex.123;
  
temp.ex.234;
  
temp.ex.345;
  
temp.data temp.ex.savevarstoarray(false);
  
findplayer("fowlplay4").triggerclient("weapon"this.name"data"temp.data);
}

//#CLIENTSIDE

function onActionClientSide() {
  if (
params[0] == "data") {
    
temp.data params[1];
    
temp.ex.loadvarsfromarray(temp.data);
    echo(
temp.ex.a SPC temp.ex.b SPC temp.ex.c); // echos 123 234 345
  
}

Hmm, well is there any instance that I would be forced to use TStaticVar, or better to use? I generally find myself just using vars like I mentioned. Not sure if that is good practice, or if I should look more into using TStaticVar's?

Sorry if it sounds like a noob question just the overwhelming lack of documentation for GScript2 makes understanding somewhat difficult at times.

Also, thanks FP4 for the reply. Ans also not trying to hijack the thread. It's still relevant to discussion of the TStaticVar.
Reply With Quote
  #8  
Old 03-23-2015, 02:25 AM
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
Quote:
Originally Posted by scriptless View Post
Hmm, well is there any instance that I would be forced to use TStaticVar, or better to use? I generally find myself just using vars like I mentioned. Not sure if that is good practice, or if I should look more into using TStaticVar's?

Sorry if it sounds like a noob question just the overwhelming lack of documentation for GScript2 makes understanding somewhat difficult at times.

Also, thanks FP4 for the reply. Ans also not trying to hijack the thread. It's still relevant to discussion of the TStaticVar.
If you don't want to constantly serialize your dictionary ("object") every time you want to pass it around, you can use a TStaticVar instead. This can be moderately faster and easier to read (less boilerplate).

This won't work going across serverside/clientside, of course -- you'll need to serialize in this case.

Also iirc savevarstoarray on a non-TStaticVar will include the random built-in object properties (stuff like initialized, joinedclasses, timeout, ...) in at least some cases. But I can't remember if that's true or not.
__________________
Reply With Quote
  #9  
Old 03-23-2015, 03:27 PM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
Quote:
Originally Posted by cbk1994 View Post

Also iirc savevarstoarray on a non-TStaticVar will include the random built-in object properties (stuff like initialized, joinedclasses, timeout, ...) in at least some cases. But I can't remember if that's true or not.
You're right.
__________________
Reply With Quote
  #10  
Old 03-23-2015, 08:22 PM
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
Interesting however I am a little confused. So TStaticVar includes object properties... but from where? Does this mean like the player properties if the TStaticVar is a player object, or if I create an object that has properties and then create a TStaticVar of the object?
Reply With Quote
  #11  
Old 03-23-2015, 10:41 PM
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
Interesting however I am a little confused. So TStaticVar includes object properties... but from where? Does this mean like the player properties if the TStaticVar is a player object, or if I create an object that has properties and then create a TStaticVar of the object?
When you create a TStaticVar it has no properties/attributes/etc. You can mold them however you wish and pass them around to functions.

e.g.

PHP Code:
function onCreated() {
  
temp.sword createItem("Sword""sword"9999);
  
temp.shield createItem("Shield""shield"100);
  
echoItemValue(temp.sword); // echos 100
  
echoItemValue(temp.shield); // echos 9999
  
echo(destroySword(temp.sword)); // echos sword destroyed
  
echo(destroySword(temp.shield)); // echos item is not sword
  
temp.shield.destroy();
}

function 
destroySword(item) {
  if (
item.itemtype == "sword") {
    
item.destroy();
    return 
"sword destroyed";
  } else {
    return 
"item is not sword";
  }
}

function 
echoItemValue(item) {
  echo(
item.value);
}

function 
createItem(itemnameitemtypeitemvalueitemmods) {
  
temp.item = new TStaticVar();
  
temp.item.itemname itemname;
  
temp.item.itemtype itemtype;
  
temp.item.value itemvalue;
  
temp.item.mods itemmods;
  return 
temp.item;

__________________
Quote:
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 11:06 PM.


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