Lua script + counter

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
trancelistic
Posts: 275
Joined: Sat Jun 16, 2012 5:32 am

Lua script + counter

Post by trancelistic » Sun Nov 01, 2015 3:11 pm

Hey. How to tell in a lua script that a counter has to go -1 by value each time the script is triggerd ( and hopefully the counter will activate when it reaches value 0?)

Prolly some genius can do this without a counter and only the script... But I can't haha.

I totally forgot the commands.

Note: Its grimrock 1 not 2.

Glew
Posts: 64
Joined: Sat Dec 21, 2013 7:57 pm

Re: Lua script + counter

Post by Glew » Sun Jan 24, 2016 1:01 pm

Well, it has been months ago, but maybe it's still relevant.

This is a very useful page, with all the script commands you can give to assets: http://www.grimrock.net/modding_log1/sc ... reference/

To decrease the value of the counter you use:
counter_id:decrement() --"counter_id" here is the unique id of the counter you want to use, eg. "counter_2".

So a script with that is actually very simple:

Code: Select all

function decrement_counter()
counter_1:decrement()
end

--or

function decrement_if()
   if x==true --this can be any condition you want to trigger the decrement
   then counter_1:decrement()
   end
end
But if that is all you want to do, using the connectors from the editor is probably easier. Unless the thing that decrements the counter is a custom script of course.

Something that can open up a lot of options is if you use counter:getValue(). This way you can use the same counter to activate multiple stuff. The simplest thing is to count how many times a button was pushed and make it do different stuff at different points. Maybe if pushed 2 times, a door will close, but if pushed 5 times, the door will re-open and if pushed 10 times it will reset and notify the player that it was reset.

Code: Select all

--first you put a counter with the id "counter_1" and connect a button to it that increments it when pushed.
function button_count()
if counter_1:getValue()==2
then door_1_close()
elseif counter_1_getValue()==5 -- you can use "elseif" so you don't need to create 3 different "if" caluses
then door_1:open()
elseif counter_1:getValue()==10
then counter_1:reset() --this sets the value to what you give it as starting value in the editor
        playSound("click_up") --this plays a sound
       hudPrint("The button was reset.")
end
end
-- or you can do it a bit shorter if you define the value of the counter as a local variable in the script, this effectively just abbreviates its name so you don't need to type it all the time

function button_count()

local x=counter_1:getValue()

if x==2
then door_1_close()
elseif x==5
then door_1:open()
elseif x==10
then counter_1:reset() --this sets the value to what you give it as starting value in the editor
        playSound("click_up")
       hudPrint("The button was reset.")
end
end
And to test scripts with counters I found out it is very helpful to be able to print the value of the counter you do that with

Code: Select all

hudPrint(tostring(counter_id:getValue()))

Post Reply