Jump to content

FPS Player trouble.


lxFirebal69xl
 Share

Recommended Posts

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.

post-13811-0-05735500-1425171619_thumb.png

 

 

 

The script I'm using (Contains Aggror's FlowGUI)

FPSPlayerInventory New.lua

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Stop toying and make games

Link to comment
Share on other sites

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?

  • Upvote 1
Link to comment
Share on other sites

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 :P

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

Link to comment
Share on other sites

Thank you, that fixed it!

 

But there's another problem tongue.png

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.

Link to comment
Share on other sites

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.

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

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

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