How to Load an Image and Create a object by Code
by Cezar Augusto Ximenes Wagenheimer Lima · in Torque Game Builder · 08/31/2009 (1:27 pm) · 4 replies
I'm trying using this code :
But I did get this error :
t2dStaticSprite::setImageMap() - t2dImageMapDatablock Datablock is invalid! (~/data/images/objects/banana.png)
game/gameScripts/levelload.cs (12): Unable to find object: '' attempting to call function 'compile'
What is wrong?
Thanks!
function initializeLevel()
{
$object = new t2dStaticSprite() {
imageMap = "~/data/images/objects/banana.png";
scenegraph = sceneWindow2D.getSceneGraph();
size = "200.000 150.000";
Layer = "20";
CollisionPhysicsSend = "0";
CollisionPhysicsReceive = "0";
};
$object.imageMap.imageName = "~/data/images/objects/banana.png";
$object.imageMap.compile();
}But I did get this error :
t2dStaticSprite::setImageMap() - t2dImageMapDatablock Datablock is invalid! (~/data/images/objects/banana.png)
game/gameScripts/levelload.cs (12): Unable to find object: '' attempting to call function 'compile'
What is wrong?
Thanks!
About the author
Game Developer! Creator of Druids Battle of Magic and Abra Academy Series games.
#2
Is it possible to use One Datablock to Each Scene???
I will have dozen of scenes with hundred of images, and I do not want to pre-load all this stuff, I want only load a datablock before I will use it on one scene!
Is that possible?
08/31/2009 (9:11 pm)
Thanks Nate... All seems to work now! =)Is it possible to use One Datablock to Each Scene???
I will have dozen of scenes with hundred of images, and I do not want to pre-load all this stuff, I want only load a datablock before I will use it on one scene!
Is that possible?
#3
i don't remember the post name in iTGB but here's a function based on what was there (with some fixes to solve messages in the console):
09/01/2009 (4:09 pm)
Cezar, i think there's a fair amount of discussion on this in the iTGB forum (which anyone can browse). Basically, make a datablock file for each level/scene (level1DataBlocks.cs, level2DataBlocks.cs, etc.) and load it when the level loadsi don't remember the post name in iTGB but here's a function based on what was there (with some fixes to solve messages in the console):
function t2dSceneWindow::loadLevel(%sceneWindow, %levelFile)
{
// Clean up any previously loaded stuff.
%sceneWindow.endLevel();
loadCurrentLevelDatablocks( %levelfile );
exec( %levelFile );
/*%count = $CurrentLevelDatablockSet.getCount();
for( %i = 0; %i < %count; %i++ ) {
%object = $CurrentLevelDatablockSet.getObject( %i );
%object.delete();
}*/
// $CurrentLevelDatablockSet.deleteContents();
// baylor - do you mean clear()?
$CurrentLevelDatablockSet.clear();
//baylor - what does this do? didn't we clear this so count=0?
for( %i = 0; %i < %count; %i++ )
$managedDatablockSet.add( $CurrentLevelDatablockSet.getObject( %i ) );
// Load the level.
$useNewSceneGraph = true;
%scenegraph = %sceneWindow.addToLevel(%levelFile);
if (!isObject(%scenegraph))
return 0;
%sceneWindow.setSceneGraph(%scenegraph);
// Set the window properties from the scene graph if they are available.
%cameraPosition = %sceneWindow.getCurrentCameraPosition();
%cameraSize = t2dVectorSub(getWords(%sceneWindow.getCurrentCameraArea(), 2, 3),
getWords(%sceneWindow.getCurrentCameraArea(), 0, 1));
if (%scenegraph.cameraPosition !$= "")
%cameraPosition = %scenegraph.cameraPosition;
if (%scenegraph.cameraSize !$= "")
%cameraSize = %scenegraph.cameraSize;
%sceneWindow.setCurrentCameraPosition(%cameraPosition, %cameraSize);
// Only perform "onLevelLoaded" callbacks when we're NOT editing a level
//
// This is so that objects that may have script associated with them that
// the level builder cannot undo will not be executed while using the tool.
//
if (!$LevelEditorActive)
{
// Notify the scenegraph that it was loaded.
if( %scenegraph.isMethod( "onLevelLoaded" ) )
%scenegraph.onLevelLoaded();
// And finally, notify all the objects that they were loaded.
%sceneObjectList = %scenegraph.getSceneObjectList();
%sceneObjectCount = getWordCount(%sceneObjectList);
for (%i = 0; %i < %sceneObjectCount; %i++)
{
%sceneObject = getWord(%sceneObjectList, %i);
//if( %sceneObject.isMethod( "onLevelLoaded" ) )
%sceneObject.onLevelLoaded(%scenegraph);
}
}
$lastLoadedScene = %scenegraph;
return %scenegraph;
}
#4
As far as I remember resources are by default kept in "~resources/" and you can in starting scripts decide which of them to load and which not.
09/03/2009 (2:27 pm)
You can also take a look at resources defined by TorqueGameBuilder (those with fishes and checkers). These are basically datablock with some additional functions to load and unload and set of assets (images, sounds, etc.)As far as I remember resources are by default kept in "~resources/" and you can in starting scripts decide which of them to load and which not.
Nate Gertsch
function initializeLevel() { $object = new t2dStaticSprite() { imageMap = BananaImageMap; scenegraph = sceneWindow2D.getSceneGraph(); size = "200.000 150.000"; Layer = "20"; //These default to 0 I believe //CollisionPhysicsSend = "0"; //CollisionPhysicsReceive = "0"; }; $object.setLocation(0,0); }