Page 1 of 1

Re Destroying Multiple Objects need help

Posted: Mon Sep 23, 2013 6:54 am
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

Re: Re Destroying Multiple Objects need help

Posted: Mon Sep 23, 2013 7:46 pm
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

Re: Re Destroying Multiple Objects need help

Posted: Mon Sep 23, 2013 8:20 pm
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....

Re: Re Destroying Multiple Objects need help

Posted: Wed Oct 02, 2013 3:16 am
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

Re: Re Destroying Multiple Objects need help

Posted: Wed Oct 02, 2013 3:45 am
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

Re: Re Destroying Multiple Objects need help

Posted: Wed Oct 02, 2013 11:31 am
by Eleven Warrior
Hi guys Thank you both for this coding.. Now I know I can call the Entity more than once..