Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Function To Change Value (https://forums.graalonline.com/forums/showthread.php?t=74383)

killerogue 06-04-2007 11:03 AM

Function To Change Value
 
I feel as if I'm probably missing something asking this, but as anyone here ever made a function to set a new value to an old one?

I was trying to do something like:

NPC 1


PHP Code:

function onCreated() {
  
core this;
}

function 
setValue(oldValnewVal)
(@ 
oldVal) = (@ newVal); 

NPC 2

PHP Code:

function onCreated() {
  
this.on true;
  
this.on core.setValue(this.on, (this.on = !this.on));
  echo(
this.on);


Echoes nothing. >_>

Skyld 06-04-2007 01:54 PM

PHP Code:

this.on core.setValue(this.on, (this.on = !this.on)); 

Firstly, I am not sure that (this.on = !this.on) will work there; it is probably doing comparison instead of assignation.

Secondly, this is what your function is probably actually doing in this context:
PHP Code:

(@ true) = (@ false); 

Finally, you haven't returned anything from setValue() to set this.on to, so it is probably overwriting it with NULL.

Twinny 06-04-2007 02:01 PM

this.toggle = !this.toggle; will switch between true and false. It's great! ^^

Chompy 06-04-2007 02:39 PM

You can't access this.on from npc1 to npc2.
You give this.on to npc1 and it changes, but npc2 can't get the changed this.on from npc1..

Correct me if I'm wrong..

zokemon 06-04-2007 06:38 PM

You also should make the setValue() function public in order for other npcs to be able to call it.

EDIT:

Here is probably what you are looking for:


NPC 1


PHP Code:

function onCreated() {
  
core this;
}

public function 
setValue(oldValnewVal)
(@ 
oldVal) = newVal

NPC 2

PHP Code:

function onCreated() {
  
this.on true;
  
core.setValue("this.on", (this.on = !this.on));
  echo(
this.on);


I know I changed it quiet a bit but it is sort of hard to see exactly what you want, sorry.

SECOND EDIT:

On second thought, that won't work either as "this.on" will be using the first NPC as "this" (in essence it would be the same as doing "core.setValue("core.on", (this.on = !this.on));")

xAndrewx 06-04-2007 06:47 PM

HTML Code:

public function setValue(oldVal, newVal)
(@ oldVal) = newVal; 

Bad names, here try this

HTML Code:

public function setValue(stringName, newValue)
{
  (@ temp.stringName) = temp.newValue;
}

Also, all you're doing with 'this.on' is re-placing it into your core NPC... :p
You'd use a function like this on an item database, changing local item flags. [updating etc]

zokemon 06-04-2007 06:49 PM

Quote:

Originally Posted by xAndrewx (Post 1315009)
HTML Code:

public function setValue(oldVal, newVal)
(@ oldVal) = newVal; 

Bad names, here try this

HTML Code:

public function setValue(stringName, newValue)
{
  (@ temp.stringName) = temp.newValue;
}

Also, all you're doing with 'this.on' is re-placing it into your core NPC... :p
You'd use a function like this on an item database, changing local item flags. [updating etc]

Quote:

Originally Posted by zokemon (Post 1315007)
SECOND EDIT:

On second thought, that won't work either as "this.on" will be using the first NPC as "this" (in essence it would be the same as doing "core.setValue("core.on", (this.on = !this.on));")

Didn't realize someone would reply so fast!

xAndrewx 06-04-2007 06:51 PM

your second edit makes no sense...

killerogue 06-04-2007 07:35 PM

Quote:

Originally Posted by Chompy (Post 1314942)
You can't access this.on from npc1 to npc2.
You give this.on to npc1 and it changes, but npc2 can't get the changed this.on from npc1..

Correct me if I'm wrong..

Chompy in this case, I believe you are ABSOLUTELY wrong as Chandler made something of this nature before. I just can't remember how it went.

Thanks Chandler, at least I've gotten somewhere now. It's getting the changed value back. But it just continuoulsy echos 0 or 1 depending on how the func is scripted. >_<


EDIT: WOW, I really am stupid. I had "this.on = true;" at the beginning of onCreated function. xD Thanks guys :P

Inverness 06-04-2007 08:56 PM

You don't have to reference temp variables with temp. just leave that out, it looks better.
PHP Code:

public function saveItems() {
  
temp.file =
  
temp.=
  
temp.path =
  
temp.data 0;
  
  for (
this.items.size() - 1> -1--) {
    if (
this.items[i].archname != null) {
      
data this.items[i].getSaveData();
      
file.(@ "item" data[0]) = {data[1], data[2], data[3], data[4]};
    }
  }
  
path MudControl.containerpath this.conttype "/cont" this.contid ".txt";
  
file.saveVars(path0);


Versus
PHP Code:

public function saveItems() {
  for (
temp.this.items.size() - 1temp.> -1temp.--) {
    if (
this.items[temp.i].archname != null) {
      
temp.data this.items[temp.i].getSaveData();
      
temp.file.(@ "item" temp.data[0]) = {temp.data[1], temp.data[2], temp.data[3], temp.data[4]};
    }
  }
  
temp.path MudControl.containerpath this.conttype "/cont" this.contid ".txt";
  
temp.file.saveVars(temp.path0);



xAndrewx 06-04-2007 09:37 PM

Yes you do, else it's stored.

Inverness 06-04-2007 09:48 PM

Quote:

Originally Posted by xAndrewx (Post 1315069)
Yes you do, else it's stored.

Wrong, if you'd notice in the first set of code I declare the temp variables before the actual script, this means that those variables are now temp and can be referenced without temp. and they will still be cleared after the function has ended.

If referencing a variable without the prefix meant its global than why can you reference static variables, most notably in GUI Controls, without the prefix and have them not be global variables?

When you reference a variable without the prefix, the engine first looks for a temp variable with the variable name, if it can't find that it will then look for a static (built-in) variable of the current object (this.), and if it can't find that either it defaults to global.

Function parameters are automatically declared temp so you don't need to bother with that, any other temp variables you can declare them to 0 at the beginning of the function for readability.

Declaring a temp variable to zero initializes it for the duration of the function so the engine will find it before going global.

xAndrewx 06-04-2007 10:02 PM

Do you test anything you say?

HTML Code:

function onCreated()
{
  temp.testA = 0;
  temp.myTest = "|hia";
  testA = "lol";
 
  this.onTest();
  scheduleevent(3, "Test", "");
}
function onTest()
{
  echo(temp.myTest);
  echo(testA);
}

Constantly returns "lol".

Inverness 06-04-2007 10:06 PM

Quote:

Originally Posted by xAndrewx (Post 1315084)
Do you test anything you say?

PHP Code:

function onCreated()
{
  
temp.testA 0;
  
temp.myTest "|hia";
  
testA "lol";
  
  
this.onTest();
  
scheduleevent(3"Test""");
}
function 
onTest()
{
  echo(
temp.myTest);
  echo(
testA);


Constantly returns "lol".

I just tried that, I don't get anything echoed. You've obviously made a mistake before that current compilation.
Variable names are case sensitive so you probably messed up the name of temp.testA and the name didn't match the name of testA = "lol";

Static variables appear to be an exception to this case-sensitive thing though.
PHP Code:

function onCreated() {
  
temp.0;
  
"HELLO!";
  echo(
temp.r);


This script echos "HELLO!";

I've already extensively tested this and the only mistake I made was when I thought temp.variable; automatically initialized it to zero. Your input is not going to change anything.

Also another reason I want the custom object types, so I can have my own static and read-only variables. :frown:

Btw, I recommend the avoidance of any uppercase letters in variable names, object names not included of course.


All times are GMT +2. The time now is 08:35 AM.

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