Jump to content

Just calcolate the distance using right syntax


aldoz
 Share

Recommended Posts

As in the title, I am trying to calcolate the distance between an object (sphere) in my scene and my FPSPlayer.

I am using this code as script of sphere object :

 

distance = GetDistance(Entity*FPSPlayer)

 

It's basic, I know... but I get all time same error : Script Error Attempt to perform operation on an invalid operand...

 

Help!

 

I tried this code I find in steam forum but in this case too I can't use the right syntax when I must change "obj1" and "obj2" with the LUA command..:

 

pos1 = GetPosition(Obj1)
pos2 = GetPosition(Obj2)
distance = length(pos2 - pos1)

Link to comment
Share on other sites

As in the title, I am trying to calcolate the distance between an object (sphere) in my scene and my FPSPlayer.

I am using this code as script of sphere object :

 

distance = GetDistance(Entity*FPSPlayer)

 

Syntax is wrong. should be:

distance = entity1:GetDistance(entity2)

 

http://www.leadwerks.com/werkspace/page/api-reference/_/entity/entitygetdistance-r800

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Syntax is wrong. should be:

distance = entity1:GetDistance(entity2)

 

http://www.leadwerks.com/werkspace/page/api-reference/_/entity/entitygetdistance-r800

 

Hi macklebee, sorry for my insistence;

considering entity1 name is FPSPlayer under Entity scene folder and..

considering entity 2 name is SPHERE under Models scene folder..

 

so, in your code how I need to name these objects (entity1 and entity2)?

 

and I need to set global variable distance?

Link to comment
Share on other sites

I try to explain better.

 

I use interface to make a new object, a ball.

Then I make a new object, a cube,

 

Now I go to object ball script and I use the following code to calculate the distance to the cube:

 

pos1 = self.entity:GetPosition()
pos2 = cube:GetPosition()

 

but pos2 get me a nil error.. like "cube" is not know by the ball script!

 

So you can see I need to call the object cube from the ball script but in this script there is NOT the creation of the cube object! (the object cube is born from Leadwerks interface, not by script code!)

 

I am a bit confused about..

:\

Link to comment
Share on other sites

I think we need to see a little more of the project. It sounds like you are making an object in the editor and expecting it to be available in a local script. The scope of cube is outside of the ball script.

 

There are a few ways you can do this. The most direct way would be to make a script for the cube and set it as a global variable. Then you can access cube like this. It matters where you call that distance code though, because if the cube global variable hasn't been set, then you will access an invalid object.

  • Upvote 1
Link to comment
Share on other sites

This is a common thing people ask about. To directly link an entity into your script you could do it by adding the following line of code to the top of your ball script.

 

Script.cube = nil --Entity "Cube"

 

With this code you can go into the editor and select your ball entity, then go to the script tab, there should now be a box labeled "Cube", drag the cube from the Scene tab into this box. Now within your script you could do something like

local distance = self.entity:GetDistance(self.cube)

 

There are many other ways to do this and without knowing more about what you are trying to accomplish, another method might be better.

  • Upvote 2
Link to comment
Share on other sites

I think we need to see a little more of the project. It sounds like you are making an object in the editor and expecting it to be available in a local script. The scope of cube is outside of the ball script.

 

Yeah exactly!

 

I was trying to use script proprierties;

Inside the ball script I add this command : Script.myEntity = entity --entity "Some entity"

 

So I get the textfield called "Some entity" where I could put an object to let this object is available in ball script but.... I can't choose nothing in that texfield! Seems to be disabled... (remember I am using Leadwerks demo)

 

So can I make a script on the cube where I set the cube proprierties as global.. cool.. but how I can do it?

Link to comment
Share on other sites

This is a common thing people ask about. To directly link an entity into your script you could do it by adding the following line of code to the top of your ball script.

 

Script.cube = nil --Entity "Cube"

 

With this code you can go into the editor and select your ball entity, then go to the script tab, there should now be a box labeled "Cube", drag the cube from the Scene tab into this box. Now within your script you could do something like

local distance = self.entity:GetDistance(self.cube)

 

There are many other ways to do this and without knowing more about what you are trying to accomplish, another method might be better.

 

Thank you so mutch mate!, finally it's working fine! smile.png

I was getting mad about!

Thanx to nick.ace too!

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