Author Topic: String to Variable  (Read 406 times)

Legacy_OffGraphic

  • Newbie
  • *
  • Posts: 20
  • Karma: +0/-0
String to Variable
« on: January 31, 2012, 05:02:22 pm »


               So, is there a way to check if a variable exists by using a string as its name?
Such as, GetVariable("var_"+num+"_tag") for examble.
I would really have use for such a thing. If no such function exists, then I will have
to store all the creature specific data in the description instead of a include file.
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
String to Variable
« Reply #1 on: January 31, 2012, 05:56:59 pm »


               If you use a variable set on an object, thats how it works.
If you use a variable set in a database, that is also how it works.

Look at GetLocalInt, GetLocalString, GetLocalObject for variables set on an object. By "object" I mean a game object like a creature, door, placeable, waypoint etc...
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
String to Variable
« Reply #2 on: January 31, 2012, 06:17:05 pm »


               Put differently, yes, you can concatenate strings to form variable names in any way you like. This is often taken advantage of to create pseudoarrays, since NWN has no native array functionality. If you're more specific about what you want to do, we could be more helpful.

Funky
               
               

               
            

Legacy_OffGraphic

  • Newbie
  • *
  • Posts: 20
  • Karma: +0/-0
String to Variable
« Reply #3 on: January 31, 2012, 06:25:30 pm »


               Basically, what I am trying to do is a loop through user-set variables. But since Arrays do not exist,
I named them in a way such as: var_000_main var_000_mod var_000_value and such.
It would look something like this: var_index_main var_index_mod var_index_value, whilst index is an
additional value from another variable. I have thus far not found a way to do it in a way like that.

However, if there is no such thing, I can somehow create a regexp system to fetch the data *efficiently*
from the object description. I guess it could be useful in a way, too, but I am not sure whether or not
that concept would be wise in a roleplay module.
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
String to Variable
« Reply #4 on: January 31, 2012, 10:10:27 pm »


               What is wrong with storing these as local variables on an object?
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
String to Variable
« Reply #5 on: February 01, 2012, 12:01:04 am »


               From your posts I am unclear what locals you are talking about. I am leaning the way henesua is, and think you are talking about variables that are local in the script. If this is the case you can not do what you are asking. The variable names are compile time lables only. Simply meaning that once your script is compiled the variable name no longer exsists. said another way, once your script is running the variables in the script no longer have names, Therefore there is nothing to loop through by the lable given to the variable.

If you wanted to use variables local to an object, They would keep there lables, that are nothing more then strings, that you can build any way you see fit.
               
               

               


                     Modifié par Lightfoot8, 01 février 2012 - 12:01 .
                     
                  


            

Legacy_OffGraphic

  • Newbie
  • *
  • Posts: 20
  • Karma: +0/-0
String to Variable
« Reply #6 on: February 01, 2012, 11:16:29 am »


               Well, what I tried to do cannot be done without arrays.
So, I came up with a script to fetch the data from object
description, possibly remove that data before a player gets
to see it and then apply it to the objects according to
their stats in description and a calculated medium of players
in the certain area.

The object stored variables cannot be used, due to the fact
nobody really wants to write the extra SetLocalString() for each
stat that is going to be specified to the object. It would look
too messy in the end.

Current script be below.

