Game Development Community

Flashlight - onFire

by J0linar · in Torque 3D Beginner · 01/22/2013 (11:56 am) · 15 replies

After some playing around i came to teh idea to add the flashlight (actually just a spotlight)
to a WeaponImage::onFire

i used the Flashlight Resource by CSMP
T3D Mounted Flashlight Resource

Now to te point i added the following into
scripts/server/weapon.cs

function FlashlightWeaponImage::onFire(%this)
    {
          // spotlight  
          %light = new SpotLight() // In here you can add the example datablock values for more control  
          {  
			 range = "50";
			 innerAngle = "8";
			 outerAngle = "44";
			 brightness = "4";
			 castShadows = "1";
			 color = "4 3 1 2";
			 cookie = "art/gui/torch.png"; // Basic Lighting will not render this.
			 imageAnimPrefix = "ProxMine";
			 imageAnimPrefixFP = "ProxMine";
			 // animationType = FlickerLightAnim;
			 //animationPeriod = 1.02;
             // Lens Flare will need a good mount and will need the mountOffset of the Flashlight   
             // to be modified for your specific application.  
             flareType = "LightFlareExample1"; //"LightFlareExample1"; "NullLightFlare";   
          };  
          %this.mountobject(%light, 0, "0.05 0 0"); // mountobject(%obj, mountNode, mountOffset)	  
          %this.light = %light;
		  %this.light.schedule(95000, "delete");
    }

ingame after trying my weapon with teh above onFire script
i get a Unknown command mountobject error

Is there some way i could resolve this?
i tried some stuff like having the script in client.cs and executing the onFire in the commands.cs
with no luck.

Before some questions raise about the schedule/ it works and its purpose is to shut itself or simulate some kind of energy draining

And about the imageAnimPrefix = it works when used with the light, it gives a more natural feel when running/ staying around with the flashlight

either way i would really appreciate some help here
thx for taking the time

#1
01/22/2013 (2:28 pm)
If I remember correctly you can't mountObject to a shapeBaseImage (I could be wrong its a long time since I tried).

I think the spotlight resource was meant to be mounted to the player not the weapon.

Have just had a look at the resource and yes It was designed to be mounted to the player, you can get the same result by instead of adding to 'scripts/client/default.bind.cs' add this to your weapons FlashLight.cs (or whatever your weapons.cs file is)

$FlashlightVar = 1;  
function toggleFlashlight(%val)  
{  
   if (%val)  
   {  
      if($FlashlightVar)  
      {  
         $FlashlightVar = 0;  
         commandToServer('EnableFlashlight');  
      }  
      else  
      {  
         $FlashlightVar = 1;  
         commandToServer('DisableFlashlight');   
      }  
   }  
}  

function FlashlightWeaponImage::onFire(%this)  
{  
   toggleFlashlight(1);
}
this will toggle the flashlight on or off when you press fire
#2
01/22/2013 (9:56 pm)
David, it might be a good idea to instead use a CommandToClient there, just to be safe.

Revised:

In scripts/client/default.bind.cs
$FlashlightVar = 1;    
function clientCmdToggleFlashlight(%val)    
{    
   if (%val)    
   {    
      if($FlashlightVar)    
      {    
         $FlashlightVar = 0;    
         commandToServer('EnableFlashlight');    
      }    
      else    
      {    
         $FlashlightVar = 1;    
         commandToServer('DisableFlashlight');     
      }    
   }    
}

In your weapon's script file with the ::onFire function:

function YourWeaponImage::onFire(%this,%obj,%slot)
{
  CommandToClient(%obj.client,'ToggleFlashLight',true);

  //......
}
#3
01/23/2013 (1:13 am)
function YourWeaponImage::onFire(%this, %obj, %slot)
This is the key. J0linar, when the onFire function is called, %this refers to the ShapeBaseImage datablock, and %obj refers to the Player the image is mounted on. So you need to call mountObject on %obj, not on %this.
#4
01/23/2013 (9:19 am)
Thx gyus, ye i figured it out so far, with some help of another forum user
anyways now comes to interesting part as in get it to actually use the weapons ammo/ energy

This is my new onFire function (could end up as a resource^^)
function FlashlightWeaponImage::onFire(%this, %obj)  
{  
%obj.mountable = true;  
     
// spotlight    
%light = new SpotLight() // In here you can add the example datablock values for more control    
   {    
   lightType = "SpotLight";
   lightColor = "0.992126 1 1 0.992126";
   lightRadius = "64.5";
   lightDuration = "99000";
   brightness = "4";
   cookie = "art/gui/torch.png";
   castShadows = "1"; // Basic Lighting will not render this.  
   imageAnimPrefix = "ProxMine";  
   imageAnimPrefixFP = "ProxMine"; 
   // animationType = FlickerLightAnim;  
   //animationPeriod = 1.02;  
   // Lens Flare will need a good mount and will need the mountOffset of the Flashlight    
   // to be modified for your specific application.    
   flareType = "LightFlareExample1"; //"LightFlareExample1"; "NullLightFlare";    
   };
				  
   %obj.light = %light;  
   %obj.mountObject(%light, 0, "-0.3 0 0");
   %obj.light.schedule(95000, "delete");   
   }
}

how would i go about letting it use/ drain the ammo of the weapon it gets the onFire? any tips
#5
01/23/2013 (9:44 am)
You need to give it some ammo.

