Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   TStaticVar() (https://forums.graalonline.com/forums/showthread.php?t=134269909)

khortez 03-18-2015 05:00 PM

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

fowlplay4 03-19-2015 10:58 AM

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

Crow 03-19-2015 11:36 AM

Noobject. Liking the term.

khortez 03-20-2015 12:37 PM

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

scriptless 03-20-2015 09:55 PM

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.

fowlplay4 03-21-2015 02:36 PM

Quote:

Originally Posted by scriptless (Post 1735528)
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
  
}



scriptless 03-21-2015 07:47 PM

Quote:

Originally Posted by fowlplay4 (Post 1735531)
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.

cbk1994 03-23-2015 02:25 AM

Quote:

Originally Posted by scriptless (Post 1735534)
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.

xAndrewx 03-23-2015 03:27 PM

Quote:

Originally Posted by cbk1994 (Post 1735542)

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.

scriptless 03-23-2015 08:22 PM

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?

fowlplay4 03-23-2015 10:41 PM

Quote:

Originally Posted by scriptless (Post 1735545)
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;




All times are GMT +2. The time now is 07:27 AM.

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