How to get the Datablock from the Uiname

Author Topic: How to get the Datablock from the Uiname  (Read 1113 times)

Alrighty guys, here's my question.


How would I go about getting the datablock of an item from the Uiname of the item.
Eg:

Code: [Select]
%thingamabob = "Gun";
%datablock = %thingamabob.getDatablock();

Now of course that doesn't work, but what would make it work?

The ui name isn't an object. Why are you needing to do this anyways?

I'm sure there is a better way to do what you are trying to do, then the way you're currently trying to do it.

The ui name isn't an object. Why are you needing to do this anyways?

I'm sure there is a better way to do what you are trying to do, then the way you're currently trying to do it.

That code was just to explain what I needed it to do.
Not what I was trying to actually do.

I need to be able to take a string of the Uiname of a datablock and retrieve the datablock itself using just that uiname string

It's a terrible plan, but the only way I can think of follows:
Code: [Select]
function getDataBlockFromUIName(%uiName)
{
for(%i = 0; %i < dataBlockGroup.getCount(); %i++)
{
%o = dataBlockGroup.getObject(%i);
if(%o.UIName $= %uiName)
return %o;
}
}

It's a terrible plan, but the only way I can think of follows:
Code: [Select]
function getDataBlockFromUIName(%uiName)
{
for(%i = 0; %i < dataBlockGroup.getCount(); %i++)
{
%o = dataBlockGroup.getObject(%i);
if(%o.UIName $= %uiName)
return %o;
}
}

What do you mean by 'terrible plan'?
This works fine

Because its hard on the engine and could possibly lag with a ton of datablocks. It literally loops through all of the datablocks in the game.
« Last Edit: November 09, 2012, 07:43:33 PM by Plornt »

Because its hard on the engine. It literally loops through all of the datablocks in the game.
Are datablocks grouped in any way to lessen this?
As this is only going to be used for item datablocks, looping through any others is a bit useless, but I'm pretty sure there isn't any grouping system

Alright,
So I've noticed before among the default weapons and items it's true that you can do

Gunitem.getDatablock(); And/or Gunitem.GetID();

And that works,
But does all user created content follow this example, if so I can just add "Item" onto the end of the uiname input and do it this way, however I'm not sure if the first part is true.

No, sometimes people don't put 'item' at all.

No, sometimes people don't put 'item' at all.
forget stuff asdf

Based off what Trinick gave you:
If you're going to be searching for the same uiname a lot, you could save some time by indexing datablocks you've already found:

Code: [Select]
function getDataBlockFromUIName(%uiName)
{
if($DatablockNameArray[%uiName])
return $DatablockNameArray[%uiName];

for(%i = 0; %i < dataBlockGroup.getCount(); %i++)
{
%o = dataBlockGroup.getObject(%i);
if(%o.UIName $= %uiName)
{
$DatablockNameArray[%uiName] = %o;
return %o;
}
}
}

or similarly, after loading you could loop through every datablock and add the uiname and datablock to the array, then (provided you don't load any weapons afterwards, which you should) you won't need to run the loop at all
« Last Edit: November 09, 2012, 08:23:29 PM by Headcrab Zombie »

what are you trying to accomplish?
here in coding help, we're skilled at telling you that you're wrong and you need to do it a different way.

so yeah, tell us what you're trying to do.

what are you trying to accomplish?
here in coding help, we're skilled at telling you that you're wrong and you need to do it a different way.

so yeah, tell us what you're trying to do.
I literally told you, there's not easier explanation.

I need to retrieve a datablock from the uiname of said datablock.

Saving it to a scriptobject worked, and I did it that way, so thanks guys !

He meant to tell us why you needed this, what this mechanism served for in your code that way we could tell you a different way to do it that doesn't involve searching through datablocks.

I literally told you, there's not easier explanation.

I need to retrieve a datablock from the uiname of said datablock.

Saving it to a scriptobject worked, and I did it that way, so thanks guys !

Lol script objects?