Page 1 of 2

scripting an end

Posted: Thu May 16, 2013 6:19 pm
by bongobeat
Hi,

What a pity, but I dont know anything about scripting: :oops:

can someone explain me how to script some condition to put a end in my mod?

I want to load a screenshot or a cinematic for the end (not decided yet), only when you find all treasures + a new one + the bones of Toorum.

I mean:

If party get: golden_chalice, golden_crown, ant the other..., + toorum_remains
then:
load a screenshot with some text like: screenshot abcdef.jpg + "Congratulation you have find all Toorum treasure, blablabla..."

Re: scripting an end

Posted: Thu May 16, 2013 6:59 pm
by JohnWordsworth
Hi BongoBeat, you can finish your mod with a scripted ending (even if that scripted ending is simply a single screenshot that fades in and out) using the completeGame("mod_assets/scripts/cinematics/ending1.lua"); method. There is a good description of how to do this here:

http://www.grimrock.net/modding/how-to- ... inematics/

You can have multiple endings if you would like, or you could simply not have it end until you have collected all of the items. Now, in order to check for those conditions to see if the party has a certain set of items in their inventory, you would need a script entity with a function something like this (untested, may have a few syntax errors):
SpoilerShow

Code: Select all

-- First define a generic function you can use over and over to see if the party has a specific item.
-- We have to check every item slot for every character, if an item contains other items, we need
-- to check those too.
function partyHasItem(item_name)
  for champIdx=1,4 do 
    local champion = party:getChampion(champIdx);

    for slot=1,31 do
      local item = champion:getItem(slot);

      if ( item ~= nil ) then 
        if ( item.name == item_name ) then
          return true;
        end

        if ( item:containedItems() ~= nil ) then
          for subItem in item:containedItems() do 
            if ( subItem.name == item_name ) then
              return true;
            end
          end
        end
      end
    end
  end
 
  return false;
end

-- Now define a single function that we can use to check for all of these items and then end the game if necessary.
function checkToorumTreasures()
  if partyHasItem("golden_chalice") and partyHasItem("golden_crown") and partyHasItem("...") then
    completeGame("mod_assets/scripts/cinematics/ending1.lua");
  end
end
Note that, with this solution, you first define a function that you can use over and over again to see if the party is carrying a certain item. To do this, we simply have to iterate over every character in the party, and every item in their inventory and check the name of each item (if it exists) with that parameter you have passed to the function. We also have to check containedItems incase the party has put the item in a sack. If it finds the item at any point, it returns true. If you get to the end of the function and hanen't found it, then it returns false.

There are frameworks here that make this stuff a bit easier. Do a search here for 'jKos framework' or 'grimQ', both contain loads of useful functions like the one above which will help you find out if the party is carrying something for instance.

Then you just have to (a) make sure you have defined an ending1.lua and (b) call 'checkToorumTreasures', either from a timer or when you walk onto a certain pressure plate (depending on how you would like to end the game).

Re: scripting an end

Posted: Fri May 17, 2013 6:01 pm
by bongobeat
ok thank you for the answer, i will try some :)

Re: scripting an end

Posted: Fri May 17, 2013 7:03 pm
by bongobeat
hermm

ok, first: no synthax error, or the editor didn't report them :)

then:
I have try to modify the code: if i have understand , it is the code for one item?
for 2 or more, i have to copy paste some lines?

in first time i have use the torch to test: all this code is in a lua script
and a plate activates it when activated
dont know if i put something wrong but it indicates "game completed" when i activate the plate, with the torch in one hand slot

since i had no idea of what it should done, i guess it works?

the code

Code: Select all

-- item torch
function partyHasItem(torch)
  for champIdx=1,4 do
    local champion = party:getChampion(champIdx);

    for slot=1,31 do
      local item = champion:getItem(slot);

      if ( item ~= nil ) then 
        if ( item.name == torch ) then
          return true;
        end

        if ( item:containedItems() ~= nil ) then
          for subItem in item:containedItems() do 
            if ( subItem.name == torch ) then
              return true;
            end
          end
        end
      end
    end
  end

  return false;
end
-- Now define a single function that we can use to check for all of these items and then end the game if necessary.
function checkToorumTreasures()
  if partyHasItem("torch") then
    completeGame("mod_assets/cinematics/ending1.lua");
  end

end
i didnt create a file "ending1.lua"
so it shows only "game completed!"

then i create the "ending1.lua"

i put this in the file (from your link "how to create cinematics")

Code: Select all

-- simple one image cinematic
enableUserInput()
startMusic("assets/samples/music/intro.ogg")

-- show the image
showImage("assets/textures/cinematic/intro/page01.tga")
fadeIn(2)
sleep(4)
fadeOut(4)
i have reload the editor and the mod, but that didn't shows the page01.tga after i activate the plate, it's always "game completed!"
did i do something wrong? :?:

Re: scripting an end

Posted: Wed May 22, 2013 7:11 pm
by bongobeat
Hello

please I really need help! :oops:

Re: scripting an end

Posted: Wed May 22, 2013 7:27 pm
by Ryeath_Greystalk
Check this thread and see if there is anything that can help.

viewtopic.php?f=14&t=5108

Also make sure sure that your ending file is actually a LUA file. A mistake I made when doing mine was I used notepad to make the file then saved it as .txt and renamed it .LUA after. That did not work, it has to be saved as .LUA

Re: scripting an end

Posted: Wed May 22, 2013 8:56 pm
by msyblade
You may also find this thread useful:

viewtopic.php?f=14&t=4365

and one way to avoid saving the lua as text: copy another lua, erase and rewrite it.

Re: scripting an end

Posted: Wed May 22, 2013 9:16 pm
by Ryeath_Greystalk
One other detail which you probably know but I will toss it out.

Make sure it is a .LUA file located in the "mod_assets/cinematics" folder, (double check the path), not a LUA script in the editor.

Re: scripting an end

Posted: Sun May 26, 2013 10:12 am
by bongobeat
Hello all, thanks for your answers

<<< looking the links
Ryeath_Greystalk wrote:One other detail which you probably know but I will toss it out.

Make sure it is a .LUA file located in the "mod_assets/cinematics" folder, (double check the path), not a LUA script in the editor.
I have created a lua file in the cinematic folder.

Re: scripting an end

Posted: Sun May 26, 2013 11:03 am
by Sutekh
Cinematics won't be shown while in the editor, only when you play an exported version of your dungeon.