Game Development Community

Writing script for items

by Brandon Perkins · in Torque 3D Beginner · 01/14/2014 (9:36 am) · 6 replies

Here is just a piece of code for reference first:

function ItemData::create(%data)  
{  
    %obj = new Item()  
    {  
        dataBlock = %data;  
        static = false;  
        rotate = false;  
    };  
  
    return %obj;  
}  
  
datablock ItemData(Staff)  
{  
    category = "Weapons";  
    className = "Weapons";  
  
    shapeFile = "~/data/weapons/staff_06.dts";  
    isInvincible = true;  
  
    mass = 1.0;  
    elasticity = 0.1;  
    friction = 1.0;  
};  
  
function Staff::onCollision(%this, %obj, %col)  
{  
    if(%col.getClassName() $= "Player")  
    {  
        %obj.delete();  
          
    }  
}

My question is this:
My game will have all kinds of items ranging from armor and weapons to crafting and consumable items, so will I have to write this code for each type of item? Also do I have to make a separate script file for each individual item or for each item type? Or can I have them all in one file?

#1
01/14/2014 (12:44 pm)
Nope. Think more "meta" - what types of weapons do you have? Melee, ranged and hybrid (I'm guessing some staves can use spell abilities). Make a melee weapon datablock, an ranged weapon datablock (and associated projectile datablocks) and a hybrid datablock (or use melee and add a "special" field).

Then think about what properties your weapons can have. Add fields for these properties to each of your weapon datablocks.

Next, create a database of weapons. at runtime you can create a new weapon of any type with any set of properties that you have in your database.

Done.

datablock ItemData(meleeWeapon)    
{    
    category = "Weapons";
    // added subtype
    subtype = "melee";
    // added special field
    special = "6 6 fireball";  // space-separated see below (6 x 1to6 fireball)
    className = "Weapons";
    // add damageType
    damageType = "fire";
    
    shapeFile = "~/data/weapons/staff_06.dts";    
    isInvincible = true;    
    
    mass = 1.0;    
    elasticity = 0.1;    
    friction = 1.0;    
};    

function spellEffect::fireball(%dice, %dieType)
{
    %damage = 0;
    for (int %i = 0; %i < %dice; %i++)
    {
        %damage += getRandom(1, %dieType);
    }
    return %damage;
}

function meleeWeapon::onCollision(%data, %proj, %col, %fade, %pos, %normal)
{    
    if(%col.getClassName() $= "Player")    
    {
        if(%this.special $= "")
            %obj.delete();
        else
        {
            %damageNum = getWord(%this.special, 0); // 6
            %damageDie = getWord(%this.special, 1); // 6
            %function = getWord(%this.special, 2); // "fireball"
            if(%function $= "fireball")
            {
                %col.damage(%proj.sourceObject, %pos, %data.directDamage, %data.damageType);
            }
        }            
    }    
}

More or less....

The moral of the story is, use a database to store large quantities of data, only load and create what you need.

And ItemData is for "pick-ups" - WeaponImageData is for equipable weapons.
#2
01/14/2014 (12:52 pm)
How would I go about creating a database for items and how would I retrieve the information from the database?
#3
01/14/2014 (4:30 pm)
There are a few different ways - Google will help.
#4
01/14/2014 (6:36 pm)
oh so it's just making a normal database? Nothing special needs to be done just because its torquescript? Then could I use xml and or can torquescript not retrieve data from xml files?
#5
01/15/2014 (11:15 am)
*cough* XML/SQL *cough* ;)
#6
01/15/2014 (12:25 pm)
You can build your database in any way that you please. If you're going to use MySQL/PostgreSQL/MSSQL etc you'll have to get them integrated into the engine and add support for working with them in torquescript. There should be some engine functions to read XML available in Torquescript already, but I recall seeing some posts about issues with that.