Jump to content
Raxe88

Attempting to call a method of an entity from another entity

Recommended Posts

This is my code:

 

DeathTrigger.lua (attached to a box)

Script.respawnPoint = "" --Entity "respawn point"
function Script:Collision(entity, position, normal, speed)
if(entity:GetKeyValue("name") == "Player") then
spawnPos = self.respawnPoint:GetPosition()
--entity:SetPosition(spawnPos)
entity:Respawn(spawnPos)
end
end

 

and

 

FPSPlayer.lua (attached to the player)

function Script:Respawn(spawnPos)
self.entity:SetPosition(spawnPos)
end

 

When they collision I get this error:

 

3606c60f7a.png

 

I have a Pivot indicating the respawn point:

10a875d5f0.png

 

Line 65 is this from DeathTrigger.lua:

 

entity:Respawn(spawnPos)

 

From my point of view it should work. Any help?

 

Edit: I just found it out. Turns out that this works:

entity.script:Respawn(spawnPos)

 

A little explanation would be welcome tongue.png

Share this post


Link to post

The entity and a script attached to that entity are 2 different things. You can get either from both.

 

entity.script or (inside the script) self.entity (note that when inside a script 'self' refers to the script itself NOT the entity)

 

The collision callback is returning to you the entity NOT the script. So in order to call the script functions attached to the entity you need to access the script variable of the entity.

 

entity.script:Respawn()

 

Now entities do have functions (as you see in entity:SetPosition()) but those are C++ engine commands on the entity. The scripts are different than the built-in engine functions. This whole script thing is something that didn't exist in the early days of LE but something like SetPosition() has existed since day 1. Scripting functionality was added later after the core engine functions were already there.

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