Spawn/Destroy Items in different alcoves at the same time

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
maneus
Posts: 246
Joined: Mon Jun 17, 2013 10:42 pm
Location: Switzerland

Spawn/Destroy Items in different alcoves at the same time

Post by maneus »

Can someone please tell me how I can destroy or spawn objects in different alcoves at the same time? Every alcove has the same item inside and I will remove all the items by one script at the same time and later spawn inside every alcove the same item.

I´ve tried to use the following code but without any success:

Code: Select all

for i in entitiesAt(6,6,4) and
			 entitiesAt(6,6,3) and
			 entitiesAt(6,5,4) and
			 entitiesAt(6,5,3) do
			
			if i.name == "dagger" then
			i:destroy()
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Spawn/Destroy Items in different alcoves at the same tim

Post by Isaac »

Offhand [very offhand], You would probably want to recursively check the alcoves themselves, for the dagger, and destroy them.
User avatar
maneus
Posts: 246
Joined: Mon Jun 17, 2013 10:42 pm
Location: Switzerland

Re: Spawn/Destroy Items in different alcoves at the same tim

Post by maneus »

Yes, Isaac. That is true. I´ve tried to use the

Code: Select all

for i in myalcove:containedItems() and
myalcove2:containedItems() do
if i.name == "dagger" then
i:destroy()
but this works only for one alcove and not for more.

And destroy an alcove destroyed only the alcove and not the item inside.
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Spawn/Destroy Items in different alcoves at the same tim

Post by Isaac »

maneus wrote:Yes, Isaac. That is true. I´ve tried to use the

Code: Select all

for i in myalcove:containedItems() and
myalcove2:containedItems() do
if i.name == "dagger" then
i:destroy()
but this works only for one alcove and not for more.

And destroy an alcove destroyed only the alcove and not the item inside.
You would write a FOR loop that redefines the target alcove each run through the loop... Something like: alcove = findEntity(alcove_id..x)
*Where "alcove_id" could be 'dungeon_alcove_'. This would make the "alcove" variable point to dungeon_alcove_1, dungeon_alcove_2, dungeon_alcove_3 ...

**Edit: I changed 'name' to 'id' for clarity.
User avatar
maneus
Posts: 246
Joined: Mon Jun 17, 2013 10:42 pm
Location: Switzerland

Re: Spawn/Destroy Items in different alcoves at the same tim

Post by maneus »

Thank you Isaac.

So a "x" instead of a number will search for all objects with the same id? Is that right?
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Spawn/Destroy Items in different alcoves at the same tim

Post by minmay »

If your alcoves were called "myalcove1", "myalcove2", "myalcove3", and "myalcove4" then you could use code like this:

Code: Select all

for h = 1, 4 do
  for i in findEntity("myalcove"..h):containedItems() do
    if i.name == "dagger" then i:destroy() end
  end
end
The expression

Code: Select all

"myalcove"..h
makes a string that is the value of h appended to "myalcove". So if h is 3, then it will result in "myalcove3".

As for your original approach, the "and" keyword in Lua is just a logical operator. Take a look at the Lua manual. It looks like you tried to use it to combine two iterators - you can't really do that. The "containedItems()" function returns an iterator; "for i in myalcove:containedItems() do [whatever] end" takes that iterator, gets the next item from it, runs [whatever], then repeats until the iterator runs out of items.
The expression "myalcove:containedItems() and myalcove2:containedItems()" has the same result as the expression "myalcove2:containedItems()". The "and" operator just returns its first argument if it is false or nil, and otherwise returns the second, which is what's happening here.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Spawn/Destroy Items in different alcoves at the same tim

Post by Isaac »

maneus wrote:Thank you Isaac.
Image
So a "x" instead of a number will search for all objects with the same id? Is that right?
No...

Code: Select all

for x = 1,16 do
		alcove = findEntity("dungeon_alcove_"..x)
	end
In this loop, x will mean 1 through 16 as the loop repeats. So the second time the loop runs, "alcove" will point to the entity "dungeon_alcove_2".


User avatar
maneus
Posts: 246
Joined: Mon Jun 17, 2013 10:42 pm
Location: Switzerland

Re: Spawn/Destroy Items in different alcoves at the same tim

Post by maneus »

Thank you minmay and Isaac. I get it to work. Image
Post Reply