Author Topic: Referencing datablocks by number...  (Read 1547 times)

Looking over my macro saves, I see that each brick used in the save is stored a number in an array.

For example, a 1x1 brick may show as "202" (just an example) in the array, instead of showing up as the datablock name.

I have two questions.

First, wouldn't the number change if you add more datablocks later that are higher in the list alphabetically?

If so, then the save wouldn't be good for much.

If not (second question), how can I find out which numbers correspond to which datablocks without going through a tedious test and check method?

Well, it appears the %obj.getDatablock() returns the number, and %obj.dataBlock is the actual name.  Any confirmation as to whether future datablocks (e.g. those that come before the one in question, alphabetically) will change the current numbers?

202.getName(); would get the name of the datablock - brick1x1data, or similar.

nametoID("brick1x1data");, in this example, would return 202.

The macro saver saving numbers rather than names is what caused problems with loading macros between 1.02 and 1.03.

Also: Datablock numbers are done on when it is defined (datablock [whatever](name)), not on its name. Saving numbers for addons won't work well since if you install a new addon which is executed before yours, the numbers will change and saved items (I assume that's what you're doing) will change to a completely different item.

Actually, I'm not using the numbers at all.  I just wanted to make sure that other people knew that datablock numbers aren't reliable.  I'm surprised Ephi allowed them to be used for his Macro Saver.

Badspot

  • Administrator
The numbers can in fact change.  Saving by name is better. 

Be aware that %obj.datablock or %obj.getDataBlock().getName() will give you the datablock name, but it only works for the host
To get it to work client-side, you need to use the .uiname property of the datablocks (this is the name that is displayed in the brick selection dialog and in the hud)

So, to the name from a brick on the client you do
Code: [Select]
%brickName = %brick.getDatablock().uiName;

Then you save that uiname however you want.

Converting from the uiName back to a datablock id is a little trickier, but there is a built in table.
Code: [Select]
//create the table if it hasn't been created yet
if($UiNameTableCreated == false)
   createUiNameTable();

//lookup the uiname in the table
%brickData = $uiNameTable[%brickName];

Excellent, thank you.