Hello,
For over a year since I’m here, I have understand one main thing (among a lot of wonders):
If you find yourself writing a lot of code to make something work, it means you are on the wrong way.
Either you are trying to implement something so special that your users will need a big documentation to use it, or you didn’t find the correct method of the correct class.
I develop applications for children, so I can’t ask them to read a thick user manual before accessing the keyboard. Some of them come from the PC world, so things have to be even more simpler on a new machine.
I’m not a professional programmer, so I don’t have the time to master 300 classes with their dozens of methods each. Days only have 24 hours.
That’s why this site is very, very precious to me and people like me.
Now for the problem (ahem).
I have some objets (they are objects in the OOP sense: abstractions for real objects like a door, a lantern, a dragon and so on) collected in an array (I like to keep an eye on my objects and I know NSArrays are more smart than “just arrays”. They can be enumerated, filtered with predicates, merged and so on).
I want to give my objects some artificial intelligence (a really basic one). I want them to be aware at what happens to others, and react accordingly. The base of this “intelligence” is given by a (property) status, which makes them capable to receive certain messages and execute certain actions.
“Ha ha, you are trying to implement a finite state automaton using objects!”
Well, some of. Let’s say we have a DOOR object: it status is «isClosed» or «isOpen». If it’s «isClosed», it can receive the message “open”, but this message must be sent by a KEY object. If a CAN OPENER sends the “open” message, the DOOR will respond “I can’t be opened with a can opener!”. When the KEY sends the “open” message to the DOOR, the status of the DOOR become «isOpen».
But the KEY must have the status «isCarried» to send the “open” message". If its status is «isBroken» it can’t send the message. Got the picture?
This is my Model part: a collection of objects whose status (ok, “state”) allows to send or receive specific messages.
The View part consist of a textual description of the object to be inserted into a NSTextView, like “There is a door in front of you. The door is closed.” and some buttons which appear when the DOOR is selected, like “Open”, “Break” or “Leave alone”. Clicking on “Open” will, of course, send the message to the DOOR, even if the KEY is not carried: in that case, a dialog will appear, saying “You can’t open the door. It is locked”. If the key is carried, the text becomes “There is a door in front of you. The door opens on a dark corridor.”
Note: this example is just to demonstrate the concept: you may replace the door and the key with a dragon and a magic sword.
So the main problem is the Controller part. That’s why I need your cocoa’s knowledge because I need a good start:
How make my objects “aware” of the other object status? Do they have to? For example, a PARCHMENT can be read if the user carries a LAMP, so it has to “know” the LAMP status is «isCarried» (or that the LAMP is into a “carried things” array).
Oh, “One more thing.”: the objects, their characteristics, their initial status, the condition chain to change the status, everything will be. defined by the user himself before the game begins!
My app is NOT a game. It is a Game Maker.
That’s it. Interested to help me? Consider this as a shared challenge. There could be a lot of things to discover about new classes, arrays, enumerators, messages, class/instance variables, maybe useful for other (more serious) applications.
Thanks for sharing!