lxFirebal69xl Posted March 1, 2015 Share Posted March 1, 2015 Aggror and I have been making a custom FPSPlayer script for A Demon's Game for quite a while now, I recently ran into a 2 weird problems, whenever I pressed E and dind't have an entity in front of me the game crashes. And the second problem is that the FPSPlayer Script can't use entities that have a function within a function...for example, the swinging door script has an Use() with a self:open() and a self:close(). The FPSPlayer script can't do that, I've messed around with it and made it so that the Use() function of the door only did what the Open() function had, and it worked. If anyone has any thoughts on what the problem might be, I would greatly appreciate it! Error that shows up when I press E without looking at an entity. The script I'm using (Contains Aggror's FlowGUI) FPSPlayerInventory New.lua Quote Link to comment Share on other sites More sharing options...
nick.ace Posted March 1, 2015 Share Posted March 1, 2015 I had this issue as well. To solve this, you can surround that code with this: pcall( function() --whatever is causing the error end) This is how you can do exception handling in Lua. Exception handling (idk what your knowledge of this is) means that the code won't raise any errors but will silently fail. Basically, the pcall() method does this, but you have to but an anonymous function()...end inside of it. That part seems to be more of a syntax thing. Don't do this every frame though since it's expensive. You should only do this for the code inside the pick (or I guess on each KeyHit() isn't too bad either). This is useful if you are trying to access something that not everything in the scene has. Quote Link to comment Share on other sites More sharing options...
lxFirebal69xl Posted March 1, 2015 Author Share Posted March 1, 2015 I've added the code, and while it indeed fixed the issue (thanks a ton) another error showed up. And this one can't be solved via the exception handling, I tried it and it dind't work Quote Link to comment Share on other sites More sharing options...
YouGroove Posted March 1, 2015 Share Posted March 1, 2015 Verify by code pickInof.entity is not nill and pickInfo.entity:GetMass() returns a value displaying it on screen for example. The error says you try to compare a number with nil , so one of the elemenst of the comparison is nil. Quote Stop toying and make games Link to comment Share on other sites More sharing options...
lxFirebal69xl Posted March 1, 2015 Author Share Posted March 1, 2015 Verify by code pickInof.entity is not nill and pickInfo.entity:GetMass() returns a value displaying it on screen for example. The error says you try to compare a number with nil , so one of the elemenst of the comparison is nil. Can you say that again but this time like you're speaking to a donkey? 1 Quote Link to comment Share on other sites More sharing options...
YouGroove Posted March 1, 2015 Share Posted March 1, 2015 if mass ~= nil and self.maxcarryweight ~= nil then if mass >0 and mass < self.maxcarryweight then ... ... end end There are better ways, but this should make your code more secure. Quote Stop toying and make games Link to comment Share on other sites More sharing options...
lxFirebal69xl Posted March 1, 2015 Author Share Posted March 1, 2015 if mass ~= nil and self.maxcarryweight ~= nil then if mass >0 and mass < self.maxcarryweight then ... ... end end There are better ways, but this should make your code more secure. Thank you, that fixed it! But there's another problem Everytime I try to use an entity with a Use() function it doesn't work if the entity has another function in it. Example: function Script:Use() --Only allow this if the object is enabled if self.enabled then if self.openstate then --Make the door close self:Close() else --Make the door open self:Open() end end end It works if the code is like this in the Use() function. This is quite weird. if self.enabled then self.opentime = Time:GetCurrent() if self.openstate==false then self.openstate=true if self.opensound then self.entity:EmitSound(self.opensound) end self.joint:SetAngle(self.openangle) self.component:CallOutputs("Open") end end end Quote Link to comment Share on other sites More sharing options...
lxFirebal69xl Posted March 8, 2015 Author Share Posted March 8, 2015 Thank you, that fixed it! But there's another problem Everytime I try to use an entity with a Use() function it doesn't work if the entity has another function in it. Example: function Script:Use() --Only allow this if the object is enabled if self.enabled then if self.openstate then --Make the door close self:Close() else --Make the door open self:Open() end end end It works if the code is like this in the Use() function. This is quite weird. if self.enabled then self.opentime = Time:GetCurrent() if self.openstate==false then self.openstate=true if self.opensound then self.entity:EmitSound(self.opensound) end self.joint:SetAngle(self.openangle) self.component:CallOutputs("Open") end end end A week later and I still haven't got this thing to work correctly. Quote Link to comment Share on other sites More sharing options...
Josh Posted March 8, 2015 Share Posted March 8, 2015 You are setting breakpoints and walking through the code, right? You should be able to examine the object in the debugger and see whether it has those functions attached to it. 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...
lxFirebal69xl Posted March 8, 2015 Author Share Posted March 8, 2015 I got it working today, I copied over the part of the default FPSPlayer script over to the new one and made changes to it. So now it looks like this. if window:KeyHit(Key.E) then if self.carryingEntity then self:DropEntityCarrying() else local p0 = self.camera:GetPosition(true) local p1 = Transform:Point(0,0,self.useDistance,self.camera,nil) if self.entity.world:Pick(p0,p1, pickInfo, 0, true) then -- RLP Begin: if this object has text to display then set it to be displayed if pickInfo.entity ~= nil then if pickInfo.entity.script ~= nil then if pickInfo.entity.script.GetText ~= nil then self.text = pickInfo.entity.script:GetText() self.DisplayTime = Time:GetCurrent() + self.DisplayTimeMax end end end -- RLP End --Looks for any entity in the hierarchy that has a "Use" function local usableentity = self:FindUsableEntity(pickInfo.entity) if usableentity~=nil then --Use the object, whatever it may be usableentity.script:Use(self) else if self.carryingEntity == nil then mass = pickInfo.entity:GetMass() --Pick up object if it isn't too heavy if mass>0 and mass<=self.maxcarryweight then self.carryingEntity = pickInfo.entity self.carryingobjectcollisiontype = self.carryingEntity:GetCollisionType() self.carryingEntity:SetCollisionType(Collision.Debris) self.carryrotation = Transform:Rotation(pickInfo.entity:GetQuaternion(true),nil,self.camera) self.carryposition = Transform:Point(pickInfo.entity:GetPosition(true),nil,self.camera) end end end end end end And it previously looked like this. if window:KeyHit(Key.E) then if self.carryingEntity then self:DropEntityCarrying() else local p0 = self.camera:GetPosition(true) local p1 = Transform:Point(0,0,self.useDistance,self.camera,nil) if (self.entity.world:Pick(p0,p1, pickInfo, 0, true,Collision.Prop)) then --Looks for any entity in the hierarchy that has a "Use" function local usableentity = self:FindUsableEntity(pickInfo.entity) if usableentity~=nil then self.a = 1 usableentity.script:Use(self) --Use the object, whatever it may be usableentity.script:Use(self.entity) end elseif self.carryingEntity == nil then mass = pickInfo.entity:GetMass() --Pick up object if it isn't too heavy if mass>0 and mass<=self.maxcarryweight then self.carryingEntity = pickInfo.entity self.carryingobjectcollisiontype = self.carryingEntity:GetCollisionType() self.carryingEntity:SetCollisionType(Collision.Debris) self.carryrotation = Transform:Rotation(pickInfo.entity:GetQuaternion(true),nil,self.camera) self.carryposition = Transform:Point(pickInfo.entity:GetPosition(true),nil,self.camera) -- RLP Begin: if this object has text to display then set it to be displayed if pickInfo.entity ~= nil then if pickInfo.entity.script ~= nil then if pickInfo.entity.script.GetText ~= nil then self.text = pickInfo.entity.script:GetText() self.DisplayTime = Time:GetCurrent() + self.DisplayTimeMax end end end -- RLP End end end end end end Quote 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.