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
??
turning on random lights?
Re: turning on random lights?
findEntity("lights_"..i):toggle()
that way it's a string you can manipulate
that way it's a string you can manipulate
Finished Dungeons - complete mods to play
Re: turning on random lights?
thanks that worked.. and helped me make some of my other scripts more efficient.Komag wrote:findEntity("lights_"..i):toggle()
that way it's a string you can manipulate
Re: turning on random lights?
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( ????
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?
pretty much, yeah, but it looks like they're called light not lights 
Finished Dungeons - complete mods to play
- JohnWordsworth
- Posts: 1397
- Joined: Fri Sep 14, 2012 4:19 pm
- Location: Devon, United Kingdom
- Contact:
Re: turning on random lights?
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).
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.
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
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.
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
Re: turning on random lights?
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 headachesJohnWordsworth 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.
Finished Dungeons - complete mods to play
