Spawn a lot of torches

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
Dinasty
Posts: 10
Joined: Tue Oct 02, 2012 6:59 pm

Spawn a lot of torches

Post by Dinasty »

Hello all, i'm very noob on scripting but I'm working on my custom dungeon.
Please, i need that when the players place the "red gem" on the altar, all the torches spawns in the room (only once, and only where there aren't torches).

is this possible?

(sorry for bad english, but i'm italian :P)
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Spawn a lot of torches

Post by Komag »

yeah, that can be done, no problem, although it might be a little complex if you're new to scripting.

Basically, run a check on each torch holder whether it has a torch in it or not:

Code: Select all

TorchHolder:hasTorch()
if it does, then you will do nothing. if it doesn't, then you'll add a torch to it:

Code: Select all

TorchHolder:addTorch()
and finally, you will set the whole script to only run once. one way to do that is to connect the trigger event (red gem on altar) to a counter with the count of 1, and link the counter to the script. The first time the gem is placed, the counter will reach 0 and trigger the script, but subsequent gem placements will only further reduce the counter and it will never hit 0 again.
Finished Dungeons - complete mods to play
Dinasty
Posts: 10
Joined: Tue Oct 02, 2012 6:59 pm

Re: Spawn a lot of torches

Post by Dinasty »

There is a way to use 2 function in a single one?
I read this "http://www.grimrock.net/modding/how-to- ... ve-puzzle/" and i want that "When you place red fragment on the altar all the torches spawns in the room".
I try this but it doesn t work :D

Code: Select all

function itemPuzzle()
   -- Test Altar and Torches
   for i in altar_sud:containedItems() do
      if i.name == "red_fragment" then
         for i in allEntities(party.level) do
             if i.name == "torch_holder" then
                i:addTorch()
                end
             end
          end
       end
    end
end
Can someone post me the right code please? Thanks!
User avatar
Blichew
Posts: 157
Joined: Thu Sep 27, 2012 12:39 am

Re: Spawn a lot of torches

Post by Blichew »

Dinasty wrote:

Code: Select all

function itemPuzzle()
   -- Test Altar and Torches
   for i in altar_sud:containedItems() do
      if i.name == "red_fragment" then
         for i in allEntities(party.level) do
             if i.name == "torch_holder" then
                i:addTorch()
                end
             end
          end
       end
    end
end
Can someone post me the right code please? Thanks!
I think you can't use another for loop with the same variable ("i") inside another loop.
Also: you have too many "end"s.

So, try this:

Code: Select all

function itemPuzzle()
   -- Test Altar and Torches
	for altarItem in altar_sud:containedItems() do
		if altarItem.name == "red_fragment" then
			for i in allEntities(party.level) do
				if i.name == "torch_holder" then
					i:addTorch()
				end
			end
		end
	end
end
Dinasty
Posts: 10
Joined: Tue Oct 02, 2012 6:59 pm

Re: Spawn a lot of torches

Post by Dinasty »

Very thanks! It works :D but...

How can i run it only once? I try to deselect "Activate Always" from the Altar, but every time i put the red gem on the altar he play the sound i have set!
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Spawn a lot of torches

Post by Komag »

involve a counter in the script

Code: Select all

mycounter = 1
function itemPuzzle()
   for altarItem in altar_sud:containedItems() do
      if altarItem.name == "red_fragment" then
         if mycounter == 1 then
            for i in allEntities(party.level) do
               if i.name == "torch_holder" then
                  i:addTorch()
                  mycounter = 0
               end
            end
         end
      end
   end
end
Finished Dungeons - complete mods to play
User avatar
Blichew
Posts: 157
Joined: Thu Sep 27, 2012 12:39 am

Re: Spawn a lot of torches

Post by Blichew »

You could also check if i:hasTorch() returns false, if so - i:addTorch().
This won't prevent the script from running a loop until it checks all entities.

Code: Select all

function itemPuzzle()
   -- Test Altar and Torches
	for altarItem in altar_sud:containedItems() do
		if altarItem.name == "red_fragment" then
			for i in allEntities(party.level) do
				if i.name == "torch_holder" and i:hasTorch() == false then
					i:addTorch()
				end
			end
		end
	end
end
Dinasty
Posts: 10
Joined: Tue Oct 02, 2012 6:59 pm

Re: Spawn a lot of torches

Post by Dinasty »

very thanks to all! i just complete my first level of my custom dungeon *_*
Post Reply