Jump to content

Component & Property/Attribute design


Rick
 Share

Recommended Posts

>Looking good Davaris. Have you tried to get an real world example working yet?

 

Thanks. Its still early days yet. I have to add a few features (XML, a variable watcher and event sender, a way to share the parent's data with the whole system), before I'll be ready to start on the action objects that use it.

 

I am also thinking of making it accessible from LUA, so I can use LUA scripts for my actions instead of C#, but I don't know if that is possible in Leadwerks.

 

I have a question, do you think we should make these property bags thread safe?

 

 

 

BTW you can get the source for the LinkedHashMap here:

 

http://gforge.opensource-sw.net/gf/project/flexsdk/scmsvn/?action=browse&path=%2Ftrunk%2Fswfutils%2FJavaCompatibleClasses%2FLinkedHashMap.cs&revision=2&view=markup&pathrev=34

 

It is a C# port taken from part of Adobe's FlexSDK, however I think there is a bug in it which can be fixed by doing the following:

 

// (search for) m_Tail.m_Next = node;

// (add this line underneath) node.m_Previous = m_Tail;

 

 

The reason LinkedHashMap is so useful for our purposes, is everything is maintained in its original insertion order.

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

I'm much closer to finishing now. I've wrapped my property class in a decorator class that stores a second parent pointer, so I can have different hierarchies of property bags that share the same property bags (if that makes any sense :) ).

 

I've also written some messy classes (they won't win any beauty contests) that watch and interact with the property bags, using the Observer Pattern.

 

Pointers are shared between these classes, however if one or the other is deleted, they tell each other to Detach immediately (this area needs more testing). After that, it is just XML and dlls and I'll be ready to start trying to do things in LWs.

 

Have you tried to get an real world example working yet?

 

 

If you are worried that this will run slow, I don't think it will. In fact, I think it will be super fast, if used correctly because it is event driven.

 

 

This is where the bulk of work is done in my Action class. There will be one Action class for any action I invent for my games. As you can see, nothing happens until a variable it is listening for, changes in one of the property bags it is linked to.

 

   // The property bags send you messages when the
   // variables in them you are watching change.
   // You can also read or change the bags contents. 
   // You can create new actions from here as well.

   public override void OnNotify<T>(string owner_name, string opcode, string key, object val)
   {
       if (opcode != "Put") return;

       Console.WriteLine("{0} {1} has changed to {2}", owner_name, key, val);

       if (key == "uintKey2") // a message from bag1
       {
           float float_val = 0;
           // Get Speed from bag 2
           if (mSubject2.Get<float>("Speed", ref float_val))
           {
               Console.WriteLine("{0} Speed is {1}", mSubject2.Name, float_val);

               // Change Speed in bag 2
               mSubject2.Put<float>("Speed", float_val + 100);
           }
       }
       if (key == "Strength")
       {
       }
       if (key == "Speed")
       {
       }
       if (key == "JUNK")
       {
       }
       if (key == "JUNK2")
       {
       }
       if (key == "JUNK3")
       {
       }
   }

 

 

 

My action class also has an Update member, so if you need to you can keep a copy of the variable changes from OnNotify, so there is no need for costly lookups every tick or frame, or whatever they call it.

 

    
public void OnUpdate()
{
}

 

Output:

 

child1 unitKey2 has changed to 123

child1 Speed is 101

child2 Speed has changed to 201

 

 

Explanation:

 

child1, child2 are the names of the property bags mSubject1, mSubject2.

 

1) I changed the value of 'unitKey2' in 'mSubject1' (bag1) in my main loop and my action object was notified and received the changed variable in OnNotify<T>.

 

2) My Action object then reads the value of 'Speed' from 'mSubject2' (bag2) and adds 100 to it.

 

3) My action class is notified of the change it just made to 'Speed' in 'mSubject2', as are any other Action objects that are also watching it.

 

 

 

EDIT:

It just occurred to me that I may need to stop the action class, from being notified of the changes it makes to any property bags it is watching.

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

If you are worried that this will run slow, I don't think it will. In fact, I think it will be super fast, if used correctly because it is event driven.

 

Oh no, don't get me wrong. I just wanted to see a real world example so I could understand it better.

Link to comment
Share on other sites

Oh no, don't get me wrong. I just wanted to see a real world example so I could understand it better.

 

I think what I've posted above explains it pretty well. At least I hope it does. :P As soon as I get it working in LWs I'll post it. It shouldn't be more than a week or so, if I stick with it.

 

Anyway, what I love about this system, is you can use it to write glorious spaghetti code! :)

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...