May 23, 2012, 01:32:00 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]
  Print  
Author Topic: Code snippets  (Read 486 times)
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« on: June 03, 2009, 05:04:27 PM »

I wish there was sub-folders for SCRIPTING with COMPLETED SCRIPTS and WIP SCRIPTS. We could then give the community some code snippets to make things easier. I will add something here to show what I mean:

Quote
--SHOW FPS--

Author: Patrik aka pixellegolas
Rights: Included in old BV, use as you like without any credits

Explanation:
This code shows the FPS of the game at runtime. Seems though that it is showing the Hz of the screen
instead sometimes

File attached


This could then be expanded and hopefully people will get some snippets to use in games, like turning on a lamp with a key or similar
Logged
Squat
Hero Member
*****
Posts: 592


View Profile
« Reply #1 on: June 04, 2009, 01:32:41 PM »

LampSwitch.gsl -- Basic script to enter an input area and use a custom action key to turn off and on a particlar light in the world. The light is added directly to the lamp object and the calls are based on it being a child of that. The action key "lights" is a custom action I created and assigned myself in my inputs.cfg. If you have trouble or need the lines to get that functioning, please let me know but you can easily change "if (params.stringValue == "Lights")" to "== "Action" or "== PickUp". This also contains a delay snippet from Zknack which I've been basically permanently using.

I set custom values for the lights themselves, but you can easily expand this to allow for controller variables such that multiple lights with multiple values can be used with the same basic script.

Please note that it's a great idea to not turn off dynamic lights all the way either to 0 intensity or by way of deactivation. The reason is that when it comes back on, a lot is recalculated. If you set the intensity to 0.01, the calculations are set and left alone as there is no "reactivate" happening on the light source. Basically, this way there is 0 noticable hickup or lag when activating the lights...even multiple lights attached to the same object.

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; //if read, shut off
            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(15,15,0); //whole numbers only 0-255 0,1,2...
             LightsPressed = false;

          return true;
         }
      }
 
   }
   return false;
}
Logged
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« Reply #2 on: June 04, 2009, 02:07:10 PM »

that is great. I was just planning on modeling my apartment and make a walkthrough with nice ambient lightning. I also planned on a lightswitch and BAM, here it is Smiley
Logged
zknack
Full Member
***
Posts: 207


View Profile
« Reply #3 on: June 05, 2009, 03:16:15 PM »

Glad the delay code is helping, it was done due to 'pickup & throw' scripting speed issues,
although it's proven handy for spawners and such for me- and your excellent idea here.

Well done Sir :-)

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


View Profile
« Reply #4 on: June 05, 2009, 04:40:06 PM »

Lightning.gsl -- This is the same code I wrote for the lightning strikes in my demo. You'll notice it's taking a controller variable of either "calm" or "STORM". That is sent from elsewhere in another script and you can see it's defaulted to STORM being off. The script should work as-is, but if you wanted to ramp up the lightning frequency during an event, you can do that simply by sending the controller variable from any other script. This script is attached to an invisible dummy object named "lightning" within the world which has one light added to it named "light". I believe I used a directional with no shadows.

You will also need to add your own sounds. The rain is a 3D sound with a physical source because in my demo you can only hear it when you're near the window. The lightning is throughout the entire world. You should be able to tailor that easy enough and add as many sounds as you wish.

Quote
float MaxDelay;
float MinDelay;
float Delay;
float myDelay;

float SoundDelay;
float mysoundDelay;

int ambient;

int MinStrikes;
int MaxStrikes;
int myStrikes;
int Strikes;
//Object@ light;

float LightBright;

bool duringstrike;
bool SoundPlay;

bool STORM=false;
bool calm=false;

Sample ThunderSound1;
Sample ThunderSound2;
Sample ThunderSound3;
Sample RainSound;

int ThunderSound;

void SetControllerVariable( Object@ object, const String& in name, bool value)
{
    if (name == "STORM")
    {
        print("storm");
        MinDelay = 0.1;
        MaxDelay = 9;
        MinStrikes = 2;
        MaxStrikes = 6;
        //~ Delay = RandomFloat(MinDelay, MaxDelay);
        //~ myStrikes = RandomInt(MinStrikes, MaxStrikes);
    }
    if(name== "calm")
    {
        print("calm");
        MinDelay = 1;
        MaxDelay = 30;
        MinStrikes = 1;
        MaxStrikes = 3;
        //~ Delay = RandomFloat(MinDelay, MaxDelay);
        //~ myStrikes = RandomInt(MinStrikes, MaxStrikes);
    }
    return;
}

