Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > New Scripting Engine (GS2)
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11-18-2005, 01:55 AM
Yen Yen is offline
Banned
Yen's Avatar
Join Date: Oct 2005
Location: Nova Scotia, Canada
Posts: 1,085
Yen is an unknown quantity at this point
Send a message via AIM to Yen Send a message via MSN to Yen
Creating a variable from a string?

Is it possible to create a new variable from a string?

For example, if I have:
NPC Code:
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'
Reply With Quote
  #2  
Old 11-18-2005, 06:53 AM
PrinceDark PrinceDark is offline
Criminal X
PrinceDark's Avatar
Join Date: Feb 2003
Location: Miami, Florida
Posts: 662
PrinceDark will become famous soon enough
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.

NPC Code:

// 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?
__________________
- Criminal X: certified nut case
Reply With Quote
  #3  
Old 11-18-2005, 01:19 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
http://wiki.graal.us/GS1_To_GS2#Easi...ring_variables

this.(@myvars[i]) = random(1,10);
Reply With Quote
  #4  
Old 11-19-2005, 03:49 AM
ApothiX ApothiX is offline
Okiesmokie
Join Date: May 2004
Posts: 1,447
ApothiX is on a distinguished road
makevar("varname") = "blah"; works aswell, IIRC.
__________________


[06:24:19] * Parts: Skyld (i=silent@unaffiliated/skyld) ("Perhaps Okiesmokie did not realise that I like the boys. ")
Reply With Quote
  #5  
Old 11-19-2005, 04:28 AM
Yen Yen is offline
Banned
Yen's Avatar
Join Date: Oct 2005
Location: Nova Scotia, Canada
Posts: 1,085
Yen is an unknown quantity at this point
Send a message via AIM to Yen Send a message via MSN to Yen
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.
Reply With Quote
  #6  
Old 11-19-2005, 05:06 AM
napo_p2p napo_p2p is offline
oh snaps
napo_p2p's Avatar
Join Date: Sep 2003
Location: Pismo Beach, California
Posts: 2,118
napo_p2p has a spectacular aura aboutnapo_p2p has a spectacular aura about
Send a message via AIM to napo_p2p Send a message via MSN to napo_p2p
Quote:
Originally Posted by Yen
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.
__________________
Scito hoc super omnia.
Haec vita est tua una sola.
Dum vita superest, utere maxime quoque puncto, momento, et hora quae habes.
Tempus neminem non manet.
Noli manere tempus.
Carpe Diem

Seize the Day.
Reply With Quote
  #7  
Old 11-19-2005, 05:38 AM
Lance Lance is offline
dark overlord
Lance's Avatar
Join Date: Sep 2003
Location: Space Jam Mountain
Posts: 5,072
Lance is on a distinguished road
Quote:
Originally Posted by Yen
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?
Reply With Quote
  #8  
Old 11-19-2005, 03:35 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Could have probably named the thread better.
Quote:
Originally Posted by Yen
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:
NPC Code:
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:
NPC Code:
putnpc2(x, y, "putexplosion(2, 30, 30); destroy();");

__________________
Skyld
Reply With Quote
  #9  
Old 11-19-2005, 06:23 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
I guess you can dynamically call functions, but function name and parameters must be stored independently, not in one single string. Example:
PHP Code:
functionname "say2";
functionparam1 "Hello!";
(@
functionname)(functionparam1); 
or

PHP Code:
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.
Reply With Quote
  #10  
Old 11-20-2005, 04:47 AM
Yen Yen is offline
Banned
Yen's Avatar
Join Date: Oct 2005
Location: Nova Scotia, Canada
Posts: 1,085
Yen is an unknown quantity at this point
Send a message via AIM to Yen Send a message via MSN to Yen
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..

PHP Code:
functionname "putexplosion"
functionparamz = {0,player.x,player.y}; 
(@
functionname)(functionparamz); 
Reply With Quote
  #11  
Old 11-20-2005, 05:27 AM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
Not right now although you can just pass as many parameters as you want, it is only complaining if there are not enough parameters
Reply With Quote
  #12  
Old 11-20-2005, 05:28 AM
Yen Yen is offline
Banned
Yen's Avatar
Join Date: Oct 2005
Location: Nova Scotia, Canada
Posts: 1,085
Yen is an unknown quantity at this point
Send a message via AIM to Yen Send a message via MSN to Yen
Thanks a lot!
If Stefan weren't old, I'd marry him. Definately.
Reply With Quote
  #13  
