Jump to content
Sign in to follow this  
CarlGustaffa

oo - addClass and createObject, how to use?

Recommended Posts

...\ArmA 2\Expansion\Addons\modules_e\oo\data\scripts\functions

has some interesting stuff. I think, lol. Can anyone explain what they do and how to use it?

Share this post


Link to post
Share on other sites

Uhh, surely there must be someone around who have messed with these things? What is oo all about anyway?

Share this post


Link to post
Share on other sites

I know the basics of OO, classes and properties and whatnot, but no idea how this works in ArmA. :)

Share this post


Link to post
Share on other sites

This is a module which provides a scripted framework enabling a limited Object Oriented design for any system :)

I developed it for an as of yet unreleased project. In complex systems, an OO architecture brings many benefits. This framework definitely does not support all features of typical OO approaches, but it does do: classes, objects (instances), attributes, methods, message passing, inheritance, abstraction, encapsulation ;)

Basically you define an architecture in a special CfgOO class and the system then automatically creates the classes and compiles the methods (in functions). You can then instantiate objects of a class, which spawns Logic objects with all attributes initiated as variables. Invoking the methods automatically does parameter validation and generates a debug trace.

Since this is a scripted solution and not (yet) part of our engine, it does create some overhead. However, I found the advantages to be huge while implementing and debugging my system. When I have some more time I will try to document the OO module as it is present in OA now :)

Share this post


Link to post
Share on other sites

Oh, so we're talking about special CfgOO classes and not creating/instancing classes in, well, lets say cfgAmmo that we could use in scripts? Or on the fly creating unit classes with different displaynames because the mission needed it? Naah, too wild an idea :p

So, it's all about object oriented programming then? I can't say I understand the concept (I'm not a programmer, and no software engineer) fully. But I'm looking forward to the documentation, and see how others use it, even though I fear it may be slightly over my head :)

Share this post


Link to post
Share on other sites
Naah, too wild an idea :P

Nothing is too wild, but it's not what this module is about ;)

So, it's all about object oriented programming then?

Object Oriented scripting yeah ... it mostly allows for neat data and responsibility organisation.

Say your system simulates a car, you can identify Car, Wheel, Engine, and many more classes. Each class holds the data specific to that component (e.g. pressure in Wheel and horsepower in Engine) and it performs the operations specific to that component (e.g. start() in Engine).

Now you will need four wheels for most cars, so you instantiate four objects of the Wheel class. Automatically all these objects contain the pressure attribute. This means you do not have to, say, use four different global variables ... and when you add a new bit of data to your Wheel later, all instantiated objects receive this data!

That is just a very basic overview, but perhaps you can see some of the benefits when creating complex scripted systems :)

Share this post


Link to post
Share on other sites
This is a module which provides a scripted framework enabling a limited Object Oriented design for any system :)

I developed it for an as of yet unreleased project. In complex systems, an OO architecture brings many benefits. This framework definitely does not support all features of typical OO approaches, but it does do: classes, objects (instances), attributes, methods, message passing, inheritance, abstraction, encapsulation ;)

When I have some more time I will try to document the OO module as it is present in OA now :)

Since I want to start a new project from scratch and also want to play with machine learning algorithms, an OO framework would be quite handy. Any chance that you give a very brief overview or refer to an example how to do this :)?

Is it planned to add OO as a core feature into ArmaScript? That and a real debugger would be just so cool :cool:

Share this post


Link to post
Share on other sites

Or if there is no time for writing a docu, perhaps just attach an existing example for self-study?

Share this post


Link to post
Share on other sites

I'm not sure all of the following is present in your version yet, but here is a sample which may help you get started. Again, this is a very limited approach to OO scripting, but it has helped me in many ways already :)

Config:

