PDA

View Full Version : Creating a variable from a string?


Yen
11-18-2005, 01:55 AM
Is it possible to create a new variable from a string?

For example, if I have:
function onPlayerEnters() {
this.txt = "owner=Yen";
}

Can I make it read this.txt and carry out the string as a function?
In this case, it would set the string 'owner' to 'Yen'

PrinceDark
11-18-2005, 06:53 AM
I ws wondering about something like this as well, but I haven't figured out how to do this in GS2.

I wanted to get something like this below, but in gs2.


// GS1
setstring myvars,apple,banana,fruit;

for ( i = 0; i < sarraylen(myvars); i ++ ) {
setstring this.varname,#I(myvars,i);
setstring this.#s(this.myvarname),#v(random(1,10));
}


Probably a bad example, but I haven't found a way to do this with GS2 and I'm effed since I have no where to script & test online.

edit: Someone told me about a makevar function can help with this, but I can't find any documentation on it.
Any help please?

Admins
11-18-2005, 01:19 PM
http://wiki.graal.us/GS1_To_GS2#Easier_access_to_string_variables

this.(@myvars[i]) = random(1,10);

ApothiX
11-19-2005, 03:49 AM
makevar("varname") = "blah"; works aswell, IIRC.

Yen
11-19-2005, 04:28 AM
You all misunderstood.
I want to make it perform the actions that are in a string.

For example.

this.txt = "putexplosion(2,30,30)";
DoFunctions(this.txt);

This would put an explosion with a radius of 2 at 30,30.

napo_p2p
11-19-2005, 05:06 AM
You all misunderstood.
I want to make it perform the actions that are in a string.

For example.

this.txt = "putexplosion(2,30,30)";
DoFunctions(this.txt);

This would put an explosion with a radius of 2 at 30,30.

I don't think there is a way to call default functions like that. What you could do is have doFunctions trigger 'custom' ones that have the functionality of default ones.

Lance
11-19-2005, 05:38 AM
You all misunderstood.
I want to make it perform the actions that are in a string.

For example.

this.txt = "putexplosion(2,30,30)";
DoFunctions(this.txt);

This would put an explosion with a radius of 2 at 30,30.

If you can call that function, can you not simply call what you were passing to it as an argument?

Skyld
11-19-2005, 03:35 PM
Could have probably named the thread better. >_<
You all misunderstood.
I want to make it perform the actions that are in a string.

For example.

this.txt = "putexplosion(2,30,30)";
DoFunctions(this.txt);

This would put an explosion with a radius of 2 at 30,30.
I know the behaviour you're trying to achieve, but it can't be done unless you're working with a putnpc2.

The function you're looking for is called eval(), which would run whatever you passed to it:
eval("putexplosion(2, 30, 30);");
... but for safety reasons, that function is disabled to prevent people being able to write incredibly malicious scripts and using them to cause hellfire and damnation on servers.

Side-note: This is why you can't type things into the Graal console on some Graal versions which support it!

If you're working with a putnpc2, however, then I suppose you could do:
putnpc2(x, y, "putexplosion(2, 30, 30); destroy();");

Admins
11-19-2005, 06:23 PM
I guess you can dynamically call functions, but function name and parameters must be stored independently, not in one single string. Example:

functionname = "say2";
functionparam1 = "Hello!";
(@functionname)(functionparam1);


or


functionandparams = "putexplosion,2,30,30";
(@functionandparams[0])(functionandparams[1], functionandparams[2], functionandparams[3]);


Such stuff should only be done by experienced scripters though, since it can be quite insecure.

Yen
11-20-2005, 04:47 AM
:D Thanks! That was what I was looking for.

Another question..
Where different functions have a different number of params, is there any way to access an array and use each entry as a seperate parameter?
For example..

functionname = "putexplosion";
functionparamz = {0,player.x,player.y};
(@functionname)(functionparamz);

Admins
11-20-2005, 05:27 AM
Not right now although you can just pass as many parameters as you want, it is only complaining if there are not enough parameters

Yen
11-20-2005, 05:28 AM
:D Thanks a lot!
If Stefan weren't old, I'd marry him. Definately.

jake13jake
11-22-2005, 04:36 AM
overloaded functions would be a handy thing.

napo_p2p
11-22-2005, 04:45 AM
Spiffy.

jake13jake
11-22-2005, 07:14 AM
Such stuff should only be done by experienced scripters though, since it can be quite insecure
hmmm... What makes it insecure?

Skyld
11-22-2005, 10:04 AM
hmmm... What makes it insecure?
If used in, say, a badly thought out triggeraction, then people could abuse it by using hacks to triggeraction and run functions.

jake13jake
11-22-2005, 09:00 PM
If used in, say, a badly thought out triggeraction, then people could abuse it by using hacks to triggeraction and run functions.
a good thing I wouldn't use it that way :D

Skyld
11-22-2005, 09:18 PM
a good thing I wouldn't use it that way :D
I think we agree that it's a good thing if nobody uses it that way.

Admins
11-22-2005, 09:33 PM
overloaded functions would be a handy thing.
You can overload already, or what do you mean exactly ?

jake13jake
11-22-2005, 10:46 PM
You can overload already, or what do you mean exactly ?

