Game Development Community

How to convert AssertBase to ImageAsset in torque script?(solved!)

by KevinYuen · in Torque 2D Beginner · 03/14/2013 (6:01 am) · 7 replies

my code:

%query = new AssetQuery();
   AssetDatabase.findAssetName( %query, "Crosshair2" );
   for( %index = 0; %index < %query.count; %index++ )
   {
      %asset = getWord( %query, %inde );
      echo( %asset.ImageFile ); // print null?
   }
   %query.delete();

please tell me? how to do? thx

#1
03/14/2013 (6:35 am)
You cannot use

%asset = getWord( %query, %index );

AssetQuery will contain a list of Assets which have been found using AssetDatabase.findAssetName.

If you use echo(%query); it will only give you the ID of the query itself, not the ID of the Assets it contains. That is why getword will not work in this situation.

Note that the code you've posted above uses %inde which should be %index

You need to call %query.getAsset(%index); in order to obtain the AssetID that you need.

for(%index=0; %index < %query.count; %index++)
{      
   %My_asset_ID = %query.getAsset(%i);
}

You can then use the Asset ID however you desire.

For testing purposes, you can add this to your loop

%mySprite = new Sprite();  
   %mySprite.Image = %My_asset_ID;
   
   myScene.add(%mySprite);
#2
03/14/2013 (6:40 am)
I want to make a asset editor, but need not create new sprite when accessing a ImageAsset or AnimationAsset.

#3
03/14/2013 (7:24 am)
It really depends on how you want to go about it, Kevin. There are many, many ways to create an Asset Editor or even an Asset Viewer.

Once you have the Asset ID, obtained from %query.getAsset();
You can then get the actual AssetBase or ImageAsset object from the AssetDatabase.

%My_Asset_ID = %query.getAsset(%i);   
   %My_AssetBase = AssetDatabase.acquireAsset(%My_Asset_ID);

You could then view the information of the Asset (Name, Description, ImageSize, Filename, FilterMode, etc.) with something like

for(%z=0; %z< %My_AssetBase .getFieldCount(); %z++)
{
      echo(%My_AssetBase.getField(%z));
      echo(%My_AssetBase.getFieldValue(%My_AssetBase.getField(%z)));
}

You could also decide to scan the database for each Asset Type and perform actions based on that.

The following code would return all ImageAssets in the AssetDatabase and list their Fields and associated values.

AssetDatabase.findAssetType(%query, ImageAsset);
echo("Found" SPC %query.count SPC "assets");

for(%i=0; %i < %query.count; %i++)
{
   %MyassetID = %query.getAsset(%i);
   
   %MyImageAsset = AssetDatabase.acquireAsset(%MyassetID );
 
 echo("Asset Name :" SPC %MyassetID);
   
for(%z=0; %z< %MyImageAsset.getFieldCount(); %z++)
{
      echo(%MyImageAsset.getField(%z));
      echo(%MyImageAsset.getFieldValue(%MyImageAsset.getField(%z)));
}   

}

All of this info is explained in great detail in the Assets guide :
github.com/GarageGames/Torque2D/wiki/Asset-Manager-Guide
#4
03/14/2013 (7:56 am)
Don't forget to release the asset after you have acquired it using "AssetDatabase.releaseAsset()". It's the most common mistake when using acquire and if you don't release then the asset will be held in memory and never released.

"acquireAsset()" & "releaseAsset()" are there primarily for asset editors.

Also, it's worth looking at using "private" assets for newly created assets. You can generate assets like this:
%image = new ImageAsset();
%image.ImageFile = "^MyModule/assets/foo.png";

// Add as a private asset.
%assetId = AssetDatabase.addPrivateAsset( %image );

These are in-memory assets that can be assigned to stuff and later you can persist it as a public asset i.e. to disk.

You can also load-up a public asset as a private asset using the acquire asset but passing "true" as the second argument.

All this can be found in the Asset Manager documentation and know that this asset system was used by editors in an internal application called 3-Step studio.
#5
03/14/2013 (8:03 am)
In a few words then:
%query = new AssetQuery();  

// Find a specific asset.
AssetDatabase.findAssetName( %query, "Crosshair2" );  

// Iterate the results.
for( %index = 0; %index < %query.count; %index++ )  
{  
   // Fetch the asset Id.
   %assetId = getWord( %query, %inde );  

   // Acquire the asset object itself.
   %asset = AssetDatabase.acquireAsset( %assetId );

   // Do some work with it.
   echo( %asset.ImageFile );

   // Release the asset.  Must do this!
   AssetDatabase.releaseAsset( %assetId );
}  

// Delete the query.
%query.delete();
#6
03/14/2013 (8:04 am)
... but a quicker way if you know the asset Id is this:
%assetId = "ToyAssets:Crosshair2";

// Acquire the asset object itself.  
%asset = AssetDatabase.acquireAsset( %assetId );  
  
// Do some work with it.  
echo( %asset.ImageFile );  
  
// Release the asset.  Must do this!  
 AssetDatabase.releaseAsset( %assetId );

You MUST understand that if you only use an asset name you may get more than one asset with the same name. Only the asset Id is guaranteed unique!

Hope this helps.
#7
03/14/2013 (8:23 am)
Thank you for your helps! Problem solved! :)