This forum is locked and will eventually go offline. If you have feedback to share you can find us in our Discord channel "MythoLogic Interactive" https://discord.gg/nECKnbT7gk

Forum rules

Unable to call an array for IObjectTrigger?

Here you can find answered ScriptAPI topics.
Forum rules
By using the forum you agree to the following rules.
Locked
User avatar
TheOriginalCj
Moderator
Moderator
Posts: 177
Joined: Tue Mar 15, 2016 10:28 pm
Title: Lifetime Sentence to SF Wiki
SFD Alias: RedneckJed
Started SFD: PreAlpha 1.1.0
Location: USA
Gender:
Contact:

Unable to call an array for IObjectTrigger?

Post by TheOriginalCj » Thu Apr 14, 2016 7:49 am

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.
0 x
Superfighters Wiki Founder
The Superfighters Wiki
Join the Wiki!
Danger Ross wrote: What are you doing here wiki-slave?! GET BACK TO WORK!

User avatar
gwendalaze
Superfighter
Superfighter
Posts: 84
Joined: Sat Mar 19, 2016 12:55 pm
Title: Jarate Yellow Belt
Started SFD: PreAlpha 1.1.4
Location: France

Post by gwendalaze » Thu Apr 14, 2016 4:37 pm

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;
	}
0 x
- Gwendalaze, failing at being fun, just like this signature

User avatar
TheOriginalCj
Moderator
Moderator
Posts: 177
Joined: Tue Mar 15, 2016 10:28 pm
Title: Lifetime Sentence to SF Wiki
SFD Alias: RedneckJed
Started SFD: PreAlpha 1.1.0
Location: USA
Gender:
Contact:

Post by TheOriginalCj » Thu Apr 14, 2016 5:48 pm

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.
0 x
Superfighters Wiki Founder
The Superfighters Wiki
Join the Wiki!
Danger Ross wrote: What are you doing here wiki-slave?! GET BACK TO WORK!

User avatar
gwendalaze
Superfighter
Superfighter
Posts: 84
Joined: Sat Mar 19, 2016 12:55 pm
Title: Jarate Yellow Belt
Started SFD: PreAlpha 1.1.4
Location: France

Post by gwendalaze » Sat Apr 16, 2016 1:23 am

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);
   }
0 x
- Gwendalaze, failing at being fun, just like this signature

User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1887
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 36

Post by Gurt » Sat Apr 16, 2016 10:08 am

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.
0 x
Gurt

User avatar
gwendalaze
Superfighter
Superfighter
Posts: 84
Joined: Sat Mar 19, 2016 12:55 pm
Title: Jarate Yellow Belt
Started SFD: PreAlpha 1.1.4
Location: France

Post by gwendalaze » Sat Apr 16, 2016 12:26 pm

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 ?
0 x
- Gwendalaze, failing at being fun, just like this signature

User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1887
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 36

Post by Gurt » Sat Apr 16, 2016 1:24 pm

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.
1 x
Gurt

User avatar
TheOriginalCj
Moderator
Moderator
Posts: 177
Joined: Tue Mar 15, 2016 10:28 pm
Title: Lifetime Sentence to SF Wiki
SFD Alias: RedneckJed
Started SFD: PreAlpha 1.1.0
Location: USA
Gender:
Contact:

Post by TheOriginalCj » Sun Apr 17, 2016 7:34 am

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.
0 x
Superfighters Wiki Founder
The Superfighters Wiki
Join the Wiki!
Danger Ross wrote: What are you doing here wiki-slave?! GET BACK TO WORK!

User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1887
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 36

Post by Gurt » Sun Apr 17, 2016 1:40 pm

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;
}
0 x
Gurt

Locked