string gs_Balance_GetSTR (string this) {
  string STR      = "";
  string DEX      = "";
  string CON      = "";
  string WIS      = "";
  string INT      = "";
  string CHA      = "";
  string bonus    = "";
  string levels   = "";
  int    slength  = 3;
  int    pos      = 0;
  int    nlength  = 3;
//VERIFY STRENGTH
  if (FindSubString(this, "STR:", pos) != -1) {
    pos= FindSubString(this, "STR:");
    STR = GetSubString(this, pos + slength, nlength);
//  VERIFY DEXTERITY
    if (FindSubString(this, "DEX:", pos) != -1) {
      pos = FindSubString(this, "DEX:");
      DEX = GetSubString(this, pos + slength, nlength);
//    VERIFY CONSTITUTION
      if (FindSubString(this, "CON:", pos) != -1) {
        pos = FindSubString(this, "CON:");
        CON = GetSubString(this, pos + slength, nlength);
//      VERIFY WISDOM
        if (FindSubString(this, "WIS:", pos) != -1) {
          pos = FindSubString(this, "WIS:");
          WIS = GetSubString(this, pos + slength, nlength);
//        VERIFY INTELLIGENCE
          if (FindSubString(this, "INT:", pos) != -1) {
            pos = FindSubString(this, "INT:");
            INT = GetSubString(this, pos + slength, nlength);
//          VERIFY CHARISMA
            if (FindSubString(this, "CHA:", pos) != -1) {
              pos = FindSubString(this, "CHA:");
              CHA = GetSubString(this, pos + slength, nlength);
            }
//          END
          }
//        END
        }
//      END
      }
//    END
    }
//  END
  }
//END
  return "";
}

               
               

               


                     Modifié par OffGraphic, 01 février 2012 - 11:33 .
                     
                  


            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
String to Variable
« Reply #7 on: February 01, 2012, 12:39:17 pm »


               (1) Instead of a description which everyone can read in game, you could use a local string in exactly the same way that you describe above.
(2) Why would you not use a separate Local Int for each stat? It is more efficient to do so rather than convert from string to int after parsing a string.

In other words, you seem to be going out of your way to make this more complicated and inefficient than it needs to be. Also you don't have to write an extra set local string for each stat.

object oObject = GetObjectByTag("myDataObject");
string STRING_IDENTIFIER = "var_"+"_tag";
int nValue = num;

SetLocalInt(oObject, STRING_IDENTIFIER, nValue);
               
               

               
            

Legacy_OffGraphic

  • Newbie
  • *
  • Posts: 20
  • Karma: +0/-0
String to Variable
« Reply #8 on: February 01, 2012, 02:20:59 pm »


               Henesua, if I were to do it in a way such as yours, I would need to write a huge wall of text.
The data values for each tag are unique, and there can be easily over 1000 unique tags to balance
with, and all of those would have extra unique data such as STR, STR BONUS, STR PERIODIC_LEVEL. See the issue that gets in there? Multiple declarations of ...

int nValue = 0;
string STRING_IDENTIFIER = "something"
SetLocalInt(OBJECT_SELF, STRING_IDENTIFIER, nValue)

and that would just make it too messy.
Sure, I can remove nValue and STRING_IDENTIFIER from locals, but still it would get messy.
               
               

               


                     Modifié par OffGraphic, 01 février 2012 - 02:38 .
                     
                  


            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
String to Variable
« Reply #9 on: February 01, 2012, 03:24:54 pm »


               Did you take what I wrote literally? It wasn't meant as such. It was a pattern to follow. Look at it again in that context.

In anycase, there is no reason to be using the description to store a string. You can use a local string in exactly the same way as the description.

The advantage however in using a series of local ints is that you don't need to convert each snippet of string to an integer. And you don't need to separate each snippet from the larger string.

Give each INT a unique string identifier that follows the pattern you need in your array. The string identifier can be created out of variable substrings. Its easy to do.
               
               

               
            

Legacy_OffGraphic

  • Newbie
  • *
  • Posts: 20
  • Karma: +0/-0
String to Variable
« Reply #10 on: February 01, 2012, 03:29:41 pm »


               It is indeed easy to do. But I cannot come up with a concept that would work efficiently enough for the
balancing system.
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
String to Variable
« Reply #11 on: February 01, 2012, 06:17:20 pm »


               I understand. Handling data is a tricky business to get just right.

If you spell out exactly what you need to do with a few more script examples, I am sure many of the people here will be happy to show you different ways to solve the problem.