Old 11-22-2005, 04:36 AM
jake13jake jake13jake is offline
Former Classic Staff
jake13jake's Avatar
Join Date: Dec 2002
Location: Northwest Vermont
Posts: 1,452
jake13jake will become famous soon enough
overloaded functions would be a handy thing.
Reply With Quote
  #14  
Old 11-22-2005, 04:45 AM
napo_p2p napo_p2p is offline
oh snaps
napo_p2p's Avatar
Join Date: Sep 2003
Location: Pismo Beach, California
Posts: 2,118
napo_p2p has a spectacular aura aboutnapo_p2p has a spectacular aura about
Send a message via AIM to napo_p2p Send a message via MSN to napo_p2p
Spiffy.
__________________
Scito hoc super omnia.
Haec vita est tua una sola.
Dum vita superest, utere maxime quoque puncto, momento, et hora quae habes.
Tempus neminem non manet.
Noli manere tempus.
Carpe Diem

Seize the Day.
Reply With Quote
  #15  
Old 11-22-2005, 07:14 AM
jake13jake jake13jake is offline
Former Classic Staff
jake13jake's Avatar
Join Date: Dec 2002
Location: Northwest Vermont
Posts: 1,452
jake13jake will become famous soon enough
Quote:
Originally Posted by Stefan
Such stuff should only be done by experienced scripters though, since it can be quite insecure
hmmm... What makes it insecure?
Reply With Quote
  #16  
Old 11-22-2005, 10:04 AM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by jake13jake
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.
__________________
Skyld
Reply With Quote
  #17  
Old 11-22-2005, 09:00 PM
jake13jake jake13jake is offline
Former Classic Staff
jake13jake's Avatar
Join Date: Dec 2002
Location: Northwest Vermont
Posts: 1,452
jake13jake will become famous soon enough
Quote:
Originally Posted by Skyld
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
Reply With Quote
  #18  
Old 11-22-2005, 09:18 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by jake13jake
a good thing I wouldn't use it that way
I think we agree that it's a good thing if nobody uses it that way.
__________________
Skyld
Reply With Quote
  #19  
Old 11-22-2005, 09:33 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
Quote:
Originally Posted by jake13jake
overloaded functions would be a handy thing.
You can overload already, or what do you mean exactly ?
Reply With Quote
  #20  
Old 11-22-2005, 10:46 PM
jake13jake jake13jake is offline
Former Classic Staff
jake13jake's Avatar
Join Date: Dec 2002
Location: Northwest Vermont
Posts: 1,452
jake13jake will become famous soon enough
Quote:
Originally Posted by Stefan
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):
NPC Code:

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() {}
Reply With Quote
  #21  
