Page MenuHomeFeedback Tracker

Extend typename script API to retrieve attribute information
Assigned, NormalPublic

Description

Summary
Add additional script API functions to the typename class to be able to fetch meta-information about class and member annotations via attributes. This increases refactorability when using annotation setups in script because less info needs to be hardcoded as a workaround into the attribute params to give them context.

The updated API could look like the code below. It features one function set for retrieving class-level attributes and one for member-level attributes.
In theory function can have attributes as well, but to address functions we will some more API changes that will need a bit more thought. The use case for them is not as valuable in my opinion.

Variant 1 using counts

sealed class typename
{
	....
	proto external int GetAttributeCount();
	proto external owned Class GetAttributeValue(int aIdx);

	proto external int GetVariableAttributeCount(int vIdx);
	proto external owned Class GetVariableAttributeValue(int vIdx, int aIdx);
}

Variant 2 using get by typename

sealed class typename
{
	....
	proto external owned Class GetAttributeValue(typename attributeType);
	proto external owned Class GetVariableAttributeValue(int vIdx, typename attributeType);
}

The difference between the two variants is that the second one is easier to use because you directly ask for a kind of attribute you want to get, but that way you only get one result. If there are 3 attributes on a class that share a base class that would not work. The count variant 1 would work in any scenario.

Why is this needed?
Right now attributes can not be used in script without hardcoding the context into the attribute parameters. They are instantiated 1-N times globally at compile time and not not when the class is instantiated. So even though an attribute might be next to a member variable, the script has no idea about this. Using the updated API a use-case as below becomes possible.

OLD: Code example without the change, using bad hardcoded names for context. The class typename and variable name need to be passed in so the attribute knows anything about the variable it is put above.

class EL_DbIndex
{
	void EL_DbIndex(typename entityType, string indexVariableName)
	{
		EL_DbIndexCollection.DB_INDICES.Get(entityType).Insert(indexVariableName);
	}
}

class EL_DbIndexCollection
{
	static const ref map<string, ref array<string>> DB_INDICES = new map<string, ref array<string>>();
}

class EL_PlayerRecord
{
	[EL_DbIndex(EL_PlayerRecord, "m_PlayerId")]
	protected string m_PlayerId;
	
	[EL_DbIndex(EL_PlayerRecord, "m_PlayerName")]
	protected string m_PlayerName;
	
	protected int m_MoreData;
}

void SavePlayerRecord(EL_PlayerRecord record)
{
	array<string> indices = EL_DbIndexCollection.DB_INDICES.Get(record.Type());
}

NEW: The clean and dynamic variant with the proposed updated script API - using the proposed variant 1 with counts instead of typename fetch

class EL_DbIndex
{
	void EL_DbIndex()
	{
	}
}

class EL_PlayerRecord
{
	[EL_DbIndex()]
	protected string m_PlayerId;
	
	[EL_DbIndex()]
	protected string m_PlayerName;
	
	protected int m_MoreData;
}

void SavePlayerRecord(EL_PlayerRecord record)
{
	array<string> indices();
	
	int nVariables = record.Type().GetVariableCount();
	
	for(int nVariable = 0; nVariable < nVariables; nVariable++)
	{
		int nAttributes = record.Type().GetVariableAttributeCount(nVariable);
		
		for(int nAttribute = 0; nAttribute < nAttributes; nAttribute++)
		{
			Class attribute = record.Type().GetVariableAttributeValue(nVariable, nAttribute);
			
			//Check if an attribute was returned, null if no attribute for that index.
			//Check if attribute is of the type we care about
			if(attribute && attribute.Type().IsInherited(EL_DbIndex))
			{
				//Do something with the variable that was annotated to be relevant
				string variableName = record.Type().GetVariableName(nVariable);
				
				indices.Insert(variableName);
			}
		}
	}
}

Details

Severity
Minor
Resolution
Open
Reproducibility
N/A
Operating System
Windows 10 x64
Category
General

Event Timeline

Arkensor updated the task description. (Show Details)Jun 29 2022, 1:12 PM
Arkensor updated the task description. (Show Details)
Arkensor updated the task description. (Show Details)Jun 29 2022, 1:34 PM
Arkensor updated the task description. (Show Details)
Geez changed the task status from New to Assigned.Jul 11 2022, 11:28 PM