Jump to content

Toglle Light on and off


AggrorJorn
 Share

Recommended Posts

Does anyone have an example regarding turning a light on and off via messages?

 

I've been trying to get this to work by fooling around with the switch.lua for a while now, but sofar no good. I'm looking for a script that eihter sends a 'show' or 'hide' message. The light that can be turned on and of is created inside a lamp. Would anyone be so kind to help me out with this.

 

This is the code of the lamp:

require("scripts/class")

local class=CreateClass(...)

function class:CreateObject(model)
local object=self.super:CreateObject(model)

--Create light
self.light=CreatePointLight(1,model)
self.light:SetColorf(1,0.4,0.2,1,1)
self.light:SetPositionf(0,0.1,-0.25,0)
self.light:SetShadowOffset(0,0.80,0)

end	
end	

Link to comment
Share on other sites

if your switch object is the lamp, then you need to link the lamp with the light.

 

Then you can add the lua code parts that you need from the switch.lua file to your lamp lua file, and you will have the ablility to send the message to the light.

AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD

76561197984667096.png

Link to comment
Share on other sites

Yes I have. the light switch code is identical to the switch that comes with the sdk. I'm only using a different message.

local n,target,targetmessage,args,delay
		for n=0,7 do
			target=self.model:GetTarget(n)
			if target~=nil then
				delay = tonumber( self.model:GetKey("delay"..n,"0") ) * 1000.0
				if self.active==1 then
					target:SendMessage("Hide",nil,delay)
				else
					target:SendMessage("Show",nil,delay)
				end
			end
		end

 

The lamp scriptshould should then something with these messages like:

function object:ReceiveMessage(message,extra)

	if message=="show" then
                       self.light:SetIntensity(2)
               else
                       self.light:SetIntensity(0)

 

Would I need to configure these messages in the class.lua first?

Link to comment
Share on other sites

I don't know what's in switch.lua so this may be what's in there, but what I would do is the following:

 

In the switch Initialize() lua method, you can see my Pi-Main on how I did that, find the player object and store that off in the switch lua data. Now your switch has a reference to the player object. Then in the switch Update() method check if say KEY_E is hit. If it is check the distance between the player object and the switches target object, and if within a certain range send a "toggle" message to the target. It's then up to the lamp code to receive that "toggle" message and do whatever it should do with it.

 

Then the usage would be to add our lamp. Add this switch object. Make the lamp a target of this switch and place the switch anywhere you want (it could be a wall light if you like).

Link to comment
Share on other sites

Hide the light, don't set the color to pure black. A black light will still have to be rendered, even if its effect is invisible.

 

You also don't have to use messages at all, since its all a single Lua state.

 

It can be overwhelming to try to imagine all the possibilities these kind of systems might have, but it's not hard to implement simple relationships. This is exactly the kind of thing I wanted to see users start doing with script. :)

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

how did this work out for you?

Well I tried your way, and the the script in the switch was no correct. I think it would have worked, but I had the problem that light that was created inside the lamp, could shine through walls. If I would drag a regular lamp in screen, this didn't happen. Also I would like to leave the script for the light alone.

 

Hide the light, don't set the color to pure black. A black light will still have to be rendered, even if its effect is invisible.

 

You also don't have to use messages at all, since its all a single Lua state.

 

It can be overwhelming to try to imagine all the possibilities these kind of systems might have, but it's not hard to implement simple relationships. This is exactly the kind of thing I wanted to see users start doing with script. :)

 

I tried this and it worked! Instead of sending messages, I just used the show() and hide() commando's. This is script for the switch:

require("scripts/class")

local class=CreateClass(...)

class.sound_switch=LoadSound("abstract::switch.wav")

function class:CreateObject(model)
local object=self.super:CreateObject(model)

local lightOn=1

function object:ReceiveMessage(message,extra)

	local n
	local target
	local args
	local delay

	if message=="use" then

	if class.sound_switch~=nil then
		PlaySound(class.sound_switch)
	end

		local n,target,targetmessage,args,delay
		for n=0,7 do
			target=self.model:GetTarget(n)
			if target~=nil then
				if lightOn==1 then
					target:Hide()
					lightOn=0
				else
					target:Show()
					lightOn=1	
				end
			end	
		end	
	end

end
end

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...