Script: GetName() can return the property a proplist was defined in

This means that the ActMaps do not need to repeat their name anymore,
provided that all Scripts use GetName() instead of directly accessing .Name.
liquid_container
Günther Brammer 2014-04-20 22:05:50 +02:00
parent 5a470f51f8
commit e5cfb1858f
5 changed files with 43 additions and 14 deletions

View File

@ -7,8 +7,26 @@
<title>GetName</title>
<category>Objects</category>
<version>5.1 OC</version>
<syntax><rtype>string</rtype></syntax>
<desc>Returns the name of an object or of an object definition. If the object does not have a name of its own, the definition name is returned anyway.</desc>
<syntax>
<rtype>string</rtype>
<params>
<param>
<type>bool</type>
<name>truename</name>
<desc>Returns only the constant in which it was defined, ignoring the <code>Name</code> property.</desc>
</param>
</params>
</syntax>
<desc>Returns the name of a proplist. This is either the contents of the <code>Name</code> property, or if that doesn't exist or the true name was requested, the name of the constant in which it was defined.</desc>
<examples>
<example>
<code>
static const Bee = { Buzz = func() {} };
func Poke(proplist animal) {
if (animal->GetName(true) == "Bee") animal->Buzz();
}</code>
</example>
</examples>
</func>
<author>jwk</author><date>2002-06</date>
<author>Günther</author><date>2014</date>
</funcs>

View File

@ -196,14 +196,6 @@ static long FnGetCon(C4Object *Obj, long iPrec)
return iPrec*Obj->GetCon()/FullCon;
}
static C4String *FnGetName(C4PropList * _this)
{
if (!_this)
throw NeedNonGlobalContext("GetName");
else
return String(_this->GetName());
}
static bool FnSetName(C4PropList * _this, C4String *pNewName, bool fSetInInfo, bool fMakeValidIfExists)
{
if (!Object(_this))
@ -2611,7 +2603,6 @@ void InitObjectFunctionMap(C4AulScriptEngine *pEngine)
::AddFunc(p, "SetContactDensity", FnSetContactDensity, false);
F(GetController);
F(SetController);
F(GetName);
F(SetName);
F(GetKiller);
F(SetKiller);

View File

@ -264,6 +264,14 @@ StdStrBuf C4PropListStatic::GetDataString() const
return r;
}
const char *C4PropListStatic::GetName() const
{
const C4String * s = GetPropertyStr(P_Name);
if (!s) s = ParentKeyName;
if (!s) return "";
return s->GetCStr();
}
C4PropList::C4PropList(C4PropList * prototype):
FirstRef(NULL), prototype(prototype),
constant(false), Status(1)

View File

@ -66,7 +66,7 @@ class C4PropList
{
public:
void Clear() { constant = false; Properties.Clear(); prototype.Set0(); }
const char *GetName() const;
virtual const char *GetName() const;
virtual void SetName (const char *NewName = 0);
virtual void SetOnFire(bool OnFire) { }
@ -255,8 +255,9 @@ public:
virtual C4PropListStatic * IsStatic() { return this; }
void RefCompileFunc(StdCompiler *pComp, C4ValueNumbers * numbers) const;
StdStrBuf GetDataString() const;
virtual const char *GetName() const;
const C4PropListStatic * GetParent() { return Parent; }
const C4String * GetParentKeyName() { return ParentKeyName; }
C4String * GetParentKeyName() { return ParentKeyName; }
protected:
const C4PropListStatic * Parent;
C4RefCntPointer<C4String> ParentKeyName; // property in parent this proplist was created in

View File

@ -327,6 +327,16 @@ static C4Value FnCall(C4PropList * _this, C4Value * Pars)
return fn->Exec(_this, &ParSet, true);
}
static C4String *FnGetName(C4PropList * _this, bool truename)
{
if (!_this)
throw NeedNonGlobalContext("GetName");
else if(truename)
return _this->IsStatic() ? _this->IsStatic()->GetParentKeyName() : nullptr;
else
return String(_this->GetName());
}
/* Effects */
static C4Value FnAddEffect(C4PropList * _this, C4String * szEffect, C4PropList * pTarget,
@ -934,6 +944,7 @@ void InitCoreFunctionMap(C4AulScriptEngine *pEngine)
F(GetProperty);
F(SetProperty);
F(ResetProperty);
F(GetName);
F(AddEffect);
F(CheckEffect);
F(RemoveEffect);