Scripting: Add an optional 'required' parameter to getReference (default: true). If required=false, it will not throw an exception if there's no reference. Fixes PcExpell not working without a reference like it's supposed to, and makes the code nicer for some others (use required=false instead of catching the exception)

This commit is contained in:
scrawl 2014-01-09 02:14:08 +01:00
parent 223c1d5a38
commit 768d9f7237
6 changed files with 19 additions and 30 deletions

View file

@ -54,14 +54,7 @@ namespace MWScript
public:
virtual void execute (Interpreter::Runtime& runtime)
{
// FIXME: No way to tell if we have a reference before trying to get it, and it will
// cause an exception is there isn't one :(
MWWorld::Ptr bed;
try {
bed = R()(runtime);
}
catch(std::runtime_error&) {
}
MWWorld::Ptr bed = R()(runtime, false);
if (bed.isEmpty() || !MWBase::Environment::get().getMechanicsManager()->sleepInBed(MWBase::Environment::get().getWorld()->getPlayerPtr(),
bed))

View file

@ -23,7 +23,7 @@
namespace MWScript
{
MWWorld::Ptr InterpreterContext::getReference (
const std::string& id, bool activeOnly)
const std::string& id, bool activeOnly, bool doThrow)
{
if (!id.empty())
{
@ -31,7 +31,7 @@ namespace MWScript
}
else
{
if (mReference.isEmpty())
if (mReference.isEmpty() && doThrow)
throw std::runtime_error ("no implicit reference");
return mReference;
@ -39,7 +39,7 @@ namespace MWScript
}
const MWWorld::Ptr InterpreterContext::getReference (
const std::string& id, bool activeOnly) const
const std::string& id, bool activeOnly, bool doThrow) const
{
if (!id.empty())
{
@ -47,7 +47,7 @@ namespace MWScript
}
else
{
if (mReference.isEmpty())
if (mReference.isEmpty() && doThrow)
throw std::runtime_error ("no implicit reference");
return mReference;
@ -498,8 +498,8 @@ namespace MWScript
ptr.getRefData().getLocals().mFloats[index] = value;
}
MWWorld::Ptr InterpreterContext::getReference()
MWWorld::Ptr InterpreterContext::getReference(bool required)
{
return getReference ("", true);
return getReference ("", true, required);
}
}

View file

@ -33,9 +33,9 @@ namespace MWScript
bool mActivationHandled;
boost::shared_ptr<MWWorld::Action> mAction;
MWWorld::Ptr getReference (const std::string& id, bool activeOnly);
MWWorld::Ptr getReference (const std::string& id, bool activeOnly, bool doThrow=true);
const MWWorld::Ptr getReference (const std::string& id, bool activeOnly) const;
const MWWorld::Ptr getReference (const std::string& id, bool activeOnly, bool doThrow=true) const;
public:
@ -150,7 +150,7 @@ namespace MWScript
virtual void setMemberFloat (const std::string& id, const std::string& name, float value);
MWWorld::Ptr getReference();
MWWorld::Ptr getReference(bool required=true);
///< Reference, that the script is running from (can be empty)
};
}

View file

@ -699,13 +699,11 @@ namespace MWScript
public:
virtual void execute(Interpreter::Runtime& runtime)
{
// No way to tell if we have a reference before trying to get it, and it will
// cause an exception is there isn't one :(
try {
MWWorld::Ptr ptr = R()(runtime);
MWWorld::Ptr ptr = R()(runtime, false);
if (!ptr.isEmpty())
printLocalVars(runtime, ptr);
}
catch(std::runtime_error&) {
else
{
// No reference, no problem.
printGlobalVars(runtime);
}

View file

@ -16,7 +16,7 @@ namespace MWScript
{
struct ExplicitRef
{
MWWorld::Ptr operator() (Interpreter::Runtime& runtime) const
MWWorld::Ptr operator() (Interpreter::Runtime& runtime, bool required=true) const
{
std::string id = runtime.getStringLiteral (runtime[0].mInteger);
runtime.pop();
@ -27,12 +27,12 @@ namespace MWScript
struct ImplicitRef
{
MWWorld::Ptr operator() (Interpreter::Runtime& runtime) const
MWWorld::Ptr operator() (Interpreter::Runtime& runtime, bool required=true) const
{
MWScript::InterpreterContext& context
= static_cast<MWScript::InterpreterContext&> (runtime.getContext());
return context.getReference();
return context.getReference(required);
}
};
}

View file

@ -921,8 +921,6 @@ namespace MWScript
virtual void execute (Interpreter::Runtime& runtime, unsigned int arg0)
{
MWWorld::Ptr ptr = R()(runtime);
std::string factionID = "";
if(arg0 >0 )
{
@ -931,6 +929,7 @@ namespace MWScript
}
else
{
MWWorld::Ptr ptr = R()(runtime);
if(MWWorld::Class::get(ptr).getNpcStats(ptr).getFactionRanks().empty())
{
factionID = "";
@ -955,8 +954,6 @@ namespace MWScript
virtual void execute (Interpreter::Runtime& runtime, unsigned int arg0)
{
MWWorld::Ptr ptr = R()(runtime);
std::string factionID = "";
if(arg0 >0 )
{
@ -965,6 +962,7 @@ namespace MWScript
}
else
{
MWWorld::Ptr ptr = R()(runtime);
if(MWWorld::Class::get(ptr).getNpcStats(ptr).getFactionRanks().empty())
{
factionID = "";