Jump to content
DigitalHax

Lua door help

Recommended Posts

Hey everyone,

 

I am currently working on some objects in lua for use in the editor levels, and I am working on a door to open when e is pressed. The activation is fine, and it plays a sound, but I can't get any rotation to work on the door. I was hoping that someone could give me some help, as I know almost nothing about Lua.

 

Here is the lua file.

 

 

require("scripts/class")
require("scripts/math/math")

local class=CreateClass(...)

class.sound_dooropen=LoadSound("abstract::metaldoorOpen.wav")

function class:InitDialog(grid)
self.super:InitDialog(grid)
local group=grid:AddGroup("Door")
local messages="|Activate,Enable,Hide,Set Key,Run Code"
group:AddProperty( "Limits", "", PROPERTY_VEC2 )
group:Expand(1)
end

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

function object:UpdateState()
if self.active==1 then
//Door is closed
else
//Door is open

end
end

function object:SetKey(key,value)
if key=="torque" then
self.torque = tonumber( value )
elseif key=="limits" then
self.limits=StringToVec2(value)
if self.limits.x>self.limits.y then
self.limits.x = -360.0-self.limits.x
end
if self.joint~=nil then
self.joint:SetLimits(self.limits.x,self.limits.y)
end
else
return self.super:SetKey(key,value)
end
return 1
end

function object:ClearJoint()
if self.joint~=nil then
self.joint:Free()
self.joint=nil
end
if self.body~=nil then
self.body:Free()
self.body=nil
end
collectgarbage(collect)
end

function object:UpdateJoint()
local pos
pos=self.model:GetPosition(1)
self:ClearJoint()
self.body = CreateBodyPivot()
self.body:SetPosition(pos)
self.joint = CreateJointHinge(self.body,self.model,pos,self.model.mat:J())
self.joint:SetLimits(self.limits.x,self.limits.y)
end

function object:Init()
self.limits=Vec2(0,90)
self.model:SetKey("limits","0,90")
self.model:SetKey("mass",1)
self.model:SetKey("collision",COLLISION_PROP)
self:UpdateJoint()
if self.class.ambientnoise~=nil then
self.source=self.model:EmitSound(self.class.ambientnoise,40,0,1)
end
end

function object:GetKey(key,value)
if self==nil then return value end
if key=="torque" then
return self.torque
else
return self.super:GetKey(key,value)
end
end

function object:Reset()
self:UpdateJoint()
end

function object:Free(model)
self:ClearJoint()
self.super:Free()
end

function object:ReceiveMessage(message,extra)
local n
local target
local resettime=tonumber(self.model:GetKey("resettime","0"))
local args
local delay

if message=="reset" then
self.model:SetKey("state",0)
end

if message=="use" then
if resettime>0 then
if self.state==1 then
return
end
end

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

--Just set the key and everything will get updated automatically
self.model:SetKey("active",1-self.active)

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("activate",nil,delay)
else
target:SendMessage("deactivate",nil,delay)
end
end
end

if resettime>0 then
self.model:SendMessage("reset",nil,resettime)
end
end
end


object:Init()
end

 

What would I need to get the door to turn?

 

Thanks in advance to anyone who can give me some help.

Share this post


Link to post

Since you are using a door with physics then add a force to it... or just rotate the door with RotateEntity to give you more control on opening and closing, but if you are going to do that then there is no reason to use physics with the door.

Share this post


Link to post

I use a very crude method. I have two doors, one set in the open door position(placed in the editor but invisible ) the closed door is placed in code. When a key is pressed the closed door becomes invisible with its entitytypechanged from 1 t to 0 and the open version becomes visible.

Share this post


Link to post

Thanks for the replies.

And yeah I of course tried rotate entity first, but I guess due to my lack of knowledge in lua I am having troubles getting it working. It keeps crashing :(

 

Anyway I will keep fussing around with it. I am bound to get something to work :)

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

×
×
  • Create New...