Author Topic: Can constants have more than one value?  (Read 438 times)

Legacy_lordofworms

  • Sr. Member
  • ****
  • Posts: 422
  • Karma: +0/-0
Can constants have more than one value?
« on: January 02, 2012, 03:51:05 pm »


               instead of
const string BONES = "wrm_bones";

like so:

const string BONES = "wrm_bones" || "wrm_skull" || "wrm_femur" 'Posted

?

it doesnt compile and I couldnt find any info on whether its possible at all?

Thanks!!
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Can constants have more than one value?
« Reply #1 on: January 02, 2012, 04:16:52 pm »


               I've never been able to get this to work either so my assumption is: no, they can only have one value.

There are probably other ways to achieve what you want.
It looks like you are trying to associate a list of resrefs with a category called BONES. I wonder if you'd want to do that with a local int on objects that represents a bitwise mask identifying which category the object belongs to. Then when checking to see "type" you simply check for that bit. BONES would be category 1.

Or you could create a 2DA with the first column being resref, and then a list of columns each being a category. Then a script that checks category versus resref.

---
Or perhaps one of the database gurus on this forum will know of a much better way to create a "NAMESPACE" in NWScript or better yet using MySQL and NWNX.
               
               

               


                     Modifié par henesua, 02 janvier 2012 - 04:18 .
                     
                  


            

Legacy_lordofworms

  • Sr. Member
  • ****
  • Posts: 422
  • Karma: +0/-0
Can constants have more than one value?
« Reply #2 on: January 02, 2012, 04:31:46 pm »


               well the local int variable thing I thought of..
the problem is I want to use components that are default NWN items, as well as my own custom items that don't have these variables set...and I am waaaaay to into my PW to go about adding new variables to everything in game (including items players may already have in possession)

2DA sounds interesting and possible, except I havent yet made the foray into new 2DA creation...so that will prob take me longer than I have time for...

I do use NWNX so hmmm, if there is something like that within the funcs that be great...

its not the biggest dilemna in the world if I cannot assign more than one value to a constant, but hey I love a challenge!!
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Can constants have more than one value?
« Reply #3 on: January 02, 2012, 04:52:43 pm »


               If this is a "material" thing perhaps you can add a material property to all items in game.
I believe you can add item properties to items in inventory when a player enters.
               
               

               
            

Legacy_wyldhunt1

  • Sr. Member
  • ****
  • Posts: 443
  • Karma: +0/-0
Can constants have more than one value?
« Reply #4 on: January 02, 2012, 05:04:08 pm »


                The closest I know of to your example is a struct.

struct BONES
{
    string wrm_bones = "mybones";
    string wrm_skull = "otherbones"; 
   string wrm_femur;
};

Then, call them with
BONES.wrm_bones

Not exactly what you wanted, but that's as close as I know how to get to that.
               
               

               


                     Modifié par wyldhunt1, 02 janvier 2012 - 05:04 .
                     
                  


            

Legacy_wyldhunt1

  • Sr. Member
  • ****
  • Posts: 443
  • Karma: +0/-0
Can constants have more than one value?
« Reply #5 on: January 02, 2012, 05:07:06 pm »


               Or, you could make an array of strings and parse the array for a match.


This is from the Legacy NWN system that we use:
http://pastebin.com/NNWzQh37

Leg_AppendToLocalStringArray(GetModule(), "BONES", "wrm_bones", "wrm_skull", "wrm_femur" );
if (Leg_FindInLocalStringArray("BONES", "wrm_bones", GetModule()))
{
   //Do Stuff
}


               
               

               


                     Modifié par wyldhunt1, 02 janvier 2012 - 05:38 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Can constants have more than one value?
« Reply #6 on: January 02, 2012, 06:17:37 pm »


               

lordofworms wrote...

instead of
const string BONES = "wrm_bones";

like so:

const string BONES = "wrm_bones" || "wrm_skull" || "wrm_femur" 'Posted

?

it doesnt compile and I couldnt find any info on whether its possible at all?

Thanks!!


Ahh,  I already typed this one just to have it lost when the forums logged me out.  So I am going to cover it a little faster this time. 

I see serveral misconceptions in your statement.   First is the Logical OR Operator.  ( || ).
The Logical OR Operator  Eveulates two Numbers as being either TRUE or FALSE  to a TRUE or FALSE result per the logical truth table.  It has no Idea on how to operate on strings.  

If it could evelulate out it would set the result to the BONES lable equal to the single stinng of the evulation.   Since the || operator eveulates out to a number again you have a type missmatch trying to place it into a string data type.  

constants: 
Constants are a Compile Time Varaiable.  They are never placed into the compiled code as a Labled Constant.   Anyplace the constant is used in the script, the compiler replaces it with the value of the constant before the script is converted into compiled code. 

The compiler is stupid; 
The compiler does not know how to eveulate expressions.  Where a statment like: 
const int MyCONST = 20/5; 
would be legal in most languages with 20/5 eveulating out to 4 and having all uses of MYCONST being replaced by 4 in the script at compile time.    The nwscript compiler simply does not know how to divide.    Or how to eveulate any of the Mathmatical or logical  Operations for that matter. 

I hope this shorter post of the original is helpfull,  If not feel free to ask questions.   

 As far as what you are trying to do.   It looks like just placing everything into one string would give you something to check against.   Also a constant is probably not the best choice in this case,  the reason is that the constant when used is replaced with the string.   If you used a varaiable instead of the constant, everything would be checked against the one string var instead of reproducing that constant for every use in the script.

int GetIsBones(string sTag)
{
   //note: that I left a space at the begining and end of the string.
   string BONES = " wrm_bones  wrm_skull  wrm_femur " ;
   sTag = " "+sTag+" ";
   return FindSubString(BONES,sTag) != -1;
 }
               
               

               
            

Legacy_lordofworms

  • Sr. Member
  • ****
  • Posts: 422
  • Karma: +0/-0
Can constants have more than one value?
« Reply #7 on: January 03, 2012, 07:48:16 pm »


               well as always lightfoot gives me loads of info! I appreciate that, it helps me understand whats happening so I can creat my own scripts with better knowledge.

so based on this I re-wrote how I look for ingredients and dropped the constants.
but this is good info nonetheless! thanks everyone!