Jump to content

refferencing fpsplayer


cassius
 Share

Recommended Posts

I am learning lua script and have placed the fpsplayer in my map. Whats the best way to refer to it in code, and other entities that are not loaded in code but just placed in editor?i

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

There are several ways to do this.

  • Script property. If is it an occasional entity that needs a reference, just use a script propery for entities and drag in the player. 
    Script.player = nil --entity "Player"

     

  • If many objects require a reference, I would handle it automatically. At the start of each script, loop over the entities in the world and check if the player is among them. If the player name is unique, then you can do something like this:
    local n
    for n=0, world:CountEntities()-1 do
    	local entity = world:GetEntity(n)
    	if entity:GetKeyValue("name") == "MyPlayer" then
    		playerEntity = entity
    		break
    	end
    end

    Note that you might want to store the player reference somewhere as it is used a lot. Saves iterations on start up.

Link to comment
Share on other sites

I need to know when the player is close to another character for example using GetDistance. Its easy in my  c++ project as all characters are loaded by code.

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

Again, many ways to do this. Some possibilities:

  • Let each enemy perform a distance check once a loop or after a timer is done. 
    --in enemey UpdateWorld
    local playerPos = self.entity:GetPosition(true)
    local enemyPos = self.playerEntity:GetPosition(true)
    local distance = playerPos:DistanceToPoint(enemyPos)
    
    if distance < 1.5 then --1.5 is just an examepl.
       System:Print("within attack range or something")            
    end

     

  • Or you let the player do a ForEachEntityinAABB. This retrieves all entities within a give shape around the player. Then for every entity it finds,check if it is an enemy and you can do things. 
    --player updateworld
    local playerSurrounding = self.entity:GetAABB(Entity.GlobalAABB)
    local world = World:GetCurrent()
    world:ForEachEntityInAABBDo(playerSurrounding, "MyCallback", self.entity)
    
    function MyCallback(entity, extra)
    	if entity.script ~= nil and entity.script.isEnemy then
    		System:Print("this is an enemy in the players region")
    	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...