Page 1 of 1

See if a Level Exists from a Script?

Posted: Thu Oct 04, 2012 9:25 pm
by Lark
I'm working in my secret labs to develop a (hopefully) fun script that needs to detect if a level exists, then looks for a specific object on that level. If I use "entitiesAt(level, x, y)" with an invalid level, it crashes the script. How can I tell if a specific level exists and make a decision accordingly? I've searched the docs and forum, but I've not found anything yet.

Thanks Everyone, -Lark

Re: See if a Level Exists from a Script?

Posted: Thu Oct 04, 2012 9:32 pm
by Komag
what the heck? When will you "not know" when a level exists?

Re: See if a Level Exists from a Script?

Posted: Thu Oct 04, 2012 9:53 pm
by Lark
The script needs to look for something on the level below where the script is running. My hopes are that this script can be pasted in anywhere it's desired without modifications regardless of the size of the dungeon. Right now, if I paste it verbatim on the last level, it crashes. Obviously, I can easily place a parameter of "lastLevel = 20" into the script once, then use that copy everywhere, but I'm trying to make it totally dynamic for others to use. Does this help? Hopefully I'll post it next week, but it needs more tweaking first!

Re: See if a Level Exists from a Script?

Posted: Fri Oct 05, 2012 3:52 am
by Edsploration
I tried using the allEntities(level) iterator but don't know if it's possible to catch the error of calling a level that doesn't exist. I suppose it's possible to loop through all the objects and check for a down staircase or pit, but that's a really ugly solution and doesn't capture teleporters.

Re: See if a Level Exists from a Script?

Posted: Sun Oct 14, 2012 9:58 pm
by Lark
Komag wrote:what the heck? When will you "not know" when a level exists?
Check out viewtopic.php?f=14&t=3723 for my Automatic Elevator script. Multiple scripts stack on levels above and below each other to form an elevator. Since the script was to always look above and below itself for another (elevator) script, I was hoping to not have the script crash if it was already on the last level. Looking downward again crashes it. As it turned out, however, I needed several other parameters, so I just included the upper and lower limits of the elevator as parameters too.

So while the request sounded silly, it really had a use. I'm going to rewrite the stack of scripts as a single script now that I know how, but level checking would still be nice to get the user a nice error message if they configure an elevator beyond the actual levels that exist. It’s not a big deal, of course.

Cheers, -Lark

Re: See if a Level Exists from a Script?

Posted: Sun Oct 14, 2012 10:15 pm
by djoldgames
Simple - build your own array (table) like

Code: Select all

MyLevels = { 1,2,3,4,5 }
and check it in all your functions?

Re: See if a Level Exists from a Script?

Posted: Mon Oct 15, 2012 12:50 am
by JohnWordsworth
A completely theoretical and untested suggestion...

Code: Select all

local level = 17
local status, err = pcall(allEntities, level)

if status == true
  print(level .. " exists")
else
  Print("Boo, no more levels.")
end
This assumes that allEntities throws an error if you pass an invalid level (instead of just returning nothing, which would also give you a useful result?). It also assumes that it's a Lua catchable error and that Lua in Grimrock supports pcall!

Edit: Equally, you could use pcall with your original call and then check the status to see if it worked or not!

Re: See if a Level Exists from a Script?

Posted: Tue Oct 16, 2012 10:08 pm
by Lark
JohnWordsworth wrote:...that Lua in Grimrock supports pcall!
This would have been great for this, and many other applications. It does not appear, however, that Grimrock supports pcall as I get attempt to call global pcall (a nil value). Thank you for the suggestion, however. -Lark