Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

llVarName(string str)

Bosozoku Kato
insurrectionist midget
Join date: 16 Jun 2003
Posts: 452
08-02-2004 18:43
I have a suggestion for an LSL function.

llVarName(string str)

Whereby you can pass in a string and it will return a variable's NAME. LSL would use the returned name to either create a variable by that name (if declaring a var), or call the var if it exists (else returns an error if the var was called but it doesn't exist).

What this allows is dynamic variable calls. For example, say I have a game that has 3 players. I keep scores and player keys for each player. I name my vars with a numeric "tag" for ease of scripting (assuming llVarName() existed).
CODE

// player keys
key g_sPlayer1;
key g_sPlayer2;
key g_sPlayer3;
// player scores
integer g_nPlayer1;
integer g_nPlayer2;
integer g_nPlayer3;
// player count
integer g_nTotalPlayers;

// Now I can easily make functions/routines such as:
AddPlayer(integer i, key pid)
{
llVarName("g_nPlayer"+(string)i) = pid; // if "i" was "2" then this statement is evaluated as: "g_nPlayer2 = pid;"
}

default
{
touch_start(integer num)
{
if (g_nTotalPlayers < 3)
{
g_nTotalPlayers++;
AddPlayer(g_nTotalPlayers, llDetectedKey(0));
}
}
}


Would also be quite handy for creating multiple dynamically created local vars:
CODE

integer i = 0;
while (i >= 0 && i < 10)
{
i++;
string llVarName("NewStringNumber"+(string)i) = "Yadda Yadda";
}
// gives you 10 string vars "NewStringNumber1" to "NewStringNumber10"


Anywho, my twisted idea for today!
--Boso

Edit:
Should return a null string "" if the var doesn't exist
CODE

if (llVarName("MyVar"+(string)i+"hrm") != "")
{
// var exists
}
else
{
// var doesn't exist
}
Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
08-02-2004 19:20
I know that was a bit of a trivial example you posted, but dont lists provide somewhat the same kind of functionality?
==Chris
Grim Lupis
Dark Wolf
Join date: 11 Jul 2003
Posts: 762
08-02-2004 19:27
What Bos is asking for is a dictionary class.

Which, as Christopher has indicated, can be fairly easily simulated by using a list with a stride of 2, or paired lists.
_____________________
Grim

"God only made a few perfect heads, the rest of them he put hair on." -- Unknown
Carnildo Greenacre
Flight Engineer
Join date: 15 Nov 2003
Posts: 1,044
08-03-2004 00:24
Sounds like what you want is pointers or symbolic references.
_____________________
perl -le '$_ = 1; (1 x $_) !~ /^(11+)\1+$/ && print while $_++;'
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
08-03-2004 00:54
pointers would be very nice.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Hiro Pendragon
bye bye f0rums!
Join date: 22 Jan 2004
Posts: 5,905
08-03-2004 01:43
Strife, indeed. And I think you make the Linden Sysadmins quake in fear mentioning them. Talk about opening a huge hole for people to crash sims... ;)
Eggy Lippmann
Wiktator
Join date: 1 May 2003
Posts: 7,939
08-03-2004 04:48
I dont even understand what boso is asking for, but I would sure love to have the standard eval(string code) function call.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
08-03-2004 23:19
they wouldn't be real pointers in the sense of the word. They would be restricted to the memory space of the script. But it would be cool to use a pointer to reverse engineer the scripting virtual machine. Oh i can't wait :D. I think it would be fun to make a polymorphic script that could change it's own code by manipulating the copy of the code that is stored in the 16k memory space.

:D :D :D
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Carnildo Greenacre
Flight Engineer
Join date: 15 Nov 2003
Posts: 1,044
08-04-2004 00:38
From: someone
Originally posted by Strife Onizuka
they wouldn't be real pointers in the sense of the word. They would be restricted to the memory space of the script.

Only if the implementation was bulletproof. I once hacked a stack machine by finding one of the stack manipulation ops that wasn't protected properly, and using very large offsets to access data outside the virtual machine.

From: someone
But it would be cool to use a pointer to reverse engineer the scripting virtual machine. Oh i can't wait :D. I think it would be fun to make a polymorphic script that could change it's own code by manipulating the copy of the code that is stored in the 16k memory space.


And this is a big part of the reason it won't be implemented: self-modifying code is a bitch to debug, and even harder to protect the virtual machine against.
_____________________
perl -le '$_ = 1; (1 x $_) !~ /^(11+)\1+$/ && print while $_++;'
Hiro Pendragon
bye bye f0rums!
Join date: 22 Jan 2004
Posts: 5,905
08-04-2004 01:04
From: someone
Originally posted by Carnildo Greenacre
Only if the implementation was bulletproof. I once hacked a stack machine by finding one of the stack manipulation ops that wasn't protected properly, and using very large offsets to access data outside the virtual machine.

And this is a big part of the reason it won't be implemented: self-modifying code is a bitch to debug, and even harder to protect the virtual machine against.


Gee, and the fact that self-modifying code that can replicate is ... oh, a virus? LOL

I think Strife was being a lil' sarcastic ;)

Okay, I think we should stop hijacking this thread and let it die. :)

-Hiro Pendragon