May 23, 2012, 01:19:18 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Please report any bugs or issues that you might be encountering with the Beta in the Support System so that we can better keep track of any oustanding issues that may come up.

GameCore Support System
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: FPS Flashlight example  (Read 1578 times)
Ransom
Full Member
***
Posts: 132



View Profile
« on: July 31, 2008, 01:32:47 AM »

Yo.  Here is a first person flashlight example I whipped up.

Step 1.  Add this to your input.cfg under "InputSet Player Character"
Quote
Action FlashLight
KeyMapping KeyF FlashLight

Step 2.  Add this to your player's .opr
Quote
GlobalLight 1
AddLight Flashlight
LightType Spot
LightConeAngle 15
LightEdgeAngle 5
LightColour 255 255 255
LightIntensity 1.2
LightShadowType ShadowMap
LightSpecular 1.0
LightFalloff Linear 1.5
LightPosition 0.0 1.3 0.00

Step 3. Add this to your player's .gsl

In the variables section add...
Quote
bool TurnOnFlash = false;
bool FlashPressed = false;
float lastFlashPressed = 0;

Inside
Quote
bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
{
   if (event == "Input")

 Add this...
Quote
if (params.stringValue == "FlashLight")
      {
         if ( params.floatValue >= 0.5f && lastFlashPressed < 0.5f)
            FlashPressed = true;
         lastFlashPressed = params.floatValue;
         return true;
      }

And inside the DynamicsAdvance section add this...
Quote
Light@ light = object.GetLight("Flashlight");
   light.SetRotationEuler(Vector( 0, 180 + lookUpDown , 0));
   light.SetEnabled(TurnOnFlash);
   if ( FlashPressed)
   {
      FlashPressed = false;
      if(TurnOnFlash == false)
         {
            TurnOnFlash = true;            
         }
      else if(TurnOnFlash == true)
         {
            TurnOnFlash = false;
         }

   }

Tweak the settings in the .opr to your liking.  Try experimenting with different colors, cone and edge angles, and also different fall off types and distances.

Enjoy!
Logged
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« Reply #1 on: July 31, 2008, 02:09:53 AM »

That is a great script. Thanks for contributing again Smiley
Logged
Javier
Newbie
*
Posts: 47



View Profile
« Reply #2 on: July 31, 2008, 03:15:14 AM »

Nice Ramsom.  Grin

Works very well and the code is very clear.

Thanks for the contribuition.
Logged
Delerna
Jr. Member
**
Posts: 50


View Profile
« Reply #3 on: August 01, 2008, 08:48:36 PM »

And good instructions too. Worked for me first time.
Logged
gekido
Guest
« Reply #4 on: August 08, 2008, 11:53:13 AM »

Cool, will sticky.

Actually will probably just add this to the default script - I have added crouching and jump as well - and wanted a flashlight as well ;}
Logged
Squat
Hero Member
*****
Posts: 592


View Profile
« Reply #5 on: August 25, 2008, 12:53:07 AM »

I've developed the flashlight into a script that turns objects into lightswitches. It's a bit lazy at the moment, but a good start for anybody who's still getting aquainted with scripting. I have plans to make it more clean, such as putting the actual light values into controller variables instead of right in the code so that one script can do multiple lights. However, you'll have to point the switch to a current light source, so you might have to have it figure out which lightsource is closest if you want something more dynamic, such as switches that work on the closest light or something. For now, you have to make a new script for a new light.

This also causes conflicts with using multiple lights at one time. Since each of them sets the ambient to 0,0,0, if the other light is on, it no longer looks correct. I want to figure out how to increase the ambient value by a variable instead of manually inserted values and have it decrease by that amount when off so that it adds to the overall lighting. I gotta get the syntax for that first cause of the commas. This is, of course, if you want to boost the ambient when a particular light goes on or off, otherwise comment that part out. I needed it and it was a great relief to see how easy it was to control.

You can tie this script in to any object that you want to act as the actual switch for the light. It is in that object that the input area is laid. The code itself will need to point to the name of your object and your light, but that means you can have any object control any other. Again, i don't know how to dynamically get it to realize which object to work on, but I don't find it necessary at the moment.

Please help me enhance it any way you can think of or make it more efficient.

config.cfg
Quote
Action Lights
KeyMapping KeyL Lights

Object's ORP file. Substitute your own object or replace with a box to test.
Quote
OPRP

LoadObject Lamp.obj

AddCollision Lamp
CollisionType UseRenderObject

