Page 1 of 1

turning on random lights?

Posted: Fri Mar 22, 2013 2:07 am
by remilafo
Say i had 15 lamps...

and i wanted to randomly toggle (on/off) one of them every 2 seconds..

How would i do this?

Right now my code is this but it's not working//

Function Lights()
local i = math.random(1, 15)
lights_i:toggle()
end

??

Re: turning on random lights?

Posted: Fri Mar 22, 2013 2:19 am
by Komag
findEntity("lights_"..i):toggle()

that way it's a string you can manipulate

Re: turning on random lights?

Posted: Fri Mar 22, 2013 5:35 am
by remilafo
Komag wrote:findEntity("lights_"..i):toggle()

that way it's a string you can manipulate
thanks that worked.. and helped me make some of my other scripts more efficient.

Re: turning on random lights?

Posted: Fri Mar 22, 2013 3:00 pm
by remilafo
how would i manipulate a large string of names?

ex. I have my 15 lights in a 3 x 5 grid.. there names are as follows..

light_1_1
light_1_2
light_1_3
..
light_3_5
light_2_4
light_3_4

etc.. i hope that gets the point across..

how do i manipulate the string as to acess those entities..

local x = math.random(1,3)
local y = math.random(1,5)

findEntity("lights_"..x.."_"..y):toggle( ????

Re: turning on random lights?

Posted: Fri Mar 22, 2013 4:19 pm
by Komag
pretty much, yeah, but it looks like they're called light not lights :)

Re: turning on random lights?

Posted: Sat Mar 23, 2013 2:24 am
by JohnWordsworth
As Komag said, it looks like it's just a typo that is preventing the above from working, but you can always get some useful debug information out by doing the following (it's a tiny bit slower, but useful for debugging purposes).

Code: Select all

local light_id = "light_" .. math.random(1,3) .. "_" .. math.random(1,5);
print("Toggling " .. light_id);
local light = findEntity(light_id);

if ( light ~= nil ) then
  light:toggle();
else 
  print("Error in light toggle script: LIght " .. light_id .. " does not exist!");
end
So, the above script stores the light_id in a string first, and the print() command will print it to the top left of the game window in the Dungeon Editor. I personally also think that it's good practice to check whether the findEntity(...) call actually returns an entity. By storing the result of findEntity in a local variable, we can easily check whether that variable exists with if (light ~= nil). This means that, if the entity doesn't exist - we just get an error message instead of Grimrock crashing to the desktop.

You will want to comment out or delete the print line when you are happy and it all works, otherwise you'll get debug messages for the rest of time while the script is running! Just add '--' to the start of the line and Grimrock will ignore that line in the script.

Sidenode: I know that you don't need to put the ~= nil part in the if statement, but I like to clarify whether I'm checking for nil or for a true/false value - so this makes it really clear what I'm trying to catch.

Re: turning on random lights?

Posted: Sat Mar 23, 2013 12:37 pm
by Komag
JohnWordsworth wrote: Sidenode: I know that you don't need to put the ~= nil part in the if statement, but I like to clarify whether I'm checking for nil or for a true/false value - so this makes it really clear what I'm trying to catch.
Hmm, that's a very good idea that I never really followed, but now that you mention it I can remember some situations where if I'd been more careful about the difference I might have had a few less headaches :)