class CfgOO
{
[color="DarkGreen"]//Name used in method prefix[/color]
class MyClass 
{
	class Attributes 
	{
		[color="DarkGreen"]//Name used in Logic variable space
		//Initialized with default value for this type[/color]
		class myAttribute {type = "SCALAR";};
	};
	class Methods 
	{
		[color="DarkGreen"]//Constructor method[/color]
		class MyClass 
		{
			[color="DarkGreen"]//Amount of type of provided parameters are validated when invoking methods[/color]
			parameterTypes[] = {"OBJECT"};
			[color="DarkGreen"]//This script file implements the method as a function[/color]
			script = "some_path\data\scripts\classes\MyClass\MyClass.sqf";
		};
		class myMethod 
		{
			parameterTypes[] = {"OBJECT", "SCALAR", "STRING"};
			[color="DarkGreen"]//The default return value is based on this[/color]
			returnType = "SCALAR";
			script = "some_path\data\scripts\classes\MyClass\myMethod.sqf";
		};
	};
};
};

Script:

[color="DarkGreen"]//Precompiles all methods into global variables
//Naming convention: BIS_OO_<Class>_<method>
//Here: BIS_OO_MyClass_MyClass and BIS_OO_MyClass_myMethod[/color]
"MyClass" call (compile (preprocessFileLineNumbers "ca\modules_e\oo\data\scripts\functions\addClass.sqf"));

[color="DarkGreen"]//Creates a Logic with all attributes initialized
//Invokes the constructor[/color]
private ["_myClassObject"];
_myClassObject = ["MyClass"] call BIS_OO_createObject;

[color="DarkGreen"]//Invoking a method and getting an attribute[/color]
private ["_return", "_value"];
_return = [player, 1, "Hello"] call BIS_OO_MyClass_myMethod;
_value = _myClassObject getVariable "myAttribute";

Share this post


Link to post
Share on other sites

Oooh does this mean i can write classes that simulate 'things' that dont need to be part of the ArmA2 config?

Lets say i need to simulate a bank account in a mission i'm making. A class, methods and attributes of bank accounts are surely nowhere to be found in the ArmA2 config.cpp

So what i'm doing is i'm providing all that i need in a MyBankaccount class?

And the methods are in the interface between the object at runtime and action menu entries added via *.sqf i write for my methods?

Like Get_bankaccount.sqf would be activated via some action menu entry?

Now that would rock.

Share this post


Link to post
Share on other sites

Hmm. The ability to make configurations without having to roll up a pbo comes to mind. That would be a boon for those that dislike using addons.

Could see a LOT of uses for this. Way kewl.

Share this post


Link to post
Share on other sites
I'm not sure all of the following is present in your version yet, but here is a sample which may help you get started. Again, this is a very limited approach to OO scripting, but it has helped me in many ways already :)

I've played with it and after a bug fixing ;) in validateParameters.sqf (the logic did not consider that the _logic was always the first parameter in the array if the method to be checked is a constructor, thus it rejected a scalar constructor parameter...) a simple example works^^.

I'd have the following questions:

  • I assume inheritance is not support in the released code, yet?
  • I'd expect that in instance methods you also get the _logic passed so that you have access to the instance (similar to _self in python)...or do I oversee sth?

Hope, you guys continue that OO-topic^^

Is that OO-approach only for the time being, until it is replaced by a built-in language construct?. If not, a tool to generate the method stubs by parsing the config, would be quite handy^^

Share this post


Link to post
Share on other sites

Documentation posted in http://community.bistudio.com/wiki/Object_Oriented_scripting_shell :)

I assume inheritance is not support in the released code, yet?

Configs support inheritance, so the definition of the classes could use it, but in the current approach it's very limited (it was not necessary for the primary application).

I'd expect that in instance methods you also get the _logic passed so that you have access to the instance

That would have actually been really nice (and logical), but unfortunately for now you'll have to manually pass the Logic.

Is that OO-approach only for the time being, until it is replaced by a built-in language construct?

We'll see what future projects bring. It's certainly a direction we're going in, so I would expect this functionality to eventually become implemented by language constructs.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×