Re Destroying Multiple Objects need help

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Eleven Warrior
Posts: 752
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re Destroying Multiple Objects need help

Post by Eleven Warrior »

Hi again all..

I remember in the Super Threads reading about how to Destroy lots of Objects in one function, as you can see I have done it the hard way and its taking to long to copy - paste LOL.. I think it has something to do with AllEntity and a Table of sorts, cant remember..

Any help please.. Thank You again

---- Destroy The Fogs ----

function foggone1()
fog_big1:destroy()
end

function foggone2()
fog_low1:destroy()
end
function foggone3()
fog_big2:destroy()
end
function foggone4()
fog_low2:destroy()
end
function foggone5()
fog_big3:destroy()
end
function foggone6()
fog_low3:destroy()
end
function foggone7()
fog_low4:destroy()
end
User avatar
Diarmuid
Posts: 807
Joined: Thu Nov 22, 2012 6:59 am
Location: Montreal, Canada
Contact:

Re: Re Destroying Multiple Objects need help

Post by Diarmuid »

Well, if you want to destroy them all at once, why not:

Code: Select all

function destroyFogs()
    fog_big1:destroy()
    fog_low1:destroy()
    fog_big2:destroy()
    fog_low2:destroy()
    fog_big3:destroy()
    fog_low3:destroy()
    fog_low4:destroy()
end
Of course, with large numbers, it easier if you make up the id string with a for loop, in which case you need to use findEntity:

Code: Select all

function destroyFogs()
    for i = 1, 4 do
        findEntity("fog_low"..i):destroy()
        findEntity("fog_big"..i):destroy()
    end
end
User avatar
Eleven Warrior
Posts: 752
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: Re Destroying Multiple Objects need help

Post by Eleven Warrior »

Hi Diarmuid how are you?

Well thats awesome thanks for the help.. Ithought i saw this Coding in the Threads, man so easy LOL..
This will save me alot of work. I have to Destroy the Objects in order to reduce CPU usage it works good to..

Thank You again PS: My son tested my Mod yesterday and guess what (Dont get mad at me now) He loved the Sky System you made ahy just awesome Thxs....
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Re Destroying Multiple Objects need help

Post by JohnWordsworth »

Just a quick tip, you can make this code 'crash safe' by checking for nil results from findEntity() before trying to call the destroy method. Problems arise if you try to call methods on a nil object, and if find entity doesn't find a given entity, it will return nil. For instance, if you accidentally call this method a second time, it will crash the game. You could get over that by doing (okay, not 100% safe! but it catches the obvious fail case)...

Code: Select all

function safeDestroy(entity)
  if (entity ~= nil) then
    entity:destroy();
  end
end

function destroyFogs()
    for i = 1, 4 do
        safeDestroy( findEntity("fog_low"..i) );
        safeDestroy( findEntity("fog_big"..i) );
    end
end
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
Diarmuid
Posts: 807
Joined: Thu Nov 22, 2012 6:59 am
Location: Montreal, Canada
Contact:

Re: Re Destroying Multiple Objects need help

Post by Diarmuid »

JohnWordsworth wrote:Just a quick tip, you can make this code 'crash safe' by checking for nil results from findEntity() before trying to call the destroy method. Problems arise if you try to call methods on a nil object, and if find entity doesn't find a given entity, it will return nil. For instance, if you accidentally call this method a second time, it will crash the game. You could get over that by doing (okay, not 100% safe! but it catches the obvious fail case)...
Great addition. On thing however I find with "safe" coding, is that it tends to hide bugs away, and you think things are working great because nothing crashes... a warning could be useful (and it also does the findEntity for you, less code to type):

Code: Select all

function safeDestroy(entityId)
	local entity = findEntity(entityId);
	if (entity ~= nil) then
		entity:destroy();
	else
		print("Warning: trying to destroy "..entityId..", a nil value");
	end
end

function destroyFogs()
    for i = 1, 4 do
        safeDestroy("fog_low"..i);
        safeDestroy("fog_big"..i);
    end
end
User avatar
Eleven Warrior
Posts: 752
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: Re Destroying Multiple Objects need help

Post by Eleven Warrior »

Hi guys Thank you both for this coding.. Now I know I can call the Entity more than once..
Post Reply