Jump to content

[SOLVED] LUA - Get an object by Name in editor


tipforeveryone
 Share

Recommended Posts

This is my Scene Tree

 

If I attach a script to "CSCDSoldier" then I can write something to do to its child (ex: mp5sd) by using

 

self.entity:FindChild("mp5sd"):GetPosition()

 

or in case, script is attached to "mp5sd", I still can get "CSCDSoldier" by using

 

self.entity:GetParent():GetPosition()

 

But how can I get "CSCDSoldier" even "mp5sd" from script which I attached to "Player"

 

I did not find anything like

 

self.entity:GetSiblings("CSCDSoldier"):GetChild("mp5sd")

 

in the API Reference Library

 

Please show me how. Thanks smile.png

post-16833-0-04255300-1455435156.png

Link to comment
Share on other sites

The simple way is if you add

Script.Enemy = nil --entity

at the top of player script. If you highlight player in the scene tree and look at players script tab.

Then drag CSCDSoldier from the scene tree to the empty Enemy box.

You can then access CSCDSoldier by using

self.distance = self.entity:GetDistance(self.Enemy) //self. distance is an example

or even some variable on CSCDSoldier script for example self.TakeHit was in that script.

self.Enemy.script:TakeHit(self.damage)

This way you can also access CSCDSoldier's child items.

 

The following are worth looking at as well.

self.entity:SetKeyValue("item","name")

 

self.target:GetKeyValue("item")

  • Upvote 1

Elite Cobra Squad

Link to comment
Share on other sites

I created all objects in Editor and rename them

I want to access them from anywhere without using

Script.variableName = "" --entity "Label"

 

Example: I press Q key to make an entity disappear

ìf window:KeyDown(Key.Q) then
GetSomeThingByNameFunction("EntityName"):DoSomeThingFunction()
end

 

Just simple like that smile.png I need to put this code to main.lua so that I can't use the '--entity' method I guess

Link to comment
Share on other sites

I make a utility function for this when I need to. Note that every entity you use this for should be a unique name or else this will just get the first one. You could modify it if you want to get all entities with the same name to have it add to a table and return the table.

 

function FindEntity(entityName)
local x
local world = World:GetCurrent()

for x = 0, world:CountEntities() - 1 do
        local entity = world:GetEntity(x)

--System:Print("name = "..entity:GetKeyValue("name"))

        if entity:GetKeyValue("name") == entityName then
return entity
end
end

return nil
end

  • Upvote 2
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...