CS Plugin System Help

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
User avatar
grimdoomer




System Engineer

Posts: 1440
Joined: Mon Oct 09, 2006 4:36 pm

CS Plugin System Help

Post by grimdoomer »

Well I'm working on a couple apps that depend on a modified version of shade45 and Anthonys CS plugin system. Now here is my issue. I parse out the tag, then I want to filter the reflexives into a list. But I can't seem to find the right object to get the value with. Here is my code:

Code: Select all

public Chunk Plugin;

private void FilterReflexives()
        {
            FieldInfo[] fields = Plugin.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
            for (int i = 0; i < fields.Length; i++)
            {
                FieldInfo f = fields[i];
                if (f.FieldType.IsGenericType && (f.FieldType.GetGenericTypeDefinition() == typeof(Reflexive<>)))
                {
                    Reflexives.Add((Reflexive<Chunk>)f.GetValue(Plugin));
                    Reflexives.AddRange(((Reflexive<Chunk>)f.GetValue(Plugin)).FilterReflexives().ToArray());
                }
            }
        }
Here is the exception:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'HaloObjects.Reflexive`1[HaloObjects.sbsp+Collision_MaterialsBlock]' to type 'HaloObjects.Reflexive`1[HaloObjects.Chunk]'."

Note: I set the fields value with the Plugin object, but it doesn't work when I try to retrive it that way.
Image
AI Zones in MP | Ambiance | Gravemind Beta v1.1
Aumaan Anubis wrote:Grimdoomer. The first person ever to mod Halo 2 Vista.
User avatar
LuxuriousMeat





Posts: 824
Joined: Thu Nov 03, 2005 6:43 pm
Location: zzzzzzzzzzzzzzzz
Contact:

Post by LuxuriousMeat »

Does "HaloObjects.sbsp.Collision_MaterialsBlock" inherit from "HaloObjects.Chunk"?
Image
User avatar
grimdoomer




System Engineer

Posts: 1440
Joined: Mon Oct 09, 2006 4:36 pm

Post by grimdoomer »

Yes.
Image
AI Zones in MP | Ambiance | Gravemind Beta v1.1
Aumaan Anubis wrote:Grimdoomer. The first person ever to mod Halo 2 Vista.
User avatar
kornman00




ONI New Age

Posts: 146
Joined: Fri Dec 12, 2003 6:30 pm
Contact:

Post by kornman00 »

Even if the type parameter used in a generic type is a class which inherits from a class you're using in another instance of the same generic type, you can't cast it.

In simpilar terms, you can't do the following:

Code: Select all

List<StreamWriter> writers = ...

List<Stream> streams = writers; // can't. same with explicit conversion or an 'as' operator
With that said, what you're trying to do with that code isn't feasible in its current direction. Which is why they made abstraction
User avatar
grimdoomer




System Engineer

Posts: 1440
Joined: Mon Oct 09, 2006 4:36 pm

Post by grimdoomer »

Ok so if my reflexive look like this:

Reflexive<Collision_MaterialsBlock> Collision_MaterialsBlock
Reflexive<Collision_BspBlock> Collision_BspBlock
Reflexive<LeavesBlock> LeavesBlock

How do I get them like this:

Reflexive<Chunk> Collision_MaterialsBlock
Reflexive<Chunk> Collision_BspBlock
Reflexive<Chunk> LeavesBlock

If I can't cast them to a class they inherit?
Image
AI Zones in MP | Ambiance | Gravemind Beta v1.1
Aumaan Anubis wrote:Grimdoomer. The first person ever to mod Halo 2 Vista.
User avatar
kornman00




ONI New Age

Posts: 146
Joined: Fri Dec 12, 2003 6:30 pm
Contact:

Post by kornman00 »

The thing is, you're not casting them to a class they inherit from. "Reflexive<Collision_MaterialsBlock>" doesn't inherit from "Reflexive<Chunk>".

You're going to have to create a "Reflexive<Chunk>" then add each element in "Reflexive<Collision_MaterialsBlock>" one by one, or call List<>.ToArray() to add it by range. However this is not performance nor memory (let alone design) efficient. I suggest you actually learn about generic types and also about interfaces before you try to implement what you're trying to do.
User avatar
grimdoomer




System Engineer

Posts: 1440
Joined: Mon Oct 09, 2006 4:36 pm

Post by grimdoomer »

After looking at some of shde45 and Anthonys code again, I saw they converted it to an IList. After try it myself I got it to work.

I know this isn't efficient as it takes a lot longer then xml. But I like it because it automatically parses the tag.
Image
AI Zones in MP | Ambiance | Gravemind Beta v1.1
Aumaan Anubis wrote:Grimdoomer. The first person ever to mod Halo 2 Vista.
User avatar
kornman00




ONI New Age

Posts: 146
Joined: Fri Dec 12, 2003 6:30 pm
Contact:

Post by kornman00 »

There are more efficient ways to implement what you're doing, while keeping the definition process in actual source code and not XML. I didn't mean to sound as if I was backing the xml avenue of approach. But you got what you wanted, so all is well in the end I guess
User avatar
grimdoomer




System Engineer

Posts: 1440
Joined: Mon Oct 09, 2006 4:36 pm

Post by grimdoomer »

How would you do it?
Image
AI Zones in MP | Ambiance | Gravemind Beta v1.1
Aumaan Anubis wrote:Grimdoomer. The first person ever to mod Halo 2 Vista.
Post Reply