Hi there! You are currently browsing as a guest. Why not create an account? Then you get less ads, can thank creators, post feedback, keep a list of your favourites, and more!
Instructor
Original Poster
#1 Old 29th Jan 2017 at 12:15 PM Last edited by whoward69 : 16th May 2017 at 12:26 PM.
Default Best way to initialise object attributes?
I have a custom object, with 16 attributes (I've increased the num attributes value in the OBJD record) which need to be initialised to various default values when instances of the object are created.

I've added a BCON with all the default values, and edited the "Function - Init" BHAV to initialise each attribute with its corresponding BCON value (16 lines of My Attribute X := BCON:X - there's probably a better way to do that!).

I know the code is running for each instance creation as I added a notification after the 16 lines, and these appear correctly.

For debugging, the object has a "Show Values" menu option, which just displays the attribute values in a notification.

Now, the attribute values displayed are NOT those set in the BCON. What's more, if I create two instances of the object (for different Sims), the values displayed are also different (so it's not as if I'm displaying the values incorrectly, as then I'd expect consistent wrong values). Also, the values displayed are different depending if I create the objects in the order "for Sim A then Sim B" or "for Sim B then Sim A" - so it appears that the values in the attributes for the objects are just whatever happened to be in memory when the objects were instantiated.

I'm obviously missing something here. So what's the correct/best way to initialise object attributes? Apparently not in the "Function - Init" BHAV, so after the "Create Object" (or whatever the global is) called?

Edit: So now I'm totally confused. Moved the init lines into their own BHAV (Sub - Set Defaults) and called that from Function - Init, also added a CT - Set Defaults BHAV to call it and called that just after creating the object. And added the call to display the values at the end of Sub - Set Defaults

The bottom notification is what the object thinks the values are at the end of the Function - Init BHAV (which are correct), the middle notification is what the object thinks the values are in the CT - Set Defaults BHAV (which are also correct), the top notification is what the menu option on the object (which calls the exact same BHAV) thinks the values are.

So what's changing the attribute values between Function - Init and the object being usable?

Edit 2: When I first set the attributes, I forgot to change "num attributes", and the error was about attribute 0x0A being out of range. Are the first 10 attributes reserved for something and unusable? [S]Perhaps I should just set "num attributes" to 0x20 and add 0x10 to all my attribute index values???[S]

Edit 3: Well that didn't work, everything is now just 0!

TIA

W

Just call me William, definitely not Who-Ward
Advertisement
Scholar
#2 Old 31st Jan 2017 at 2:38 PM
The first eight attributes (0 to 7) are given to all items without being 'declared' in the Object Data (OBJD) , any more than that must be declared. All attributes begin with a zero value until changed, if you need a non zero value then setting the start value in the init behave is correct. Make sure the Object Functions (OBJF) is actually using your Init behave not a semi global. I'm confident you'll find the attributes on your item are being altered elsewhere, after it has begun to run.
Instructor
Original Poster
#3 Old 31st Jan 2017 at 7:23 PM
Long story short. PICNIC. Can't use the same BHAV to display the values from Function - Init as from a TTAB entry, as the former uses My Attributes whereas the latter uses Stack Object Attributes.

Just call me William, definitely not Who-Ward
Back to top