Jump to content

Light Intensity and "For" loops. -= NEWB=-


GSFC_1
 Share

Recommended Posts

Hey guys, trying to learn to program better. Made a little 3d lunar lander game, but i want a light to act as a plume illuminator when i hit "UP" button.  I am going to try to learn to increment a value over time using the for loop, so, I would like it to have SetIllumination(.01) at first, but then over the course of 100 frames, have it add .1 to the SetIntensity() until I call a break. 

So in the scene, there are 3 objects:

    1. A Box - (My Lunar Lander)
    2. A Point light (child of the box, used to illuminate the ground when i hold the "L" button)
    3. A ground plane. (its the ground)

I have set the world gravity to .1 . 

Lander(Box) starts about 10 feet above the ground, immediately starts falling and i hit the L Button. Script attached to the box says :
--------------------------------------------------------------------------------------------------

function Script:UpdatePhysics()
    --local window = Window:GetCurrent()

C_LOC = self.entity:GetPosition()
C_ROT = self.entity:GetRotation()

    if window:KeyDown(Key.L) then self.entity:AddForce(0,0,-1,false) end
    if window:KeyDown(Key.E) then self.entity:AddTorque(0,0,-25,false) end
    if window:KeyDown(Key.Q) then self.entity:AddTorque(0,0,25,false) end
    if window:KeyDown(Key.A) then self.entity:AddForce(-1,0,0,false) end
    if window:KeyDown(Key.D) then self.entity:AddForce(1,0,0,false) end
    if window:KeyDown(Key.W) then self.entity:AddForce(0,1,0,false) end
    if window:KeyDown(Key.S) then self.entity:AddForce(0,-1,0,false) end

    --System:Print(C_LOC)
    --System:Print(C_ROT)
end

----------------------------------------------------------------------------------------

This works, My little box flies around

now, the light on the bottom of my spacecraft.

This has a script that looks for when the L button is being pressed and it sets the Light Intensity with the SetIntensity() command. This script looks like so: 

---------------------------------------------------------------------------------------------

 

--Script.limits: 0.0, --float Max thust


function Script:Start()
        --entity = self.entity
        --light_int = entity:GetIntensity() --float
        --entity.Intensity = 0.0 --float 
end


function Script:UpdateWorld()
    if window:KeyDown(Key.L) == true then 
        
        for n=(light_int), 100, 1 do 
            entity:SetIntensity((light_int)+.1)
        end
        System:Print("Engine ON")
        entity:SetIntensity(1.0) 
        
    else
        self.entity:SetIntensity(0.10)
        System:Print("Engine OFF")
        
    end
end


------------------------------------------------------------------------------------------------------------------------------

This works, works well. I'm better than buzz, look out Kerbal, GSFC Space Program is coming to a leadwerks capable machine near you.

But i want the light, when the engine is not firing, to stay at Intensity(0). But when the engine is fired, I would like the intensity to grow to a value of 1.0 by incrementing 0 by .1. When I let go of the engine button (L), I would like it to access its current value, then subtract .1 per frame until it comes to zero again. 

I know my code looks bad and there is erroneous garbage in there and such, but, i am on like ...... day 4. Please don't crucify me. 

Any ideas would be great. 

Respectfully

GSFC
 

Link to comment
Share on other sites

Would this "Notional Throttle Variable" be better placed on the object or in the main.lua? 

As of right now, the engine is only correllated to the intesity of the light, the engine itself, (like most rocket fuel sources) is not throttle-able. It is on or off, but there is a growth to its impulse and intensity. I essentially want the glow to have a falloff applied to it, so that its intensity ramps in. 

Link to comment
Share on other sites

Let's handle the engine on/off state first.  I assume torque or force correlates to engine on, so we need a variable for the engine being on or off.  Let's restructure your code a bit:

local force = 0
local torque = 0
if window:KeyDown(Key.W) then force = force + 1 end
if window:KeyDown(Key.S) then force = force - 1 end
if window:KeyDown(Key.A) then torque = torque + 1 end
if window:KeyDown(Key.D) then torque = torque - 1 end
self.entity:AddForce(force)
self.entity:AddTorque(torque)
self.enginerunning=false
if force~=0 or torque~=0 then self.enginerunning = true end

if self.enginerunning then
	light:SetIntensity(1)
else
	light:SetIntensity(0.25)
end

 

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Yes, This ^^^ Thank you Josh. 

I was trying to tackle it one small problem at a time, just getting the light to turn on for right now. But this has organized it a lot better. Should this go on the object or in the main script though?

Let me give this a shot and thank you again. 

 

Link to comment
Share on other sites

I can see now, that your code was very top level code. But the structure can be applied to my code. I am still struggling deciding when a variable should be object based or should be a globally shared variable that can be tracked. I guess its a theoretical question that just requires trial and error. 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...