SUGGESTION:OO:Extending Built In Objects

Started by Alynn, Thu 07/09/2006 14:47:24

Previous topic - Next topic

Alynn

Since AGS is going OO I thought of this during a smoke break (I do most of my thinking then).

Perhaps not true java like extention like Character2 extends Character but perhaps allowing us to attach our own functions to the Character object.

For instance I have a Util module for my game where I put Utility codes for self made functions to speed up development time. FaceDirection is in this module

Util.FaceDirection(cEgo, eDirUp);

If we could extend the functionality of current built in structs then we could make it so you could just type

player.FaceDirection(eDirUp);
cEgo.FaceDirection(eDirUp);

As far as how to do it in the editor we just put this in the header
Code: ags

enum FacingDirection{eDirUp, eDirRight, eDirDown, eDirLeft};

extention Character{
  static FaceDirection(FacingDirection dir);
}


And put the code in the global script or module (perhaps even create a new single extension file that handles all extensions for any object in the game).

Now I know this is not anything small, but the ability to extend functionality of any existing Class is one of the more powerful aspects of OOP. And I thought I'd put it out there.

SSH

AGS classes can already be extended, but the problem is that all the built-in objects are declared as those built-ins BEFORE any user scripting gets compiled.  If we could stick this in before somehow...


Code: ags

struct mychar extends Character {
  // my cool functions
};

#define Character mychar


would that work?
12

Alynn

#2
How it is done isn't as important to me (well not as important) as the final result. IE I can make my own functions for objects, characters, hotspots or whatever, and then able to use those in conjuction with the script names of those objects, characters, hotspots or whatever. So I can make a player.SayWithGreenLettersAndHatInHand("Sorry.") if I so choose.

On one hand, making it a simple Ok I extend it, here is what you to do would be great...

on the other, I could see this as needing to be a bit more complicated so we don't have neophytes (see that I didn't call them n00bs) breaking everything then coming here for help...


EDIT:
Ok I played around a bit... it seems in places that it will do what I want until I actually try to compile...

MyChar mEgo = cEgo;
  mEgo.FaceDirection(eUp);
  mEgo.Say("Test");
  mEgo.FaceDirection(eDown);
  mEgo.Say("Downtest"); 
  mEgo.FaceDirection(eUp);

throws cannot convert Character* to MyChar which I actually expected since they are different types... But I was trying to figure out a way to make the built in characters information into the MyChar extention struct.. which I would guess the only way to really do it would be to one for one convert them but that doesn't work with things like MyChar.ID = Character.ID since you can't change the ID field.

Now of course trying to keep it as cEgo throws the FaceDirection is not a public member of Character.

Basically I'm trying to figure out a way to hack it in there using the script language, but it looks like it's pretty locked down...

But anyway it was an experiment... and it failed...

Perhaps CJ could implement (as I said before) an extention script (like a module) that gets compiled with the game internals.

Unless there is something out there that I am completely missing. I'm trying not to assume too much with the scripting engine (which is why I didn't know about the extends keyword).

The reason I'm asking is because there have been a few changes I wasn't aware of with a few suggestions I've made before, and I want to make sure that this isn't one of those situations.

Pumaman

I've thought about this before, basically it all comes down to compile order. I guess it would need a special extra module-type thing that could get compiled before the main user code and be allowed to extend built-in types. I would like to do it, but we'll have to see.

Alynn

Thanks for at least looking into it CJ, I know it isn't anything tiny and I don't know how much work you would have to do to get it into the current system, but I know it won't be 3 lines of code.

Not knowing too much about your game internals, I'd make it that users can only create their own fuctions for the objects and not override any existing function (Error: Cannot override built in functions.). I guess you could have it do everthing it is supposed to do, then the user code, but honestly, just being able to attach your own functions to the built in objects is good enough, and overriding internal builds would probably cause too many issues if people were allowed to mess with them.

Oh, and I'd definately put a warning on the extention module that says "Hey you can totally hose your game if you mess with this stuff, so you better know what you are doing." Warning :)

SMF spam blocked by CleanTalk