Page 1 of 1

Unable to call an array for IObjectTrigger?

Posted: Thu Apr 14, 2016 7:49 am
by TheOriginalCj
IObjectTrigger[] Olympus = Game.GetObjectsByCustomId("Sensory");

It's returning the error where it cannot convert implicitly. Either I've written something wrong or there's a serious issue with what I want to do.

Re: Unable to call an array for IObjectTrigger?

Posted: Thu Apr 14, 2016 4:37 pm
by gwendalaze
The thing is you are trying to put an IObject array into an IObjectTrigger array, that is inherited from simple IObjects. You need a type cast :

IObjectTrigger[] theOriginalCJArray = (IObjectTrigger [])Game.GetObjectsByCustomId("Id");

Note that you should add a failsafe of some sort

Example (may not be the best, but still works):

Code: Select all

	IObject [] obj = Game.GetObjectsByCustomId("Id");
	if (Array.TrueForAll<IObject>(obj,  o => o is IObjectTrigger)){
		IObjectTrigger [] objTrigger = (IObjectTrigger[])obj;
	}

Re: Unable to call an array for IObjectTrigger?

Posted: Thu Apr 14, 2016 5:48 pm
by TheOriginalCj
It compiles fine, but does not execute in game:
"Unable to cast object of type 'SFDGameScriptInterface.IObject[]' to type 'SFDGameScriptInterface.IObjectTrigger[]'."

It's unable to cast it as an IObjectTrigger for some reason. It's like a bitch and 3/4 to array for non-IObject entities. I need to use IObjectTrigger in order to access the Trigger Properties.

Re: Unable to call an array for IObjectTrigger?

Posted: Sat Apr 16, 2016 1:23 am
by gwendalaze
My bad, forgot that casting wont work with collections (ashamed emoji)

here is the working piece of code :

Code: Select all

	IObject [] obj = Game.GetObjectsByCustomId("Id");
   if (Array.TrueForAll<IObject>(obj,  o => o is IObjectTrigger)){
      IObjectTrigger[] newArray = Array.ConvertAll(obj, item => (IObjectTrigger)item);
   }

Re: Unable to call an array for IObjectTrigger?

Posted: Sat Apr 16, 2016 10:08 am
by Gurt
I'm adding the System.Linq namespace and assembly to the ScriptAPI for the next update so it can be done in one line like this:

Code: Select all

IEnumerable<IObjectTrigger> triggers = Game.GetObjectsByCustomID("MyID").Where(o => o is IObjectTrigger).Cast<IObjectTrigger>();

OR

IObjectTrigger[] triggers = Game.GetObjectsByCustomID("MyID").Where(o => o is IObjectTrigger).Cast<IObjectTrigger>().ToArray();
For now refer to gwendalaze's solution.

Re: Unable to call an array for IObjectTrigger?

Posted: Sat Apr 16, 2016 12:26 pm
by gwendalaze
Gurt wrote:I'm adding the System.Linq namespace and assembly to the ScriptAPI for the next update
I am wondering wether it would be possible to make so that users can include new libraries/assemblies/namespaces themselves ?

Re: Unable to call an array for IObjectTrigger?

Posted: Sat Apr 16, 2016 1:24 pm
by Gurt
gwendalaze wrote:
Gurt wrote:I'm adding the System.Linq namespace and assembly to the ScriptAPI for the next update
I am wondering wether it would be possible to make so that users can include new libraries/assemblies/namespaces themselves ?
I will not allow that due to security issues.

Re: Unable to call an array for IObjectTrigger?

Posted: Sun Apr 17, 2016 7:34 am
by TheOriginalCj
Getting back to the issue at hand. I used gwendalaze's reccomended fix, the condition compiles fine, but it doesn't assign the IObject array to IObjectTrigger array, it's almost as if it skips the assignment segment of the code.

EDIT: I forgot that when you run conversions it's only constant while in the if branch, not when it's in the function.

Re: Unable to call an array for IObjectTrigger?

Posted: Sun Apr 17, 2016 1:40 pm
by Gurt
You can also use a slightly more generic approach which can be reused for other types.
Bonus is that only those triggers will be returned even if you name triggers, joints, solid tiles and more with the same ID.

Code: Select all

public void OnStartup() {
	List<IObjectTrigger> triggers = GetTypedObjectsById<IObjectTrigger>("myTriggersId");
	List<IObjectAreaTrigger> areaTriggers = GetTypedObjectsById<IObjectAreaTrigger>("myAreaTriggersId");
}

static List<T> GetTypedObjectsById<T>(string id) where T : IObject {
	List<T> objects = new List<T>();
	foreach (IObject obj in Game.GetObjectsByCustomId(id))
		if (obj is T) { objects.Add((T)obj); }
	return objects;
}