Something in the sense where you can have a function of the same name with different sets of parameters, and the function that is called is the one with the correct amount of parameters. It probably wouldn't be that useful in gscript since you can't declare variable types though (but I actually enjoy this flexibility).

like:
function test(String s, int n) vs. function test(int n, String s), but still, if you didn't want to necessarily use all of your parameters it might be useful

In essence (although you probably wouldn't use it this way):

function test(param1) {
chat = param1;
}
function test(param1,param2) {
chat = param2;
headimg = param1;
}

function test(param1,param2,param3) {
chat = param3 @ param2;
headimg = param1;
}


It would also be neat if you had an option of doing:

serverside function onCreated() {}
clientside function onCreated() {}

rather than
function onCreated() {}
//#CLIENTSIDE
function onCreated() {}

Admins
11-23-2005, 12:23 AM
You can still do stuff like this though:

function test() {
switch (params.size()) {
...
}
}

jake13jake
11-23-2005, 03:02 AM
You can still do stuff like this though:

function test() {
switch (params.size()) {
...
}
}

So if you send a function unnamed parameters they're loaded into the params array?
Is it possible to call event handlers when the event hasn't occured? I wouldn't really recommend this either.
Also, would setTimer(0) immediately call onTimeOut?

Yen
11-23-2005, 03:03 AM
Yes.

jake13jake
11-23-2005, 03:35 AM
Yes.
Knowing this is very useful, thank you.

ApothiX
11-23-2005, 04:26 PM
It would also be neat if you had an option of doing:

serverside function onCreated() {}
clientside function onCreated() {}

rather than
function onCreated() {}
//#CLIENTSIDE
function onCreated() {}
I disagree with this. This would lead to extremely badly structured code, and I would assume it would grant people more problems then they already have.

Having the //#CLIENTSIDE directive is a nice way to split your code between serverside and clientside, there is really no need to have it all jumbled together.

Skyld
11-23-2005, 06:16 PM
It would also be neat if you had an option of doing:

serverside function onCreated() {}
clientside function onCreated() {}

rather than
function onCreated() {}
//#CLIENTSIDE
function onCreated() {}
I don't like the idea of your client sending functions to the server, either. Hacking could result in changing the contents of the function being sent.

Seperate them on the serverside, you say? Well, what's wrong with the current //#CLIENTSIDE method?

Admins
11-23-2005, 06:29 PM
So if you send a function unnamed parameters they're loaded into the params array?
They are always loaded into params[]

Is it possible to call event handlers when the event hasn't occured?
Yes, like a function call.
Also, would setTimer(0) immediately call onTimeOut?
No, it would unset the timeout.

Ajira
11-23-2005, 09:32 PM
Also, would setTimer(0) immediately call onTimeOut?
Just do 'onTimeOut();' in your script.

There goes my 1 post per day. ^^

jake13jake
11-26-2005, 07:41 AM
Hmm, is there anyway to load an operator from a string?
like

temp.calc (+)= tokens[i];

Admins
11-26-2005, 05:36 PM
No I don't think

ApothiX
11-26-2005, 05:49 PM
function doOperator(var1, operator, var2) {
switch(operator) {
case "+":
return var1 + var2;
break;

// etc..
}
}

Fry
11-26-2005, 08:21 PM
And then it gets more complex and we get as far as rewriting GS2 in GS2? ;)

An eval with some restrictions (like Ruby's $SAFE variable (http://www.ruby-doc.org/docs/ProgrammingRuby/html/taint.html)) would be a possible replacement maybe. For example, let's call the imaginary function seval and expect a security level and the code that should be evaled. Security level 1 could mean "don't allow triggeractions", 2 could mean "don't allow player warping", or even "variables are read only" and so on.
I am not sure whether that's possible for GS2, depends on the way it has been designed, but certainly it'd be better than putnpc2.

jake13jake
11-27-2005, 12:23 AM
haha, apothix, that way is the obvious way, but if there were a way to take it from a string it would make that code one line, which is why i asked.

Skyld
11-28-2005, 12:45 AM
haha, apothix, that way is the obvious way, but if there were a way to take it from a string it would make that code one line, which is why i asked.
Put custom functions in a class or so.

jake13jake
11-28-2005, 03:56 AM
Put custom functions in a class or so.
Just wondering, where is this coming from?

ApothiX
11-29-2005, 01:30 AM
haha, apothix, that way is the obvious way, but if there were a way to take it from a string it would make that code one line, which is why i asked.
use tokenize, and parse the line

Skyld
11-29-2005, 01:34 AM
Just wondering, where is this coming from?
Well, you said about wanting to use few lines in your script.

Write a function and save it into a class, and join() it, if you're that worried about clutter in your script.

jake13jake
11-29-2005, 10:00 AM
use tokenize, and parse the line
Now I'm confused.
Anyways, I do use tokenize in this script. However, if I were to make it have real functionality, it might be better to parse it myself.
Using tokenize to parse it, you'd want to have custom delimeters +,-,*,/,%,^,(,)
But either way, you're going to have to figure out what you skipped anyway.
So you could parse it yourself, and then store as args and operators. You'd probably store the numbers and operators in scattered arrays, and then try to find some way of applying the order of operations.