See if a Level Exists from a Script?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Lark
Posts: 178
Joined: Wed Sep 19, 2012 4:23 pm
Location: Springfield, MO USA

See if a Level Exists from a Script?

Post 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
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: See if a Level Exists from a Script?

Post by Komag »

what the heck? When will you "not know" when a level exists?
Finished Dungeons - complete mods to play
User avatar
Lark
Posts: 178
Joined: Wed Sep 19, 2012 4:23 pm
Location: Springfield, MO USA

Re: See if a Level Exists from a Script?

Post 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!
User avatar
Edsploration
Posts: 104
Joined: Wed Sep 19, 2012 4:32 pm

Re: See if a Level Exists from a Script?

Post 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.
Open Project -> Community FrankenDungeon: viewtopic.php?f=14&t=4276
User avatar
Lark
Posts: 178
Joined: Wed Sep 19, 2012 4:23 pm
Location: Springfield, MO USA

Re: See if a Level Exists from a Script?

Post 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
User avatar
djoldgames
Posts: 107
Joined: Fri Mar 23, 2012 11:28 pm
Contact:

Re: See if a Level Exists from a Script?

Post 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?
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: See if a Level Exists from a Script?

Post 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!
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
Lark
Posts: 178
Joined: Wed Sep 19, 2012 4:23 pm
Location: Springfield, MO USA

Re: See if a Level Exists from a Script?

Post 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
Post Reply