GuiSpriteCtrl::setImageFrame() does not function
by Sean Xie · in Torque 2D Beginner · 01/25/2014 (4:06 pm) · 18 replies
I'm trying to set the image frame of a GuiSpriteCtrl object using this function. But I always get
ImageFrameProviderCore::setImageFrame() - Cannot set Frame without existing asset Id.
in my console.
Also, when I try to print out getImageFrame() for the object, the result is always -1.
Can someone explain to me how to set the image frame for a GuiSpriteCtrl object?
ImageFrameProviderCore::setImageFrame() - Cannot set Frame without existing asset Id.
in my console.
Also, when I try to print out getImageFrame() for the object, the result is always -1.
Can someone explain to me how to set the image frame for a GuiSpriteCtrl object?
#2
01/26/2014 (2:08 am)
Are getImageFrame() and setImageFrame() always associated with animation? What if I have a bunch of icons in one image asset, and want to set the image of the GuiSpriteCtrl to one of them?
#3
01/26/2014 (2:20 am)
No, get and setImageFrame have nothing to do with animations. They are used for static images. The error message you are getting implies something is not right with your image asset. Check to see if it is properly defined and loaded.
#4
...
%GSC.setImage(%obj.getImage());
%GSC.setImageFrame(%obj.getImageFrame());
...
where %GSC is my GuiSpriteControl and %obj is an existing Sprite on the scene.
The sprite %obj appears on the screen without any problems, but %GSC still does not get the correct image frame.
01/26/2014 (4:01 am)
I am using the function as follows:...
%GSC.setImage(%obj.getImage());
%GSC.setImageFrame(%obj.getImageFrame());
...
where %GSC is my GuiSpriteControl and %obj is an existing Sprite on the scene.
The sprite %obj appears on the screen without any problems, but %GSC still does not get the correct image frame.
#5
It might be better to examine your %obj.get...() results before dropping them into %otherObj.set...() to ensure you have what you think you have - I went through a similar problem while making the animation editor for 3SS.
01/26/2014 (7:29 am)
Ok, how about this - is the scene sprite using an animation? If so, SpriteBase.getImage() returns StringTable->EmptyString, but you should see "SpriteBase::getImage() - Method invalid, not in static mode." in the console if this is the case.It might be better to examine your %obj.get...() results before dropping them into %otherObj.set...() to ensure you have what you think you have - I went through a similar problem while making the animation editor for 3SS.
#6
Are you sure that GuiSpriteCtrl::setImageFrame() only works when the GuiSpriteCtrl object uses an animation?
01/26/2014 (1:41 pm)
I'm not using animations in both cases. %obj.getImage() and %obj.getImageFrame() both return the correct result. Even if I hard-code the results into %GSC.set..., it still does not work.Are you sure that GuiSpriteCtrl::setImageFrame() only works when the GuiSpriteCtrl object uses an animation?
#7
See engine/source/2d/gui/guiSpriteCtrl_ScriptBindings.h, line 40:
So, if the sprite in the scene is running an animation the call to getImage() will return StringTable->EmptyString.
So if you're not using animations it should work.
What I was recommending was this (helps with debugging):
01/26/2014 (5:40 pm)
No - exactly the opposite - it does not work if it is using an animation.See engine/source/2d/gui/guiSpriteCtrl_ScriptBindings.h, line 40:
ConsoleMethod( GuiSpriteCtrl, getImage, const char*, 2, 2, "() - Gets current image asset Id.n"
"@return (string imageAssetId) The image being displayed.")
{
// Are we in static mode?
if ( !object->isStaticFrameProvider() )
{
// No, so warn.
Con::warnf( "GuiSpriteCtrl::getImage() - Method invalid, not in static mode." );
return StringTable->EmptyString;
}
// Get image.
return DYNAMIC_VOID_CAST_TO(GuiSpriteCtrl, ImageFrameProvider, object)->getImage();
}So, if the sprite in the scene is running an animation the call to getImage() will return StringTable->EmptyString.
So if you're not using animations it should work.
What I was recommending was this (helps with debugging):
%imgAsset = %obj.getImage();
echo(" -- got " @ %imgAsset.AssetName @ " with ID " @ %imgAsset.getAssetId()); // check the asset name and ID
%GSC.setImage(%imgAsset.getAssetId()); // note this change....
%GSC.setImageFrame(%obj.getImageFrame());You may need to get the image, request a new instance from the AssetDatabase and then assign the new instance to the control if the call to getAssetId() fails:%imgAsset = %obj.getImage();
echo(" -- got " @ %imgAsset.AssetName @ " with ID " @ %imgAsset.getAssetId()); // check the asset name and ID
%asset = AssetDatabase.acquireAsset(%imgAsset);
%GSC.setImage(%asset.getAssetId()); // note this change....
AssetDatabase.releaseAsset(%asset);
%GSC.setImageFrame(%obj.getImageFrame());
#8
When instantiating the object, I used:
%obj = new Sprite();
%obj.setImage("someModule:someImage",10);
...(some more codes)
So I think I am using images, not animations.
01/27/2014 (8:13 am)
I tried pulling the %imgAsset.AssetName and got an empty string.When instantiating the object, I used:
%obj = new Sprite();
%obj.setImage("someModule:someImage",10);
...(some more codes)
So I think I am using images, not animations.
#9
01/27/2014 (9:15 am)
Ok, then try dumping the result of getImage() directly:%imgAsset = %obj.getImage();
echo(" -- got " @ %imgAsset @ " with ID " @ %imgAsset.getAssetId()); // check the asset name and IDIf that drops " -- got someModule:someImage " then you definitely have to acquire the asset from the database:%asset = AssetDatabase.acquireAsset(%imgAsset); %GSC.setImage(%asset); AssetDatabase.releaseAsset(%asset);Hopefully that'll do it.
#10
When I acquire the asset from the database, I got the messages:
Asset Manager: Failed to release asset Id '1213' as it dos not exist.
ImageFrameProviderCore::setImageFrame() - Cannot set Frame without existing asset Id.
01/27/2014 (10:26 am)
That does drop "-- got someModule:someImage with ID"When I acquire the asset from the database, I got the messages:
Asset Manager: Failed to release asset Id '1213' as it dos not exist.
ImageFrameProviderCore::setImageFrame() - Cannot set Frame without existing asset Id.
#11
01/27/2014 (2:34 pm)
Ok - how about this?%GSC.Image = %obj.getImage(); %GSC.setImageFrame(%obj.getImageFrame());
#13
In AudioBasicsToy::createButtons() I added this at the end:
This works for me. Give it a shot and see if it functions as billed. If so, something odd is going on with your particular case.
01/29/2014 (9:41 am)
Ok, I grabbed the audioBasics toy and tweaked it like so:In AudioBasicsToy::createButtons() I added this at the end:
%this.guiSprite = new GuiSpriteCtrl();
%this.guiSprite.position = "5 5";
%this.guiSprite.extent = "32 32";
%this.guiSprite.Image = "AudioBasicsToy:StopButton";
SandboxWindow.addGuiControl(%this.guiSprite);Modified AudioBasicsToy::createAnimatedSprite() to this:function AudioBasicsToy::createAnimatedSprite(%this)
{
// Create the sprite.
%object = new Sprite() {class = "KnightClass";};
// Set the position.
%object.Position = "50 -15";
// If the size is to be square then we can simply pass a single value.
// This applies to any 'Vector2' engine type.
%object.Size = 8;
// Set the sprite to use an animation. This is known as "animated" image mode.
%object.Image = "ToyAssets:TD_Knight_CompSprite"; // replaced %object.Animation = ...
%object.setImageFrame(1); // set frame to 1
// Setup collision properties.
%object.createPolygonBoxCollisionShape(7, 8);
%object.setCollisionCallback(true);
// Add the sprite to the scene.
SandboxScene.add(%object);
// Give the sprite a velocity.
%object.setLinearVelocityX(-12);
%this.guiSprite.Image = %object.getImage();
%this.guiSprite.setImageFrame(%object.getImageFrame());
}Then at the end of TrapClass::onCollision() I added this:AudioBasicsToy.guiSprite.setImage("AudioBasicsToy:StopButton");So clicking the play button next to "sound effect demo" spawns the sprite and updates the GuiSpriteCtrl in the top left of the screen.This works for me. Give it a shot and see if it functions as billed. If so, something odd is going on with your particular case.
#14
But I will try to create a new module and include the codes above later today.
I'll post the result after I try it.
Thank you for your help.
01/29/2014 (12:16 pm)
I cannot find the Audio Basics Toy from the module directory.But I will try to create a new module and include the codes above later today.
I'll post the result after I try it.
Thank you for your help.
#16
I tried doing the modifications on AudioBasicsToy and the guiSpriteCtrl worked in that case.
But it seems that when I do the similar thing in my own project, every time the program calls guiSpriteCtrl::setImageFrame, it'll fail.
01/31/2014 (10:36 am)
Thanks!I tried doing the modifications on AudioBasicsToy and the guiSpriteCtrl worked in that case.
But it seems that when I do the similar thing in my own project, every time the program calls guiSpriteCtrl::setImageFrame, it'll fail.
#17
01/31/2014 (2:56 pm)
So somewhere something is either mis-named or it's being stomped or something. Hope you have Torsion - otherwise debugging scripts is going to be really "exciting...."
Torque Owner Richard Ranft
Roostertail Games