Old 11-23-2005, 12:23 AM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
You can still do stuff like this though:
PHP Code:
function test() {
  switch (
params.size()) {
...
  }

Reply With Quote
  #22  
Old 11-23-2005, 03:02 AM
jake13jake jake13jake is offline
Former Classic Staff
jake13jake's Avatar
Join Date: Dec 2002
Location: Northwest Vermont
Posts: 1,452
jake13jake will become famous soon enough
Quote:
Originally Posted by Stefan
You can still do stuff like this though:
PHP Code:
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?
Reply With Quote
  #23  
Old 11-23-2005, 03:03 AM
Yen Yen is offline
Banned
Yen's Avatar
Join Date: Oct 2005
Location: Nova Scotia, Canada
Posts: 1,085
Yen is an unknown quantity at this point
Send a message via AIM to Yen Send a message via MSN to Yen
Yes.
Reply With Quote
  #24  
Old 11-23-2005, 03:35 AM
jake13jake jake13jake is offline
Former Classic Staff
jake13jake's Avatar
Join Date: Dec 2002
Location: Northwest Vermont
Posts: 1,452
jake13jake will become famous soon enough
Quote:
Originally Posted by Yen
Yes.
Knowing this is very useful, thank you.
Reply With Quote
  #25  
Old 11-23-2005, 04:26 PM
ApothiX ApothiX is offline
Okiesmokie
Join Date: May 2004
Posts: 1,447
ApothiX is on a distinguished road
Quote:
Originally Posted by jake13jake
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.
__________________


[06:24:19] * Parts: Skyld (i=silent@unaffiliated/skyld) ("Perhaps Okiesmokie did not realise that I like the boys. ")
Reply With Quote
  #26  
Old 11-23-2005, 06:16 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by jake13jake
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?
__________________
Skyld
Reply With Quote
  #27  
Old 11-23-2005, 06:29 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
Quote:
Originally Posted by jake13jake
So if you send a function unnamed parameters they're loaded into the params array?
They are always loaded into params[]
Quote:
Originally Posted by jake13jake
Is it possible to call event handlers when the event hasn't occured?
Yes, like a function call.
Quote:
Originally Posted by jake13jake
Also, would setTimer(0) immediately call onTimeOut?
No, it would unset the timeout.
Reply With Quote
  #28  
Old 11-23-2005, 09:32 PM
Ajira Ajira is offline
Poont.
Join Date: Oct 2004
Location: NY, USA
Posts: 477
Ajira is on a distinguished road
Quote:
Originally Posted by jake13jake
Also, would setTimer(0) immediately call onTimeOut?
Just do 'onTimeOut();' in your script.

There goes my 1 post per day.
__________________
Liek omigosh.

Reply With Quote
  #29  
Old 11-26-2005, 07:41 AM
jake13jake jake13jake is offline
Former Classic Staff
jake13jake's Avatar
Join Date: Dec 2002
Location: Northwest Vermont
Posts: 1,452
jake13jake will become famous soon enough
Hmm, is there anyway to load an operator from a string?
like

temp.calc (+)= tokens[i];
Reply With Quote
  #30  
Old 11-26-2005, 05:36 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
No I don't think
Reply With Quote
  #31  
Old 11-26-2005, 05:49 PM
ApothiX ApothiX is offline
Okiesmokie
Join Date: May 2004
Posts: 1,447
ApothiX is on a distinguished road
PHP Code:
function doOperator(var1operatorvar2) {
  switch(
operator) {
  case 
"+":
    return 
var1 var2;
    break;

  
// etc..
  
}

__________________


[06:24:19] * Parts: Skyld (i=silent@unaffiliated/skyld) ("Perhaps Okiesmokie did not realise that I like the boys. ")
Reply With Quote
  #32  
Old 11-26-2005, 08:21 PM
Fry Fry is offline
Registered User
Fry's Avatar
Join Date: Sep 2001
Location: Germany
Posts: 384
Fry has a spectacular aura about
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) 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.
__________________
Graal Statistics

Top 3 servers at the moment (players):


Reply With Quote
  #33  
Old 11-27-2005, 12:23 AM
jake13jake jake13jake is offline
Former Classic Staff
jake13jake's Avatar
Join Date: Dec 2002
Location: Northwest Vermont
Posts: 1,452
jake13jake will become famous soon enough
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.
Reply With Quote
  #34  
Old 11-28-2005, 12:45 AM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by jake13jake
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.
__________________
Skyld
Reply With Quote
  #35  
Old 11-28-2005, 03:56 AM
jake13jake jake13jake is offline
Former Classic Staff
jake13jake's Avatar
Join Date: Dec 2002
Location: Northwest Vermont
Posts: 1,452
jake13jake will become famous soon enough
Quote:
Originally Posted by Skyld
Put custom functions in a class or so.
Just wondering, where is this coming from?
Reply With Quote
  #36  
Old 11-29-2005, 01:30 AM
ApothiX ApothiX is offline
Okiesmokie
Join Date: May 2004
Posts: 1,447
ApothiX is on a distinguished road
Quote:
Originally Posted by jake13jake
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
__________________


[06:24:19] * Parts: Skyld (i=silent@unaffiliated/skyld) ("Perhaps Okiesmokie did not realise that I like the boys. ")
Reply With Quote
  #37  
Old 11-29-2005, 01:34 AM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by jake13jake
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.
__________________
Skyld
Reply With Quote
  #38  
Old 11-29-2005, 10:00 AM
jake13jake jake13jake is offline
Former Classic Staff
jake13jake's Avatar
Join Date: Dec 2002
Location: Northwest Vermont
Posts: 1,452
jake13jake will become famous soon enough
Quote:
Originally Posted by ApothiX
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.
Reply With Quote
Reply


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:20 PM.


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