void Initialize( Object@ object)
{

    SoundDelay = 3;
    mysoundDelay =0;
    SoundPlay = false;
   
    ambient = 10;
    MinDelay = 1;
    MaxDelay = 30;
    MinStrikes = 1;
    MaxStrikes = 3;
    Strikes = 0;
    //@light = GetGameManager().GetWorld().GetObject("Spotlight15");
    duringstrike = false;
    LightBright = 0.01;
    Delay = RandomFloat(MinDelay, MaxDelay);
    myDelay=0;   
    myStrikes = RandomInt(MinStrikes, MaxStrikes);   
   
    ThunderSound1.Load("Thunder1.wav");
    ThunderSound1.SetVolume(3);
    ThunderSound1.SetLooped(false);
    ThunderSound1.SetMinDistance(100);
   
    ThunderSound2.Load("Thunder2.wav");
    ThunderSound2.SetVolume(3);
    ThunderSound2.SetLooped(false);
    ThunderSound2.SetMinDistance(100);
   
    ThunderSound3.Load("Thunder3.wav");
    ThunderSound3.SetVolume(3);
    ThunderSound3.SetLooped(false);
    ThunderSound3.SetMinDistance(100);
   
    RainSound.Load("Rain.wav");
    RainSound.SetVolume(1);
    RainSound.SetLooped(true);
    Vector RainPos = GetGameManager().GetWorld().GetObject("RainObject").GetPosition();
    RainSound.SetPosition(RainPos);
    RainSound.SetMinDistance(2);
    RainSound.Play();
}

bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
{

        //~ Object@ light = GetGameManager().GetWorld().GetObject("Spotlight15");
        //~ Object@ PoLightON = GetGameManager().GetWorld().GetObject("PoLightON");

return false;

}

void DynamicsAdvance( Object@ object, float seconds)
{
    GameManager@ game = GetGameManager();
    if(duringstrike)
    {
        if(Strikes>=myStrikes)
        {
            LightBright=0.01;
            ambient=10;
            game.GetWorld().GetObject("Lightning").GetLight("Light").SetIntensity(LightBright);
            GetGameManager().GetWorld().SetAmbientColour( ambient,ambient,ambient);
            myStrikes=RandomInt(MinStrikes, MaxStrikes);
            Strikes=0;
            duringstrike=false;
        }
        else
            if(LightBright<=0.01)
            {
                LightBright=3;
                ambient = 255;
                //game.GetWorld().GetObject("Lightning").GetLight("Light").SetIntensity(LightBright);
                Strikes++;
                SoundPlay=true;
            }
            else
            {
                LightBright-=seconds*2;
               
                if(ambient<=10)
                ambient=10;
                else
                ambient-=5;
               
               
                game.GetWorld().GetObject("Lightning").GetLight("Light").SetIntensity(LightBright);
                GetGameManager().GetWorld().SetAmbientColour( ambient,ambient,ambient);
            }
           
    }
   
    if(!duringstrike)
    {
       
        if(myDelay>=Delay)
        {
            duringstrike=true;
            myDelay = 0;
            Delay = RandomFloat(MinDelay, MaxDelay);
        }
        else
        {
            myDelay+=seconds;
        }
    }
   
    if(SoundPlay)
    {   
        if(mysoundDelay>=SoundDelay)
        {
            Vector SoundPos=GetGameManager().GetWorld().GetObject("player").GetPosition();
           
            ThunderSound=RandomInt(1,3);
            if(ThunderSound==1)
            {
            ThunderSound1.SetPosition(SoundPos);
            ThunderSound1.Play();
            }
             if(ThunderSound==2)
            {
            ThunderSound2.SetPosition(SoundPos);
            ThunderSound2.Play();
            }
             if(ThunderSound==3)
            {
            ThunderSound3.SetPosition(SoundPos);
            ThunderSound3.Play();
            }
            //print(ThunderSound);
            mysoundDelay=0;
            SoundPlay=false;
        }
        else
        {
            mysoundDelay+=seconds;
        }
    }
}
Logged
Pages: [1]
  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!