Author Topic: How would I go about.........  (Read 332 times)

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
How would I go about.........
« on: October 01, 2010, 02:25:02 pm »


               So I created a struct, only took like 4 years for the light bulb to brighten, how can I store that struct to an item, or a database? I devised a method for converting the struct to a string, and storing that as a local to a pc item, but it is clunky.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
How would I go about.........
« Reply #1 on: October 01, 2010, 03:57:07 pm »


               unfortiantly that is about the only way to go about it.   You would however be better off storing it as local ints if you could,  The only real use I see for structures in NWN is passing mass arguments to a function,  Since there is no real good way to store a user defined structure they are really only usefull in a locals in a script.
               
               

               
            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
How would I go about.........
« Reply #2 on: October 01, 2010, 04:30:22 pm »


               Appreciate it Lightfoot8, I just been messing round with em trying to figure out structs. With my convert to string method you have to know what number of variables and types so the function would not work for any and all structs.



Is there a way to look into a struct and determine the number of and types of variables within?
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
How would I go about.........
« Reply #3 on: October 01, 2010, 10:34:54 pm »


               No there is not.  unfortantly the structure does not even contain that information.    In order to explain that lets look at a Vector.
a Vector is defined in the compiler to be something like.
struct  vector
{
    float x;
    float y;
    float  z;
}; 

now say you declair a vector and give it a name of vStart.

vector vStart; 

when the compiler compiles that line it is going to allocate 3 dwords of storage for the var vStart. (floats being 4 bytes long or a dword)  the Var name 'vStart' gets replaced with a memory location(another Dword) that that holds the adrress of the first byte for the 12 bytes allocated for the data. 

Now any time you access one of the data types within the structure.  

vStart.y = 5.0;  

Here since y is the second dword in the structure the compiler replaces this with something like. 

 
mov type dword  [vStart + 4] ,  5.0


That line basicly reads  move  5.0  as a Dword into the memory location of the number stored at location vStart+4.  

Keep in mind that vStart is also replaced with a number of where it is located at.  

So to get  move something to vStart.x it would look like.  

 mov type dword  [vStart + 0] ,  5.0

I guess i should get to my point in all this.  

The only place the names(lables) or data types are knows within the 12 bytes that make up the data for the vector, is through the delclarition of the structure itself.  

Here is a real Kicker.   Your string you are useing to pack your data from the structure into.  Is in itself a structure.   All you need to know is the offset of the data you want in the string and you can pull it right out.  But the string itself, Is just a string and can not give you the information you need to look for it.    

I hope that helps a little. 


As a side note here in  structures are not often used just by themselves.   They are often grouped with methods and procedures that go along with the.  this is so that all mulipation of the data in the structure can be controled.    


note: in order to tell what kind of data is in a structure you could always give all of your structures a type field and pack that field as the first entry into your string then match it with the correct unpack procedure(function) to unpack it. 

For an example of where bioware used the a struct declairation you can look in X0_I0_STRINGLIB