Copy the Lurker ammo datablock, call it something like FlashlightAmmo.
Then add it to the player datablock. Been a while since I have done this stuff.


I think there is a tutorial that shows you how to add a new weapon and ammo. I'll see if I can find it.

Or someone can post a link if they already know.




#6
01/23/2013 (9:54 am)
Well thx, i do know how to add a weapon, actually this is a weapon with its own datablock
Thing is the following -
i want to use the onFire function to turn the Flashlight on/(off if possible)

(I already investigated other optiosn as for example the MuzzleFlash/ Weaponfirelight - after coding a new WeaponfireLight that acts like a SpotLight i lost teh abbility to add cookies to it/ in my case the cookie is sorta essential as it gives the Flashlight some kidn of natural feel)

and at the same time i want it to drain the Flashlight ammo

now when adding the
extra functions described by Bryce & Daniel
i get the weaponimage::onFire to drain the ammo but i loose the light as in
i dont get it to see/ or activate.

To explain the whole thing in a sentence
am working on a Project that is heavily depending on Lights
and Darkness.
#7
01/23/2013 (10:31 am)
I think you need to call the creation of your SpotLight and mounting outside of the onFire function.

Create another function, something like this...

function TurnOnFlashLight(%obj)
{
%obj.mountable = true;    
       
// spotlight      
%light = new SpotLight() // In here you can add the example datablock values for more control      
   {      
   lightType = "SpotLight";  
   lightColor = "0.992126 1 1 0.992126";  
   lightRadius = "64.5";  
   lightDuration = "99000";  
   brightness = "4";  
   cookie = "art/gui/torch.png";  
   castShadows = "1"; // Basic Lighting will not render this.    
   imageAnimPrefix = "ProxMine";    
   imageAnimPrefixFP = "ProxMine";   
   // animationType = FlickerLightAnim;    
   //animationPeriod = 1.02;    
   // Lens Flare will need a good mount and will need the mountOffset of the Flashlight      
   // to be modified for your specific application.      
   flareType = "LightFlareExample1"; //"LightFlareExample1"; "NullLightFlare";      
   };  
                    
   %obj.light = %light;    
   %obj.mountObject(%light, 0, "-0.3 0 0");  
   %obj.light.schedule(95000, "delete");     
}  


function FlashlightWeaponImage::onFire(%this, %obj)    
{    
   TurnOnFlashLight($this);
}

#8
01/23/2013 (10:47 am)
Instead of scheduling your light to be deleted, make use of the ShapeBaseImage state system. For example, what if your user wants to turn the light off before it runs out of energy? Torque Players already have an 'energy' bar that your light image can use. Simply set the usesEnergy field to true, and then make use of the stateTransitionOnAmmo and stateTransitionOnNoAmmo members. Have a look at this page for documentation on images, or try the FPS tutorial on this site.

A good starting point would be to have two states for your flashlight: on and off. First, let the player switch between them with the trigger. Then write callbacks for them that create or delete the light object. Then make a transition that turns the light off when there's no energy!
#9
01/23/2013 (10:50 am)
To use energy, simply have the relevant image state use an energyDrain value greater than 0. Energy is drained at a rate of energyDrain/tick while the image is in this state.

For a flashlight to use energy I would work my state machine such that the firing (or trigger down state) would transition to a flashlighton state that has energyDrain. This state would idle until the next trigger press that would transition to a flashlightoff state, which in turn would time out and return back to the ready state.

Ammo for energy based weapons/images are not necessary. However, there may be some check(s) in the onFire(), onMount(), or onUse() callbacks (it's been a while since I paid attention to stock script code) that would prevent the use of a "weapon" that doesn't have ammo. This is easily changed. Simply tweak the logic such that if the image has the boolean enabled for usesEnergy then skip the ammo check and proceed as normal.
#10
01/23/2013 (10:53 am)
And... in combination with what Daniel said you'll also need to transition states once energy dips below the minimum energy threshold.
#11
01/23/2013 (11:03 am)
Thats some great info guys, thx
As actually all the Weapons are goin to be of non violent nature
it is the only option.

With that info i might get it done, again thx
#12
01/25/2013 (5:09 am)
Sry to bump thsi up again but am havin no real clue how to get a energy based weapon working

If someone has an example that i could apply to start with, that would be reall appreciated.

I did searched the forums but i just didnt found a example of a energy based weapon and @ Michael Hall, regarding the states
is it enough to state energyDrain and to let it transition back? or did i got that totally wrong

either way thx for the help so far
#13
01/25/2013 (2:23 pm)
I think you just need to do a couple things. In the ShapeBaseImageData datablock add these lines.

usesEnergy = true; //Tells the weapon to use energy
MinEnergy = 50; //Minimal energy required to fire the weapon.


Then add this to the onFire state.
stateName[3] = "Fire";
stateEnergyDrain[3] = 10;


It does not use ammo and you need to adjust the energy settings.

#14
01/25/2013 (2:33 pm)
Here is something I found that Michael was suggesting. Not exact but similar.

Put this in the weapons datablock

energyDrain = 10;


Put this at the top of your weapons onFire function.

//Don't fire if we don't have the energy  
   if (%obj.getEnergyLevel() < %this.energyDrain)  
          return;  
      
   %obj.setEnergyLevel(%obj.getEnergyLevel() - %this.energyDrain);


#15
01/25/2013 (2:53 pm)
hey many thx Inflight
really appreciate that, will share how it ends up kudos