Page 1 of 1

Error Suppression

Posted: Thu Nov 06, 2014 5:35 pm
by NutJob
Is there a way to silent errors AND continue executing?

Specifically I have a function that accepts a string reference for a sound and plays it, but if I, or the user, types a sound_name that either 1) doesn't exist or 2) play for whatever formatting reason I just want the script to continue on and simply not play the sound.

Example

Code: Select all

     local str = "moaning_moaner_moans" -- the string reference that doesn't exist
     @playSound(str)
--   ^ silently passes this function and continues on
     hudPrint("I'm so moany") -- this will print
I don't want a global-scale toggle for suppression either (if it exists?) but assignment to per-case to functions.

Re: Error Suppression

Posted: Thu Nov 06, 2014 8:54 pm
by Lark
You always come up with the hard ones, don't you NutJob? I thought about checking to see if they exist, since I can't find anything on error suppression... no luck. I even thought about building your own table by reading a file + the sound definitions, but the io.open, et cetera aren't supported that I can tell. I'll keep this in mind as I explore, but I don't see a way besides building your own table of defined sounds and checking to see if they exist - yuck!

If you do this, you can build the table once as a global (so it will get saved) and reference it subsequently, albeit you may have to build it in each script container (yuck squared!) If you do, you might want to store it as logical table for easy lookups.

Code: Select all

isSound = {one=true, two=true, four=true}
print(isSound["one"], isSound["three"])
Yes, this stinks, but it's all I could find so far! -Lark

Re: Error Suppression

Posted: Thu Nov 06, 2014 9:20 pm
by JohnWordsworth
I have a possible solution, but it's a lot of work for potentially not much gain.

1. At the top of your init.lua script for your mod, you can redefine the defineSound method to capture the data as it's defined. AS long as this comes before the include "assets/.../standard_assets.lua" then you should capture all of that data too.

2. Define a script entity which has source code built in listing these sounds.

3. Place that script entity in your level.

A quick draft (untested)...

init.lua
SpoilerShow

Code: Select all

g_sounds = { };

oldDefineSound = defineSound;

function defineSound(data) 
    table.insert(g_sounds, data.name)
    oldDefineSound(data)
end

import "assets/.../standard_assets.lua";

-- Other Imports

function build_sound_db_source()
  local lines = { }
  table.insert(lines, "data = { }");

  for _, value in ipairs(g_sounds) do
    table.insert(lines, "table.insert(data, \"" .. value .. "\")");
  end

  return table.concat(lines, "\n");
end

defineObject{
  name = "sound_db",
  baseObject = "script_entity",
  components = {
    {
      class = "Script",
      source = build_sound_db_source(),
    }
  }
}
Then you can make a "safe" play sound that checks the sound DB by placing the sound_db object in your dungeon and then calling the following from a script.

In a script entity
SpoilerShow

Code: Select all

function playSoundSafe(name) 
  if sound_db.script.data[name] == nil then
    Console.warn("Sound not available")
  else
    playSound(name)
  end 
end

playSoundSafe("ding")
I've literally just written that without checking anything like the scripting reference, so it likely won't work out of the box (and there is a tiny chance it's not actually a viable option at all), but I think it should work with just a few tweaks - if you really want to go through that much effort to implement this feature!

Re: Error Suppression

Posted: Thu Nov 06, 2014 9:24 pm
by NutJob
Lark wrote:You always come up with the hard ones, don't you NutJob? I thought about checking to see if they exist, since I can't find anything on error suppression... no luck. I even thought about building your own table by reading a file + the sound definitions, but the io.open, et cetera aren't supported that I can tell. I'll keep this in mind as I explore, but I don't see a way besides building your own table of defined sounds and checking to see if they exist - yuck!

If you do this, you can build the table once as a global (so it will get saved) and reference it subsequently, albeit you may have to build it in each script container (yuck squared!) If you do, you might want to store it as logical table for easy lookups.

Code: Select all

isSound = {one=true, two=true, four=true}
print(isSound["one"], isSound["three"])
Yes, this stinks, but it's all I could find so far! -Lark
Yeah, the function currently defaults to a sound in a defined table of sounds (through some very unusual conditional checking heh) but the function has some amorphic arguments that can alter how other arguments are used... so yeah, I rather just pass some function calls into void if they get whiny.

Re: Error Suppression

Posted: Thu Nov 06, 2014 9:35 pm
by NutJob
I like that approach, though I really want a universal, globally defined, and accessible to any function LUA/LoG2 API method to silently pass terminating flags. I didn't necessary want to use error suppression on only playSound() though, wow!, that is a neat way to wrap them all up into a table and check if it exists.

Sometimes while executing procedural logic of a function I'd like to kill any terminating flags so I can get procedures after the nuisance that may be valuable in a troubleshooting scenario.