Announcement

Collapse
No announcement yet.

How to Spawn/Create a Light Using UnrealScript and Set Color and Brightness

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to Spawn/Create a Light Using UnrealScript and Set Color and Brightness

    Dear Everyone,

    In case you wonder how I come up with tutorials, it is whenever I am trying to figure out how to do something and cannot find the info I need on the net, or only parts, and feel there should be a more complete resource available for the future.

    Thanking Spoof Yet Again
    I want to especially thank Spoof for posting about how to extend the LightMovable class to set bNoDelete.

    My own contributions:
    -figuring out how to set the Light Color and Brightness dynamically.
    -giving you easy way to turn light off and on
    -giving you easy way to set various properties like FallOffExponent and Radius of the light.
    -easy way to toggle CastDynamicShadows


    Enclosed is the complete class for your happy reference.


    Code:
    class JoyLight extends PointLightMovable;
    
    function turnOn() {
    	LightComponent.SetEnabled(true);
    }
    
    function turnOff() {
    
    	LightComponent.SetEnabled(false);
    }
    
    function setBrightness(float f) {
    	LightComponent.SetLightProperties(f);
            LightComponent.UpdateColorAndBrightness();
    }
    
    function setColor(byte ir, byte ig, byte ib, byte ia) {
    	local color c;
    	
    	c.R = ir;
    	c.G = ig;
    	c.B = ib;
    	c.A = ia;
    
    	LightComponent.SetLightProperties(,c); 
            LightComponent.UpdateColorAndBrightness();
    }
    
    //default 1024.0
    function setRadius(float r) {
    	PointLightComponent(LightComponent).Radius = r;
    }
    
    //default 2.0
    function setFallOffExponent(float e) {
    	PointLightComponent(LightComponent).FalloffExponent = e;
    }
    
    //default 2.0
    function setShadowFalloffExponent(float e) {
    	PointLightComponent(LightComponent).ShadowFalloffExponent = e;
    }
    
    //default 1.1
    function setShadowRadiusMultiplier(float f) {
    	PointLightComponent(LightComponent).ShadowRadiusMultiplier = f;
    }
    
    function setCastDynamicShadows(bool b) {
    	LightComponent.CastDynamicShadows = b;
    }
    
    //below is showing how to move the light
    //every light you spawn will gradually rise
    //if you uncomment this function
    //use similar code where/how you actually want
    //to move the light
    /*
    function tick(Float DT) {
    	super.tick(DT);
    	move(vect(0, 0, 1));
    }
    */
    DefaultProperties
    {
            bNoDelete = false
    	
            //for use with actor.move()
            bCollideActors = false
    	bCollideWorld = false
    }
    ~~~

    Creating the Joy Light


    Code:
    Local JoyLight p;
    
    p = Spawn(class'JoyLight',,, vect(34,1186,86190), rot(0, 0, 0), ,true );
    
    p.setBrightness(10);
    p.setColor(255, 0, 255, 255);
    
    //debug info
    `log("made a light!");
    
    //change vect and rot values according to where you are spawning light, 
    //or use location and rotation of existing object
    //last boolean in spawn is ensuring light spawns 
    //even inside other actor that has collision


    ~~~

    Moving the Light Once

    Use SetLocation(vector v) to move the light as you wish.

    ~~~

    Moving the Light Progressively Over Time

    You can also move the light progressively, as you would with Kismet Matinee,

    by using actor move() and using a small change vector (delta) in a timer function or a tick()

    See sample usage in main class above.

    ~~~

    Let Me Know If This Code Helps You

    Now you can easily dynamically create lights, move them, set their color and brightness, set values like radius and castdynamic shadows, and turn them off and on whenever you want!

    Please post if this code helps you so that others are more likely to see it!




    Rama

    #2
    Time to make torch ....

    Comment


      #3
      Ive been looking for somewhere to share this bit of code for a bit now, and this is a perfect fit.

      At first I hated the thought of interpolating things, then I did some thinking and came up with the below bits which make interping without a tick() a breeze. My reasoning is that I would rather not put something in a tick function. I figure I am not the smartest guy and if I can cut corners on processing, I can have my project be in better shape for the long run.

      So the first function below is called with, InterpTime, which is the total time you want to take to make the total change, in seconds. Framerate is how many increments you want in a second. Still works if you run in a fraction of a second interp time. This has a cap of 250 or so because of the Byte thing. The total change is the size of the number you want to change. I.E. You want to go from 0 to 60, your Total Change is 60.0. If you are going from 15.5 to -30, your total change is -45.5.
      It can be easily modified to synch 2 or more variables in the function. Just take in more 'TotalChange' type of variable and have it work out to a second increment holding variable in the setInterpSpeed. Then have that add to whatever it needs to in the actual interpolation function.

      I use this kind of interping pretty exclusively in my custom camera, it will synch camera yaw, and x, and y, positioning.

      I realize this is prolly not new, but it was new enough to me. I got the concept from the turret tutorial Heayah (Which barely made sense to me) And developed it until It was functional to me.

      The example below is for a pulsing light. (I'm kind of proud of how it loops) To run it with no looping: comment / delete the last two lines of code in LightPulseInterp.

      Code:
      function SetInterpSpeed(Float InterpTime, Byte FrameRate, float TotalChange)
      {
      	HowOftenToInterp = 1 / FrameRate; // turns fps into a frame duration
      	TimesToInterp = InterpTime / HowOftenToInterp;
      	BrightStep = TotalChange / TimesToInterp;
      }
      
      //resulting set timer call
      SetTimer(HowOftenToInterp,True,'LightPulseInterp');
      
      //Actual interpolating function
      function LightPulseInterp()
      {
      	LightStartBrightness+=BrightStep; //Increments LightStartBrightness Variable
      	LightComponent.SetLightProperties(LightStartBrightness); //Sets brightness of light to new value
      	KillTimer+=1; //increments killtimer
      
      	if(KillTimer==TimesToInterp)
      	{
      		ClearTimer('LightPulseInterp');
      		KillTimer=0; //resetting KillTimer For Further use 
      		BrightStep = BrightStep *-1; //Switches Sign before restarting timer
      		SetTimer(HowOftenToInterp,True,'LightPulseInterp'); //Calling itself with inverse value of brightstep
      	}
      }
      
      //shuts the light off
      function KillLight()
      {
      	ClearTimer('LightPulseInterp');
      	KillTimer=0;
      	LightComponent.SetLightProperties(0.0);
      	LightStartBrightness = DefaultLightBrightness;
      }
      
      
      defaultproperties
      {
              LightStartBrightness= 0.5;
      	DefaultLightBrightness = 0.5;
      }

      Comment


        #4
        Dear Slamm,

        Wow great stuff!

        Thanks for sharing the code!



        Rama

        Comment


          #5
          Dear Slamm,

          Wow great stuff!

          Thanks for sharing the code!



          Rama

          Comment


            #6
            Glad you like it. I'm always torn in my code, thinking 'this is absolutely brilliant and elegant' then thinking, well there is prolly a better way to do it. Id also like to take this post as a chance to thank you for your contributions to the community. I like like your enthusiasm and find it rather infectious.

            Comment


              #7
              Well chances are good if your code is helping you than it will certainly help others, and the only way we can build on each other's code to make even better code is if we share what we have so far!

              thanks for sharing!



              Rama

              Comment


                #8
                Very noice tut!!!

                Comment


                  #9
                  I am glad you like it!

                  I just updated the main class with the use of this super class function
                  Code:
                  LightComponent.UpdateColorAndBrightness();


                  Rama

                  Comment


                    #10
                    Originally posted by evernewjoy View Post
                    I just updated the main class with the use of this super class function
                    Code:
                    LightComponent.UpdateColorAndBrightness();
                    Where did you put this?

                    Thanks you for your contributions;evernewjoy and slamm
                    I always enjoy testing and playing other people's code and making sure I Always give credit where it is due.

                    -James

                    Comment

                    Working...
                    X