Tutorial: Creating a random object in a particular category
This tutorial assumes that you are comfortable using SimPE for BHAV programming. Screenshots are taken from SimPE v0.72.1.33855
Creation of a new object, using the Create New Object Instance primitive [0x002A] allows you to create a specific object, by GUID. However, you must know the GUID ahead of time. What if you want to create a new object of a particular type, for example, any chair, any bed, any car, but just want a random one from those in the catalog, rather than providing a specific GUID. What then?
The Set to Next primitive [0x001F] seems like a good way for finding random things - you can cycle through all sorts of things with this primitive, Pets, jobs, Sims, objects of a particular type, BUT for almost all of the Set to Next operations, the objects being cycled through are already instantiated on the lot (if you look in a .log file you will see them in the Lot Dump).
There is one Set to Next operation that can find things that are not instantiated and that is what this tutorial is about.
We will be looking at how to use the slightly oddly named Set to Next (influence) operation.
As with all uses of the Set to Next primitive, first ensure that the variable that is going to hold the next thing is zeroed out first (otherwise you won't be starting at the beginning). Also make sure you do this outside any loop you might place your Set to Next instruction in, or you will suffer from the "Too many iterations" error. In my case this is a local variable (Local) which I have labelled "Selected Item".
Now, this Set to Next (influence) operation expects Temp to be set to the selection criteria (What's it selecting on?). In this case we want to find all things set to the constant defined in Global BCON 0x0106 for the vehicle category (value 0x0003), so we set this value in Temp.
You can also select on a sub-set of the items Selector Category by setting Temp to a value. If you want all items in the Selector Category, set Temp to -1.
Having set up the expected inputs, we can now call the Set to Next primitive. As is normal for the Set to Next primitive, it will return True if it found something and False if it did not. This is how you know you've come to the end of the set of items when you call Set to Next in a loop.
Upon its return, Set to Next (influence) places the GUID of the item found in Temp and Temp.
If you want to select a random item, you will need to put this into a loop and go round for a random number of iterations. I am assuming that if you are reading this tutorial, you have already enough BHAV programming knowledge to do this, if not, there are other tutorials that will teach you about this. What is it selecting on?
This Set to Next (influence) operation is selecting upon the OBJD's 0x004B 'Selector Category' field. Although you could put any number in here, and set the matching number in Temp before calling Set to Next (influence), in order to avoid clashes with other mods, or indeed with any future Maxis changes, where possible, the values from global BCONs which determine the category value should be used, namely:
BCON 0x0106 Category
BCON 0x016C Category2 (the follow-on when 0x0106 got full)
Maxis in-game uses
This isn't actually used very much in the game by Maxis. You will find that many objects do not even have their Selector Category set, so part of using this with Maxis objects will have to include fixing that as part of any object that needs them. Those that we know of incude:
Accessories - BCON 0x016C:0x19 Value (0x0099) - used by the Accessory Spawner (Group 0x7F461C50), a test object from Maxis
Phone Calls - BCON 0x016C:0x11 value (0x0091) - used by PhoneGlobals 0x204D "Call - Check Type Availability". This particular use also uses the Selector Sub-Category with other values in BCON 0x016C, so we assume not all values in those BCONs are major Selector Categories, some may be Sub-Categories.
Attached is a package which is a modification of the Accessory Spawner (has it's own unique GUID and can be found in Hobbies - Knowledge) which produces a pie-menu of all objects in the catalog which have their Selector Category filled in. Clicking on the pie-menu that is produced will attempt to spawn an instance of the item, which may not always work. The objects main intention was simply to enumerate all the items which were already catgorized with a Selector Category. It's probably also useful for spotting any miscategorized items.
The object itself is Base Game compatible however, the instruction in question is not, so all you can do with this in the Base Game is illustrate exactly the error you will receive attempting to use this instruction in the Base Game.
If you use this object without Pets or a newer than Pets EP, you will be provided with the GUIDs of the objects in each selection criteria - not as useful for working out what they are but at least illustrating that the instruction can be used.
If you use the object with Pets or higher, you will get a nice description of the object.
All Categorized Selectable Object types
I can claim no credit myself for discovering this behaviour. All the credit goes to tunaisafish, Dizzy and Numenor for setting me on the right tracks when I needed to do exactly this. My thanks to them, and I hope I have captured all their notes in this tutorial for future reference.
If there is anything that should be said about this instruction, let me know and I'll add it here.
Excellent work! Thank you for collecting all the info in a nicely illustrated tutorial.
Few notes about the game compatibility (of this tutorial and the attached Accessory Spawner object).
The "Set To Next... Influence" instruction was first introduced with University. If used in a base-game, the Set to Next functions returns the error: "Search type is invalid. Types are 0: all motive, 1:mental motives, 2:physical motives" (which is untrue, anyway, because the base game allows search types not limited to the mentioned three).
The pie menus with 3 or more levels were introduced with UNI, too; in the base game, the menu can only show two levels ("Spawn.../Category no. X"), without further selection over the specific objects.
On the other hand, the variable "$GUIDCatalogNameLocal:X:Y" used in the Spawn object to build the pie menu was first introduced with Pets: with older games, the pie menu explicitly show the variable name ($GUIDCatalogNameLocal:1:2) instead of the actual object name.
The TTAB contained in the test object is version 0x54, i.e. Pets version; as a consequence, the test object crashes games older than Pets (this can be solved downgrading the TTAB to version 0x4F, thus allowing the object to be used in UNI games, though with the pie menu full of "$GUIDCatalogNameLocal:1:2"...).
I've finally started my Journal. Information only, no questions.
Example object updated (as per Numenor's hints) to be usable in any version of the game, although of course it has slightly different behaviour depending on what EPs you have. See notes in main thread.