how to find entity (entities) on map [Solved]

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
User avatar
Jouki
Posts: 127
Joined: Fri Oct 24, 2014 12:57 pm

how to find entity (entities) on map [Solved]

Post by Jouki »

Hello, can you help me please how can I check if entity exists? I'd like to use 'while' for enabling particles but 'findEntity()' doesnt seems to work . I need something that returns true/false if entity even exists.
Last edited by Jouki on Fri Oct 24, 2014 9:36 pm, edited 1 time in total.
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: how to find entity (entities) on map

Post by NutJob »

Might be helpful?

Code: Select all


arr_entities= {"something_1", "something_2", "something_3"}

function aGenericFunc()
   for _,strId in pairs(arr_entities) do
      x = findEntity(strId)
      x.light:enable()
   end
end
User avatar
Jouki
Posts: 127
Joined: Fri Oct 24, 2014 12:57 pm

Re: how to find entity (entities) on map

Post by Jouki »

NutJob wrote:Might be helpful?

Code: Select all


arr_entities= {"something_1", "something_2", "something_3"}

function aGenericFunc()
   for _,strId in pairs(arr_entities) do
      x = findEntity(strId)
      x.light:enable()
   end
end

Actaully it doesn't solve that I have to write all of these light sources into the code :) but nevermind thanks for help, I'd need something like :D :

Code: Select all

int i=1;
while(lightSource_[i]:isExist()){
  lightSource_[i].light:enable();
  lightSource_[i].particle:enable();
  i++;
}
User avatar
SnowyOwl47
Posts: 148
Joined: Fri Sep 12, 2014 10:41 pm

Re: how to find entity (entities) on map

Post by SnowyOwl47 »

Actaully it doesn't solve that I have to write all of these light sources into the code :) but nevermind thanks for help, I'd need something like :D :

Code: Select all

int i=1;
while(lightSource_[i]:isExist()){
  lightSource_[i].light:enable();
  lightSource_[i].particle:enable();
  i++;
}
[/quote]

That is easy to do!

First of all your light sources would be called something like castle_lantern_1 first tell me what kind of lights they are or at least what there called.
User avatar
Jouki
Posts: 127
Joined: Fri Oct 24, 2014 12:57 pm

Re: how to find entity (entities) on map

Post by Jouki »

SnowyOwl47 wrote:
That is easy to do!

First of all your light sources would be called something like castle_lantern_1 first tell me what kind of lights they are or at least what there called.
mine_support_pillar_lantern_

Now I have about 20 these pillars ;)
User avatar
SnowyOwl47
Posts: 148
Joined: Fri Sep 12, 2014 10:41 pm

Re: how to find entity (entities) on map

Post by SnowyOwl47 »

Code: Select all

i = 0
function findAllLights()
     while mine_support_pillar_lantern_ i :isExist() == true do
          i = i + 1
          mine_support_pillar_lantern_ i .light:enabled(true)
          mine_support_pillar_lantern_ i .particle:enabled(true)
     end
end
Tha code should work don't change it any way, the i has to have spaces on both sides but it will still work!
User avatar
SnowyOwl47
Posts: 148
Joined: Fri Sep 12, 2014 10:41 pm

Re: how to find entity (entities) on map

Post by SnowyOwl47 »

SnowyOwl47 wrote:

Code: Select all

i = 0
function findAllLights()
     while mine_support_pillar_lantern_ i :isExist() == true do
          i = i + 1
          mine_support_pillar_lantern_ i .light:enabled(true)
          mine_support_pillar_lantern_ i .particle:enabled(true)
     end
end
Tha code should work don't change it any way, the i has to have spaces on both sides but it will still work!

Now with that said actually from what I can tell this might be what you want to do!

Code: Select all

found = false
i = 0
function findAllLights()
     while found == false do
          if i == 21 then
                found = true
          else
                i = i + 1
                mine_support_pillar_lantern_ i .light:enabled(true)
                mine_support_pillar_lantern_ i .particle:enabled(true)
          end
     end
end
User avatar
Jouki
Posts: 127
Joined: Fri Oct 24, 2014 12:57 pm

Re: how to find entity (entities) on map

Post by Jouki »

SnowyOwl47 wrote:
SnowyOwl47 wrote:

Code: Select all

i = 0
function findAllLights()
     while mine_support_pillar_lantern_ i :isExist() == true do
          i = i + 1
          mine_support_pillar_lantern_ i .light:enabled(true)
          mine_support_pillar_lantern_ i .particle:enabled(true)
     end
end
Tha code should work don't change it any way, the i has to have spaces on both sides but it will still work!

Now with that said actually from what I can tell this might be what you want to do!

Code: Select all

found = false
i = 0
function findAllLights()
     while found == false do
          if i == 21 then
                found = true
          else
                i = i + 1
                mine_support_pillar_lantern_ i .light:enabled(true)
                mine_support_pillar_lantern_ i .particle:enabled(true)
          end
     end
end
unfortunately I put this into the script and absolutely didnt change anything (except connectors of course) and this happened (I've noticed I've forgotten 01 at the and of the name but I added that)
Image
Image
User avatar
SnowyOwl47
Posts: 148
Joined: Fri Sep 12, 2014 10:41 pm

Re: how to find entity (entities) on map

Post by SnowyOwl47 »

Hmm @Jouki Delete the spaces around the i ok
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: how to find entity (entities) on map

Post by JohnWordsworth »

When building an entity name from variables and then retrieving that entity, you will want to do some string concatenation and use the "findEntity" function. The following first defines a flexible function that can turn on a whole bunch of lights that all start with the same ID in the editor and then defines a specific "utility function" that you can call from a pressure plate that turns on all lights in your dungeon that have the name "top_floor_lights_i" (where i starts at 1 and goes up to the last light it finds using this pattern).

Code: Select all

-- A generic function that will turn on the light and particle component 
-- for all entities in the dungeon that have the id "prefix_i" where
-- "prefix" is the parameter given to this method and "i" counts up from 1.

function turnOnLights(prefix) 
  local i = 1;
  local entity = findEntity(prefix .. "_" .. i);

  while entity ~= nil do
    if entity.light ~= nil then
	  entity.light:enable();
    end

    if entity.particle ~= nil then
      entity.particle:enable();
    end

    i = i + 1;
    entity = findEntity(prefix .. "_" .. i);
  end
end

-- 
-- This function is one you can hook into a pressure plate and it will 
-- turn on all lights in your dungeon with the name "top_floor_lights_i"
-- ie. top_floor_lights_1, top_floor_lights_2, ...

function turnOnTopFloorLights()
	turnOnLights("top_floor_lights");
end
Hope this helps!
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
Post Reply