ControllerType Scripted
ControllerInputCenter 0.0000 3.0000 0.0000
ControllerInputBoxSize 4.0000 6.0000 4.0000
ControllerSetting ScriptFile LampSwitch.gsl

GlobalLight 1

AddLight Light1
LightType Area
LightColour 255 255 26
LightPosition 0.0000 6.0000 0.0000
LightIntensity 0.01 // don't use 0 or lag on switch
LightFalloff InvDistance 40.0000
LightArea 8 8
LightSpecular 0.1000
LightEnabled 1

AddLight Light2
LightType Directional
LightColour 255 255 200
LightIntensity 0.01 // don't use 0 or lag on switch
LightSpecular 0.3300
LightRotation 0.0000 -90.0000 0.0000
LightPosition 0.0000 7.0000 0.0000
LightShadowType None
LightEnabled 1

AddLight Light3
LightType Directional
LightColour 255 255 200
LightIntensity 0.01 // don't use 0 or lag on switch
LightSpecular 0.4000
LightRotation 0.0000 90.0000 0.0000
LightPosition 0.0000 10.0000 0.0000
LightShadowType None
LightEnabled 1

CastShadow 0
VisibleInReflections 0
EnableLightMap 0
CastLightMapShadow 0

LampSwitch.gsl
Quote
bool DEBUG_ENABLED = true; //set to false to disable console feedback

bool LightsPressed = false; //on/off variable


//Thank you to zknack for the inputs delay code.
float inputTimer = 0.5f; //adjusts time in seconds between inputs
float lastInput = 0; //used to capture the time in seconds from the game
bool inputEnabled = true; //on_off variable for inputs, read in HandleEvent, written in DynamicsAdvance

void DynamicsAdvance( Object@ object, float seconds)
{
   //zknack's input Delay code.
   if(!inputEnabled)
   {
      lastInput+=seconds;
      if(lastInput >= inputTimer)
      {
         inputEnabled = true;
         lastInput = 0.0f;
         if(DEBUG_ENABLED)
         Print("Input Timer Stopped");
      }
   }
}

bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
{
   GameManager@ game = GetGameManager();
   
   // Only Events for Player.
   if (params.player == null)
      return false;


   // Input event
   if (event == "Input")
   {
      if (params.stringValue == "Lights")
      {
       
        if(params.floatValue > 0 && inputEnabled)
         {
            inputEnabled = false; //input time variable
            if(DEBUG_ENABLED)
               Print("Input Timer Started");
          if (LightsPressed == false)
          {
         
          game.GetWorld().GetObject("lamp").GetLight("Light1").SetIntensity(0.1);
          game.GetWorld().GetObject("lamp").GetLight("Light2").SetIntensity(0.7);
          game.GetWorld().GetObject("lamp").GetLight("Light3").SetIntensity(0.86);
          GetGameManager().GetWorld().SetAmbientColour( 130,130,0);
          LightsPressed = true;
            
          return true;         
          }
         
          game.GetWorld().GetObject("lamp").GetLight("Light1").SetIntensity(0.01); //don't use 0 or lag on switch
          game.GetWorld().GetObject("lamp").GetLight("Light2").SetIntensity(0.01); //" "
          game.GetWorld().GetObject("lamp").GetLight("Light3").SetIntensity(0.01); //" "
          GetGameManager().GetWorld().SetAmbientColour(0,0,0); //whole numbers only 0-255 0,1,2...
          LightsPressed = false;

          return true;
         }
      }
   }
   return false;
}
Logged
gekido
Guest
« Reply #6 on: August 25, 2008, 03:44:17 PM »

just a quick note about the original post:

Quote
GlobalLight 1
AddLight Flashlight
LightType Spot
LightConeAngle 15
LightEdgeAngle 5
LightColour 255 255 255
LightIntensity 1.2
LightShadowType ShadowMap
LightSpecular 1.0
LightFalloff Linear 1.5
LightPosition 0.0 1.3 0.00

you don't necessarily need to have the ShadowType set to ShadowMap - if you are running into performance issues, you can simply leave the shadowtype to 'none' and still get the same kind of effect.

just an fyi ;}
Logged
Ransom
Full Member
***
Posts: 132



View Profile
« Reply #7 on: August 25, 2008, 05:23:55 PM »

true, true.  But, it looks much sweeter if your system can can handle it Wink
Logged
Squat
Hero Member
*****
Posts: 592


View Profile
« Reply #8 on: September 04, 2008, 10:05:19 PM »

Started playing with lights again. It's true that it's much much sweeter with shadows on. However, I would take care to try and eliminate all other shadow casting from any other lights whenever the flashlight goes on. Easy enough.

