Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Question about "autodetecting" variables. (https://forums.graalonline.com/forums/showthread.php?t=134259292)

Jiroxys7 05-24-2010 12:31 PM

Question about "autodetecting" variables.
 
okay, so i'm trying to set up my system to read a variable and use that variable to create the name of another variable to read from. say if i wanted a weapon to calculate damage from an unusual stat such as say, accuracy. i could just put something like damagebase=accuracy in the weapon file to make things easier.

in this example, we're just going to manually set the kind of damage the weapon would be using in client.weapon_damagebase.

with this line, i can get it to put together an existing variable name:
PHP Code:

this.damagebase "client.stats_" client.weapon_damagebase

so if i send this.damagebase through to the player's chat, they'll say "client.stats_accuracy"

now the actual problem is that although i've managed to make it put that together, i cant figure out how to tell it to read from that variable since my attempts have always made this.damagebase return "client.stats_accuracy". i think my problem is that it's treating what's stored, as a string, and not actually a variable. what would i use to tell it to read from the actual client.stats_accuracy variable in this manner?

cbk1994 05-24-2010 12:55 PM

To create dynamic variables you have two options.

First (best):
PHP Code:

object.(@ "something")
(@ 
"object.something")
(@ 
this.damagebase

Second (don't use, please):
PHP Code:

makevar("object.var"

So if you were calculating a damage multiplier you would do
PHP Code:

function getPlayerDamageMultiplier() {
  return (@ 
this.damagebase);



Jiroxys7 05-24-2010 03:41 PM

I cant seem to get it to work. your way didnt seem to make this.damagebase equal anything but null no matter which way i tried it. so perhaps im doing it wrong. i also tried doing both:

PHP Code:

this.damagebase "client.stats_" client.weapon_damagebase;
makevar(this.damagebase);
player.chat this.damagebase

and

PHP Code:

this.damagebase "client.stats_" client.weapon_damagebase;
makevar("this.damagebase");
player.chat this.damagebase

to see if that would even work, but my char still said "client.stats_strength" instead of the actual value stored in client.stats_strength.

Also, why is your first example preferred over the second? Is it more versatile, or is it just outdated (i.e. gs1)?

Tolnaftate2004 05-24-2010 05:21 PM

Quote:

Originally Posted by Jiroxys7 (Post 1578301)
...im doing it wrong....

PHP Code:

this.damagebase "client.stats_" client.weapon_damagebase;
player.chat makevar(this.damagebase); 


Crow 05-24-2010 05:31 PM

Quote:

Originally Posted by cbk1994 (Post 1578285)
Second (don't use, please):
PHP Code:

makevar("object.var"


Why not?

fowlplay4 05-24-2010 06:04 PM

Quote:

Originally Posted by Crow (Post 1578308)
Why not?

Well you call a function and the other doesn't from what I understand, so it's just one of those unnoticeable differences.

Personally I used makevar at first but eventually migrated to the object.("var") way, it was a lot easier to work with after I got use to it.

xAndrewx 05-24-2010 09:20 PM

Quote:

Originally Posted by fowlplay4 (Post 1578310)
Well you call a function and the other doesn't from what I understand, so it's just one of those unnoticeable differences.

Personally I used makevar at first but eventually migrated to the object.("var") way, it was a lot easier to work with after I got use to it.

Yeah, me too ^^

cbk1994 05-24-2010 10:03 PM

Quote:

Originally Posted by Crow (Post 1578308)
Why not?

makeVar is disgusting and counter-intuitive in an object-oriented programming language.

Jiroxys7 05-25-2010 03:38 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1578307)
PHP Code:

this.damagebase "client.stats_" client.weapon_damagebase;
player.chat makevar(this.damagebase); 


okay, in my attributes, i have client.weapon_damagebase=strength
also, in my attributes, client.stats_strength currently equals 190

my script, of course contains
PHP Code:

this.damagebase "client.stats_" client.weapon_damagebase

using player.chat = makevar(this.damagebase); set my chat text to "client.weapon_damagebase" when its supposed to say "190". i also just tried using player.chat = (@ this. damagebase); and got the exact same result. all of this is being done clientside. but i dont think anything needs to go through the serverside, so i dont think that could be the problem. What else might I be doing wrong?

edit: hold on, it seems that someone broke my server and now nothing's updating >:0

salesman 05-25-2010 04:59 AM

try
PHP Code:

this.damagebase player.client.(@ "stats_" player.client.weapon_damagebase); 

I hope this just for displaying information in an interface or something that doesn't need to be secure...you shouldn't be using client.vars otherwise. If not, I can help you figure out a better way to do what it is you need to do.

Jiroxys7 05-25-2010 05:29 AM

Quote:

Originally Posted by salesman (Post 1578452)
try
PHP Code:

this.damagebase player.client.(@ "stats_" player.client.weapon_damagebase); 

I hope this just for displaying information in an interface or something that doesn't need to be secure...you shouldn't be using client.vars otherwise. If not, I can help you figure out a better way to do what it is you need to do.

I plan on changing that later. dont you calculate and set variables as .clientr serverside or something for that?

salesman 05-25-2010 05:49 AM

clientr.vars can be read by both the client and server, but can only be altered on the serverside

Jiroxys7 05-25-2010 06:00 AM

so if i needed to send a variable over to serverside to be calculated for a clientr.var, would i send a temp.var or a this.var? or something else?

salesman 05-25-2010 07:19 AM

Quote:

Originally Posted by Jiroxys7 (Post 1578474)
so if i needed to send a variable over to serverside to be calculated for a clientr.var, would i send a temp.var or a this.var? or something else?

I'm not exactly the best teacher, so you probably want someone else to help you out, however I will try!

temp.variables are only accessible in the function where they are defined. For example,
PHP Code:

function someFunction() {
  
temp.foo 3.14159// define temp.foo in this function block
  
  
echo(temp.foo// echoes 3.14159

  
otherFunction(); // call a new function
}

function 
otherFunction() {
  echo(
temp.foo); // this will NOT echo anything, because temp.foo was not defined in this function
  
  // however, I can define an entirely new temp.foo, completely unrelated to the temp.foo in someFunction()
  
temp.foo "test";


Parameters passed to a function are also temp.vars and are only accessible within the function block.

this.variables on the other hand are assigned to the object in which they are defined (hence "this") and are accessible throughout the object

for example,
PHP Code:

function someFunction() {
  
this.foo "bar";
  
  echo(
this.foo); // echoes "bar"

  
otherFunction();
}

function 
otherFunction() {
  echo(
this.foo); // echoes "bar"


I can even access the variable from other objects as long as I have a reference to the object in which it was defined. Say this.foo was declared in a DBNPC called "MyNPC"...I can access the variable "foo" with the following statement from another NPC:
PHP Code:

// Another DB NPC

function inAnotherNPC() {
  echo(
MyNPC.foo); // echoes "bar"


It is important to note that a this.var defined on the serverside cannot be accessed on the clientside (or vice-versa). So how do you send data between the two? There are a few methods, but most notably, triggerServer() and triggerClient().

triggerServer() is called from the clientside and will invoke the event "onActionServerSide()" on the serverside. Alternatively, triggerClient() will invoke "onActionClientSide()" on the clientside.

Both require two parameters, and also allow an arbitrary (i think?) number of extra parameters.
  1. The first parameter will be the type of NPC you are triggering ("weapon", "gui", or "npc"). weapon and gui are interchangeable, and should be used for WNPCs, npc should be used for DB NPCs.
  2. The second is the name of the NPC

for example, I want to call a function in a weapon NPC on the serverside from the clientside of the same weapon:
PHP Code:

// invoked whenever triggerServer() is used on this weapon
function onActionServerSide(cmd) {
  
// "cmd" is a parameter that I specified in order to ensure that the trigger is the one I want
  
if (temp.cmd == "test") { // I sent "test" as a parameter from the clientside
    
myServersideFunction();
  }
}

//#CLIENTSIDE

function someFunction() {
  
triggerServer("weapon"this.name"test");



So to answer your question, you can send any type of variable you want as a parameter via triggerServer(). You can then assign the parameter on the serverside to a clientr.var, this.var, or pass it to another function...whatever you want. If this is confusing just ignore everything I said and wait for someone else to help you :p

Jiroxys7 05-25-2010 07:50 AM

I believe im fairly familiar with using the serverside <=> clientside stuff. didnt know the difference between temp.var and this.var though =3

so if i want to create an important clientr.var, i should do something like this?:

example:

PHP Code:

function onActionServerSide(cmd) {
  if (
temp.cmd == "calc_math") {
    
doMakeMathVar_S();
  }
}

function 
doMakeMathVar_S(){
   
clientr.num this.num1 this.num2;
}

//#CLIENTSIDE

function doMakeMathVar_C() {
  
this.num1 1;
  
this.num2 2;
  
triggerServer("weapon"this.name"calc_math"this.num1this.num2);


clientr.num should equal 3.

now, I'm wondering though, are people able to hack and edit this.num1 and this.num2 to change the outcome of clientr.num? or is it just harder to hack compared to client.vars?


All times are GMT +2. The time now is 10:03 AM.

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