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
Forum rules
Unable to call an array for IObjectTrigger?
Forum rules
By using the forum you agree to the following rules.
By using the forum you agree to the following rules.
- TheOriginalCj
- 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?
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.
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!
The Superfighters Wiki
Join the Wiki!
Danger Ross wrote: What are you doing here wiki-slave?! GET BACK TO WORK!
- gwendalaze
- Superfighter

- Posts: 84
- Joined: Sat Mar 19, 2016 12:55 pm
- Title: Jarate Yellow Belt
- Started SFD: PreAlpha 1.1.4
- Location: France
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):
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
- TheOriginalCj
- 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:
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.
"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!
The Superfighters Wiki
Join the Wiki!
Danger Ross wrote: What are you doing here wiki-slave?! GET BACK TO WORK!
- gwendalaze
- Superfighter

- Posts: 84
- Joined: Sat Mar 19, 2016 12:55 pm
- Title: Jarate Yellow Belt
- Started SFD: PreAlpha 1.1.4
- Location: France
My bad, forgot that casting wont work with collections (ashamed emoji)
here is the working piece of code :
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
- Gurt
- Lead Programmer

- Posts: 1887
- Joined: Sun Feb 28, 2016 3:22 pm
- Title: Lead programmer
- Started SFD: Made it!
- Location: Sweden
- Gender:
- Age: 36
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:
For now refer to gwendalaze's solution.
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();
0 x
Gurt
- gwendalaze
- Superfighter

- Posts: 84
- Joined: Sat Mar 19, 2016 12:55 pm
- Title: Jarate Yellow Belt
- Started SFD: PreAlpha 1.1.4
- Location: France
I am wondering wether it would be possible to make so that users can include new libraries/assemblies/namespaces themselves ?Gurt wrote:I'm adding the System.Linq namespace and assembly to the ScriptAPI for the next update
0 x
- Gwendalaze, failing at being fun, just like this signature
- Gurt
- Lead Programmer

- Posts: 1887
- Joined: Sun Feb 28, 2016 3:22 pm
- Title: Lead programmer
- Started SFD: Made it!
- Location: Sweden
- Gender:
- Age: 36
I will not allow that due to security issues.gwendalaze wrote:I am wondering wether it would be possible to make so that users can include new libraries/assemblies/namespaces themselves ?Gurt wrote:I'm adding the System.Linq namespace and assembly to the ScriptAPI for the next update
1 x
Gurt
- TheOriginalCj
- 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:
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.
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!
The Superfighters Wiki
Join the Wiki!
Danger Ross wrote: What are you doing here wiki-slave?! GET BACK TO WORK!
- Gurt
- Lead Programmer

- Posts: 1887
- Joined: Sun Feb 28, 2016 3:22 pm
- Title: Lead programmer
- Started SFD: Made it!
- Location: Sweden
- Gender:
- Age: 36
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.
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