I was wondering if there was a cleaner and easier way to get the flashlight to follow the player's view. Perhaps attach it to his head bone and have that take mouse inputs or something?
Logged
Ransom
Full Member
***
Posts: 132



View Profile
« Reply #9 on: September 05, 2008, 03:22:48 AM »

What camera point of view are you thinking of?  For a FPS, the above example is about as "clean" as it gets as far as setting the light's rotation to match the player's.  Two lines.  Get the light, set it's angle.  The other stuff is simply for enabling an "on/off" button.
Logged
Squat
Hero Member
*****
Posts: 592


View Profile
« Reply #10 on: September 05, 2008, 12:55:15 PM »

I might have implemented it completely wrong. I did this as one of the first things when getting the engine. I had no idea what was going on. My end result was the light followed the player's left and right view only, I couldn't get the up and down. Also, the left and right was only happening when I changed directions with the keys so if I held forward and moused to turn left, the light would stay that way until i repressed forward and then it went ahead of me. Again, I'm gonna retry this before I start confusing the matter. Looking at the code, i must have mucked something up. I might have also been using a really old character.gsl. I'm on a new version of the project so let me try again and I'll report what I see.
« Last Edit: September 05, 2008, 01:00:46 PM by Squat » Logged
Ransom
Full Member
***
Posts: 132



View Profile
« Reply #11 on: September 05, 2008, 01:16:00 PM »

my version of character.gsl is an older one too, but my script should work with the newer version as well.  The variable "lookUpDown" is something I've assumed is included in your character.gsl , but if it wasn't, the script shouldn't work at all, it would throw an error when it tried to set the rotation.

Yeah, I would suggest trying to set up my script again from the beginning, following the instructions.  For me, and apparently the users who've responded to this post, it works quite well.  Light rotates with the mouse - up, down, left, and right and turns on/off when pressing the "F" key.

Logged
Squat
Hero Member
*****
Posts: 592


View Profile
« Reply #12 on: September 05, 2008, 04:04:08 PM »

That's good to hear how it's supposed to go. Still haven't gone back to it, but I won't keep you waiting. Im certain that it works but I'll confirm that in a little bit here.

I would like to discuss lights some, particularly how best to handle turning them on and off. I have noticed a terrible freeze whenever a light either goes from disabled to enabled or from an intensity of 0 to above that. Shutting them off doesn't seem to be an issue. I think an intensity of 0 is triggering the engine to disable the light and unload it, so it reloads it when it comes back on. My lightswitch code, if set to 0 in the off state, has like a 2 second pause almost every time when turned on.

The way I've gone about alleviating the issue for myself is to set lights to an intensity of 0.001 such that they appear to be off but are still cached. As far as I can see, it's a perfect workaround. Not a split second of lag when it comes on and the light really looks like it's off. I would imagine setting the color to black might work the same.
Logged
Ransom
Full Member
***
Posts: 132



View Profile
« Reply #13 on: September 05, 2008, 07:05:50 PM »

In my flashlight script I simply use the command "light.SetEnabled(bool)", but I notice no lag at all turning it off and on.  Perhaps there is something about playing with the light's intensity that is more processor expensive?

If all you're trying to do is have the light appear to be off, why not use SetEnabled as well, instead of trying to adjust the intensity?  Or are you saying it makes no difference for you to use SetEnabled?
Logged
Squat
Hero Member
*****
Posts: 592


View Profile
« Reply #14 on: September 06, 2008, 05:05:30 PM »

I just wanted to report back that I still couldn't get this to work.

In the end, it only tracked left and right mouse when I was walking backwards. If I walked forwards, I'm pretty sure it was doing that behind me. It's facing the wrong way. It never picked up the up and down mouse and I see it defined in the rest of the script. The F key also did nothing to turn the light on or off. That's exactly the same as before. Oh well, whatever the problem is lies in the fact that I'm juggling about 11 different character.gsl files in trying to learn it all. Eventually I'm going to start again from scratch on the final versions and maybe then it'll work. Until then, I had planned on matchsticks with area lights as torches anyway. No angle necessary.

Please explain this line: light.SetRotationEuler(Vector( 0, 180 + lookUpDown , 0));

It appears to hold the entire key to the movement of the light
« Last Edit: September 06, 2008, 06:01:54 PM by Squat » Logged
Pages: [1] 2
  Print  
 
Jump to:  

 
Powered by MySQL Powered by PHP bluBlur Skin © 2006, hbSkins
Powered by SMF 1.1.14 | SMF © 2006-2011, Simple Machines LLC
Valid XHTML 1.0! Valid CSS!