Datablocks to Database
by Dreamer · in Torque Game Engine · 04/27/2005 (9:36 am) · 3 replies
Hello everyone, I got kinda tired of having all my datablocks scattered around, and so I decided to set my mind to finding a way to completely get rid of them, starting with the all the items.
I intend to make this a tutorial at some point, but I want to abstract my example out further to handle all types of datablocks in game and not just items, I also wanted to get some feedback, and error checking.
Eventually I will replace the INSERT statements with a single GUI that can interface with the in game database, and allow on the fly creation of new items etc, but for now this will have to do.
I intend to make this a tutorial at some point, but I want to abstract my example out further to handle all types of datablocks in game and not just items, I also wanted to get some feedback, and error checking.
Eventually I will replace the INSERT statements with a single GUI that can interface with the in game database, and allow on the fly creation of new items etc, but for now this will have to do.
function InitializeDB(){
//This function only ever needs initialized once
//Create Tables
if($Pref::Server::RunOnce != 1){
%query[0] = "CREATE TABLE Items (BaseType VARCHAR(30),ItemName VARCHAR(50),Category VARCHAR(20),ClassName VARCHAR(20),ShapeFile VARCHAR(255),mass VARCHAR(10),elasticity VARCHAR(10),friction VARCHAR(10),emap VARCHAR(5),pickUpName VARCHAR(50),Image VARCHAR(50),Dynamics VARCHAR(255))";
//Leaving space for new tables here
for(%x = 0; %x <= 1; %x++){
echo(%query[%x]);
%result = $SqLite.query(%query[%x],0);
if(%result){
$SqLite.clearResult(%result);
}
}
InitItems();
$Pref::Server::RunOnce = 1;
}
}
function InitItems(){
//Insert Items
//INSERT INTO Items VALUES (BaseType,ItemName,Category,ClassName,ShapeFile,mass,elasticity,friction,emap,pickUpName,Image, Dynamics)";
%query[0] = "INSERT INTO Items VALUES ('ItemData','Sword','Weapon','Weapon','dream.game/data/shapes/sword/sword.dts','1','0.2','0.6','true','a sword','SwordImage','')";
%query[1] = "INSERT INTO Items VALUES ('ItemData','HealthKit','Health','Health','dream.game/data/shapes/items/healthKit.dts','1','1','0.3','true','a health kit','','repairAmount 50')";
%query[2] = "INSERT INTO Items VALUES ('ItemData','HealthPatch','Health','Health','dream.game/data/shapes/items/healthPatch.dts','1','1','0.3','true','a health patch','','repairAmount 20 maxInventory 0')";
%query[3] = "INSERT INTO Items VALUES ('ItemData','Crossbow','Weapon','Weapon','dream.game/data/shapes/crossbow/weapon.dts','1','0.2','0.6','true','a crossbow','CrossbowImage','')";
for(%x = 0; %x <= 3; %x++){
echo(%query[%x]);
%result = $SqLite.query(%query[%x],0);
$SqLite.clearResult(%result);
}
//This code creates datablocks dynamically from database table
%query = "SELECT * FROM Items";
%result = $SqLite.query(%query,0);
$SQLite.dump();
if(%result){
while(!$SqLite.endOfResult(%result)){
%BaseType = $SqLite.getColumn(%result,1);
%Name = $SqLite.getColumn(%result,2);
%dbstring ="datablock "@%BaseType@"("@%Name@"){";
for(%x = 2; %x <= $SqLite.numColumns(%result) - 1; %x++){
%varName = $SqLite.getColumnName(%result, %x);
if(%varName !$= "Dynamics"){
if($SqLite.getColumn(%result,%varName) !$="" ){
%dbstring = %dbstring@ "\n"@ %varName@" = \""@$SqLite.getColumn(%result,%varName)@"\";";
}
}else{
%Dynamics = $SqLite.getColumn(%result,%varName);
//echo("To see this means that Dynamics is an invalid Column");
%i = 0;
while (getWord(%Dynamics, %i) !$= ""){
%varName = getWord(%Dynamics, %i);
%varValue = getWord(%Dynamics, %i++);
%dbstring = %dbstring @ "\n"@ %varName @ " = \"" @ %varValue @ "\";";
%i ++;
}
}
}
%dbstring = %dbstring@"\n};";
echo("Pulling new datablock from db");
echo(%dbstring);
eval(%dbstring);
$SqLite.nextRow(%result);
}
$SqLite.clearResult(%result);
}else{
}
}
#2
04/27/2005 (12:25 pm)
Almost forgot, if you find bugs, or can see a better way of doing this, please feel free to let me know... I want to get this perfect (or as near as I can), before making it into a resource.
#3
02/11/2006 (7:45 pm)
An intersting idea, but where on Torque's green earth are you putting this code? :)
Torque Owner Dreamer
Default Studio Name