Dynamically Setting a Property for a Script
by David Hutchison · in Torque 3D Beginner · 09/24/2012 (6:02 pm) · 6 replies
I would like to be able to dynamically set the DirectDamage property for a weapon by storing its value in a custom property in my mission file (i.e., ideally in the LevelInfo datablock).
I am able to assign the custom property (e.g., theDirectDamage) to the LevelInfo object, but I can't figure out the script for assigning the value I store there to the DirectDamage property in a weapons datablock.
Any help would be appreciated. Thanks.
I am able to assign the custom property (e.g., theDirectDamage) to the LevelInfo object, but I can't figure out the script for assigning the value I store there to the DirectDamage property in a weapons datablock.
Any help would be appreciated. Thanks.
#2
09/24/2012 (11:58 pm)
@Steve plus 1 for a well described answer
#3
Much appreciated.
09/25/2012 (5:41 pm)
Thanks Steve for the detailed reply. I will test this out over the next couple of days and report back.Much appreciated.
#4
09/25/2012 (7:16 pm)
I tested it and it works great. Thanks again Steve.
#5
09/25/2012 (8:28 pm)
Hurray!
#6
09/25/2012 (8:36 pm)
If this were Stack Overflow I would upvote you.
Associate Steve Acaster
[YorkshireRifles.com]
scripts/server/projectile.cs
Here's the stock function, note that it uses the projectile/itself as %data to find the "directDamage".
function ProjectileData::onCollision(%data, %proj, %col, %fade, %pos, %normal) { //echo("ProjectileData::onCollision("@%data.getName()@", "@%proj@", "@%col.getClassName()@", "@%fade@", "@%pos@", "@%normal@")"); // Apply damage to the object all shape base objects if (%data.directDamage > 0) { if (%col.getType() & ($TypeMasks::ShapeBaseObjectType)) %col.damage(%proj, %pos, %data.directDamageb, %data.damageType); } }So you could swap that with the damageModifier in your "theLevelInfo". Note - this will swap damage for ALL projectile types.
If you want to use it for individual weapon projectile types then make a custom onCollision() function for that type of projectile. If you look in art/datablocks/weapons/lurker.cs you'll see that the Lurker does just this, and has it's own projectile onCollision() function which overrides the default in projectile.cs. Note, "BulletProjectile" is also used by other weapons, so changing it will affect others too (eg: Ryder.cs - the pistol)
datablock ProjectileData( BulletProjectile ) { projectileShapeName = ""; directDamage = 5; radiusDamage = 0; damageRadius = 0.5; areaImpulse = 0.5; impactForce = 1; explosion = BulletDirtExplosion; decal = BulletHoleDecal; muzzleVelocity = 120; velInheritFactor = 1; armingDelay = 0; lifetime = 992; fadeDelay = 1472; bounceElasticity = 0; bounceFriction = 0; isBallistic = false; gravityMod = 1; }; function BulletProjectile::onCollision(%this,%obj,%col,%fade,%pos,%normal) { // Apply impact force from the projectile. // Apply damage to the object all shape base objects if ( %col.getType() & $TypeMasks::GameBaseObjectType ) %col.damage(%obj,%pos,%this.directDamage,"BulletProjectile"); }I'd still think about throwing in a sanity check to make sure "theLevelInfo" has the correct variable, and then default to the projectile's own directDamage if not.
So you could make a new projectile - change the projectile that the weaponImage fires to it.
example: art/datablocks/weapons/ryder.cs (pistol)
datablock ShapeBaseImageData(RyderWeaponImage) { //... projectile = CustomBulletProjectile;//changed projectileType = Projectile; projectileSpread = "0.0"; //... }In the stock file there is a blank section called "projectiles" ABOVE the shapeBaseImageData - there's a heiracrchy to maintain, things that are called from a datablock must already have been instantiated in memory - add this:
datablock ProjectileData( CustomBulletProjectile ) { projectileShapeName = ""; directDamage = 5; radiusDamage = 0; damageRadius = 0.5; areaImpulse = 0.5; impactForce = 1; explosion = BulletDirtExplosion; decal = BulletHoleDecal; muzzleVelocity = 120; velInheritFactor = 1; armingDelay = 0; lifetime = 992; fadeDelay = 1472; bounceElasticity = 0; bounceFriction = 0; isBallistic = false; gravityMod = 1; }; function CustomBulletProjectile::onCollision(%this,%obj,%col,%fade,%pos,%normal) { // Apply impact force from the projectile. // Apply damage to the object all shape base objects if ( %col.getType() & $TypeMasks::GameBaseObjectType ) { //sanity check %damageMod = theLevelInfo.customDamage;//if it exists we use it if(%damageMod < 1)//if more than 1 we don't change it, if less than ... %damageMod = %this.directDamage;//... revert to standard projectile damage %col.damage(%obj, %pos, %damageMod, "CustomBulletProjectile");//call %damageMod } }Obviously in the above example the variable on "theLevelInfo" is called "customDamage".
If this doesn't work, try using the function to call a dynamicField specifically - I can't remember what this is off the top of my head so check the T3D Script Manual chm file or call dump() on "theLevelInfo" object.