AggrorJorn Posted January 26, 2010 Share Posted January 26, 2010 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 Quote Link to comment Share on other sites More sharing options...
VicToMeyeZR Posted January 26, 2010 Share Posted January 26, 2010 do you have a link between your switch object and your light? Quote AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD Link to comment Share on other sites More sharing options...
VicToMeyeZR Posted January 26, 2010 Share Posted January 26, 2010 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. Quote AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 26, 2010 Author Share Posted January 26, 2010 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? Quote Link to comment Share on other sites More sharing options...
Rick Posted January 26, 2010 Share Posted January 26, 2010 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). Quote Link to comment Share on other sites More sharing options...
VicToMeyeZR Posted January 26, 2010 Share Posted January 26, 2010 no. Class.lua should not have to be modified at all. I could be wrong, but you should SetColor which has intensity. Quote AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD Link to comment Share on other sites More sharing options...
VicToMeyeZR Posted January 26, 2010 Share Posted January 26, 2010 try this. self.light:Hide() to be more specific function object:ReceiveMessage(message,extra) if message=="show" then self.light:show() elseif message=="hide" then self.light:hide() Quote AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 26, 2010 Author Share Posted January 26, 2010 OK thanks for your answers. I'll try this first and will let you know what the results are. Quote Link to comment Share on other sites More sharing options...
VicToMeyeZR Posted January 27, 2010 Share Posted January 27, 2010 how did this work out for you? Quote AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD Link to comment Share on other sites More sharing options...
Josh Posted January 30, 2010 Share Posted January 30, 2010 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. Quote 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 More sharing options...
AggrorJorn Posted January 30, 2010 Author Share Posted January 30, 2010 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 Quote Link to comment Share on other sites More sharing options...
Josh Posted January 30, 2010 Share Posted January 30, 2010 You can also make the player script just call something like switch:Use(). This was the whole point of the single/multi-state hiccup we experienced early on. Quote 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 More sharing options...
Recommended Posts
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.