![]() |
Creating a variable from a string?
Is it possible to create a new variable from a string?
For example, if I have: NPC Code: 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' |
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: 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? |
|
makevar("varname") = "blah"; works aswell, IIRC.
|
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. |
Quote:
|
Quote:
|
Could have probably named the thread better. >_<
Quote:
The function you're looking for is called eval(), which would run whatever you passed to it: NPC Code: ... 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();"); |
I guess you can dynamically call functions, but function name and parameters must be stored independently, not in one single string. Example:
PHP Code:
PHP Code:
|
: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.. PHP Code:
|
Not right now although you can just pass as many parameters as you want, it is only complaining if there are not enough parameters
|
:D Thanks a lot!
If Stefan weren't old, I'd marry him. Definately. |
overloaded functions would be a handy thing.
|
Spiffy.
|
Quote:
|
Quote:
|
Quote:
|
Quote:
|
Quote:
|
Quote:
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: 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() {} |
You can still do stuff like this though:
PHP Code:
|
Quote:
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? |
Yes.
|
Quote:
|
Quote:
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. |
Quote:
Seperate them on the serverside, you say? Well, what's wrong with the current //#CLIENTSIDE method? |
Quote:
Quote:
Quote:
|
Quote:
There goes my 1 post per day. ^^ |
Hmm, is there anyway to load an operator from a string?
like temp.calc (+)= tokens[i]; |
No I don't think
|
PHP Code:
|
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. |
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.
|
Quote:
|
Quote:
|
Quote:
|
Quote:
Write a function and save it into a class, and join() it, if you're that worried about clutter in your script. |
Quote:
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. |
| All times are GMT +2. The time now is 12:07 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.