Skip to content

1. Introduction, first steps

Nicolas BOITEUX edited this page Dec 27, 2017 · 10 revisions

Pre requirement

This page describe the minimun requirement to create your first class.

  1. First at all, create a new editable mission in your mpmissions folder (for example test.altis)
  2. Your folder must already contains a mission.sqm
  3. Download the last version of oop.h on git, and copy it into the directory
  4. Create a file init.sqf
  5. Add this line top to reference the oop.h #include "oop.h"

Ok then, it's all you should have to do for a ready to use environnement.

Create the first class

Now we will create your first class "OO_HELLOWORLD"

  1. Create a new file oo_helloworld.sqf that will contain the declaration of your class
  2. Add this line top to reference the oop.h #include "oop.h"
  3. Declare after the minimun definition of your class, like this

Example:

CLASS("OO_HELLOWORLD")

	PUBLIC FUNCTION("","constructor") {};

	PUBLIC FUNCTION("","deconstructor") {};

ENDCLASS;
  1. Save the file oo_helloworld.sqf, and insert this line in your init.sqf
  2. Add this line as first line call compile preprocessFileLineNumbers "oo_helloworld.sqf";

First Execution

At this time, your class is ready to use and can be instanciate. You can try it by adding this file at the end of your init.sqf

_myobjectreference = "new" call OO_HELLOWORLD;

or

_myobjectreference = NEW(OO_HELLOWORLD,nil);

Well you can try to launch the mission to be sure, there is no syntax problem. If all is done correctly, then nothing should be happen :) Normal, you don't ask to your object to do anything !

Evolution

Well, right now we will improve the HELLOWORLD object to do is real job as printing a hello world message and a bit more :)

  1. Open your oo_helloworld.sqf
  2. Add this line between class name declaration and constructor function

PRIVATE VARIABLE("string","name");

  1. Add this new function after constructor function

Example:

PUBLIC FUNCTION("","sayHelloWorld") {

	hint format ["Hello World from %1", MEMBER("name", nil)];

};
  1. Well at this time, you should understand it missing something concerning the value in format command.In fact, keyword MEMBER works as an accessor and retrive the value of private variable name declare at point 3. But at this time, this variable is not set, only declared.

  2. To set the variable name, add this line into the constructor function:

Example:

PUBLIC FUNCTION("","constructor") {

	MEMBER("name", "John");

};
  1. save the file, and come back to init.sqf to add this line:

"sayHelloWorld" call _myobjectreference;

  1. Congrats ! Check that all works as expected :)

Clone this wiki locally