Skip to content

Commit 14b73b9

Browse files
Simplify OOP tutorial
1 parent b956f86 commit 14b73b9

File tree

8 files changed

+108
-105
lines changed

8 files changed

+108
-105
lines changed

web/astro.config.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ export default defineConfig({
8585
{
8686
label: 'OOP',
8787
items: [
88-
{label: 'About OOP', link: 'OOP'},
89-
{label: 'Introduction', link: 'OOP_Introduction'},
90-
{label: 'Classes', link: 'OOP_Classes'},
88+
{label: 'About OOP', link: 'OOP_Introduction'},
89+
{label: 'OOP Classes', link: 'OOP_Classes'},
9190
]
9291
},
9392
]

web/public/_redirects

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
/Element/:elementname /:elementname
55

66
/Vector/:vectorclass /:vectorclass
7+
8+
/OOP /OOP_Introduction

web/src/pages/404.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
33
import { OLD_WIKI_URL, OLD_WIKI_REDIRECT } from '@src/content.constants';
44
const url = new URL(Astro.request.url);
55
const pathname = url.pathname;
6-
const redirectUrl = `${OLD_WIKI_URL}/${pathname}`;
6+
const redirectUrl = `${OLD_WIKI_URL}${pathname}`;
77
---
88

99
<StarlightPage frontmatter={{

web/src/pages/Element.astro

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
---
22
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
3+
import { getSeeAlsoLinksFromList } from '@src/utils/general';
4+
import SeeAlsoSection from '@src/components/SeeAlsoSection.astro';
35
import { getElementsByCategory } from '@src/utils/elements';
46
const elementsByCategory = getElementsByCategory();
57
---
@@ -12,8 +14,9 @@ const elementsByCategory = getElementsByCategory();
1214
<ul>
1315
{Object.entries(elementsByCategory).map(([category, elements], index) => (
1416
<section>
15-
<details {...(index === 0 ? { open: true } : {})}>
16-
<summary>{category}</summary>
17+
{/* <details {...(index === 0 ? { open: true } : {})}> */}
18+
<details>
19+
<summary>{category} elements</summary>
1720
<ul>
1821
{elements.map(element => (
1922
<li>
@@ -25,4 +28,8 @@ const elementsByCategory = getElementsByCategory();
2528
</section>
2629
))}
2730
</ul>
31+
32+
<SeeAlsoSection seeAlsoLinks={getSeeAlsoLinksFromList([
33+
'article:Element_tree',
34+
])} currentId='' />
2835
</StarlightPage>

web/src/pages/Element_tree.mdx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
2+
import { getSeeAlsoLinksFromList } from '@src/utils/general';
3+
import SeeAlsoSection from '@src/components/SeeAlsoSection.astro';
24
import { Image } from 'astro:assets';
35
import treeImage from '../../../assets/element_tree.webp';
46

@@ -7,11 +9,11 @@ import treeImage from '../../../assets/element_tree.webp';
79
title: 'Element tree',
810
tableOfContents: false,
911
}}>
10-
MTA uses a so-called *element tree* to store all the elements that exist on the server and the client. This is directly related to the set of running [[resources]] and their map files' XML layout, although it can be changed at run-time by scripts.
12+
MTA uses a so-called *element tree* to store all the elements that exist on the server and the client. This is directly related to the set of running [resources](/resource) and their map files' XML layout, although it can be changed at run-time by scripts.
1113

12-
If you are familiar with the concept of *trees* in computer-science, this should be easy to understand. If you are not, think of it as a family tree - except everyone only has a single parent. Every [[element]] has a *parent* element.
14+
If you are familiar with the concept of *trees* in computer-science, this should be easy to understand. If you are not, think of it as a family tree - except everyone only has a single parent. Every [element](/Element) has a *parent* element.
1315

14-
All elements that are created within scripts or from .map files are child elements of the resource they belong to. Thus, most elements (except for [[client]]s) exist only within resources and are also destroyed as soon as their resource is stopped.
16+
All elements that are created within scripts or from .map files are child elements of the resource they belong to. Thus, most elements (except for [players](/player)) exist only within resources and are also destroyed as soon as their resource is stopped.
1517

1618
<Image src={treeImage} alt="Element tree"/>
1719

@@ -25,7 +27,7 @@ getRootElement()
2527
getResourceRootElement()
2628
```
2729
* **map**: Each resource element contains at least one map element, representing either a ".map" file in the resource or the one containing the elements created by scripts (this is called the *dynamic* map). Their IDs contain the maps' filenames, or *dynamic* for the dynamic map.
28-
** Map files can contain a number of other [[element]]s as well as an unlimited number of custom elements.
30+
* Map files can contain a number of other [elements](/Element) as well as an unlimited number of custom elements.
2931

3032
### Example
3133
This in an example of a serverside tree dumped to XML from a running server.
@@ -80,11 +82,11 @@ This in an example of a serverside tree dumped to XML from a running server.
8082
</root>
8183
```
8284
### Explanation
83-
This tree consists of a number of resource root elements, the [[Element/Console|server console]] and two [[player]] elements, that are direct children of the **root** element. All these resources have a *dynamic map* as child element (it is just not shown for most of them). These contain the elements that are created dynamically by this resource using scripts, for example a [[vehicle]]. If the resource has a map file, it is also a child element, itself containing all the elements in the .map file.
85+
This tree consists of a number of resource root elements, the [Server Console](/console) and two [player](/player) elements, that are direct children of the **root** element. All these resources have a *dynamic map* as child element (it is just not shown for most of them). These contain the elements that are created dynamically by this resource using scripts, for example a [vehicle](/vehicle). If the resource has a map file, it is also a child element, itself containing all the elements in the .map file.
8486

8587
Let's have a closer look at the **assault** resource: This contains just one *dynamic* map that has 2 teams, 3 blips, 1 marker, and 1 colshape as child elements. These are the elements that are created by the script, for example, the marker, the colshape and one of the blips are probably used for the objective.
8688

87-
The **as-farm** resource's function on the contrary is to be a map for the **assault** gamemode. The dynamic map is empty (it could contain elements if there was a script in it though), while there is a map called 'as-farm.map', that contains a number of elements. These are mostly custom elements (like spawngroup, spawnarea, objective) but also a few elements that MTA creates automatically after loading the map (like pickup). In the brackets after the element type, you can see the element data it contains. These are identical with the attributes the .map file contains within these elements, while you can also set and get element data for any other elements (e.g. players) with [[setElementData]] and [[getElementData]].
89+
The **as-farm** resource's function on the contrary is to be a map for the **assault** gamemode. The dynamic map is empty (it could contain elements if there was a script in it though), while there is a map called 'as-farm.map', that contains a number of elements. These are mostly custom elements (like spawngroup, spawnarea, objective) but also a few elements that MTA creates automatically after loading the map (like pickup). In the brackets after the element type, you can see the element data it contains. These are identical with the attributes the .map file contains within these elements, while you can also set and get element data for any other elements (e.g. players) with [setElementData](/setElementData) and [getElementData](/getElementData).
8890

8991
### Practical application
9092
Elements can have as many children as they like. This does not directly affect the map in any way, but it comes into its own when combined with the scripting system.
@@ -100,12 +102,16 @@ setMarkerSize ( root, 2.5 )
100102
The same can be done on any element, it is not restricted to the root element.
101103

102104
### Map manager
103-
The [[#Example|example above]] shows the way the [[map manager]] uses different resources. The 'assault' resource is the gamemode, that manages what happens on the server using scripts and thus by creating elements in the tree dynamically. When a map resource is started, the gamemode receives a [[resource|resource pointer]] referring to the started resource - in this case *as-farm* - from which you can retrieve and store the resource root element. Using this element in conjunction with functions like [[getElementsByType]], [[getElementData]] and various others, you can access any of the information that was loaded into the tree from the 'as-farm.map'-file through scripts in the gamemode resource.
105+
The example above shows the way the [map manager](/Resource:mapmanager) uses different resources. The 'assault' resource is the gamemode, that manages what happens on the server using scripts and thus by creating elements in the tree dynamically. When a map resource is started, the gamemode receives a [resource pointer](/resource) referring to the started resource - in this case *as-farm* - from which you can retrieve and store the resource root element. Using this element in conjunction with functions like [getElementsByType](/getElementsByType), [getElementData](/getElementData) and various others, you can access any of the information that was loaded into the tree from the 'as-farm.map'-file through scripts in the gamemode resource.
104106

105107
Another thing that has to be considered related to the tree of elements is the fact that when you change the map, you don't have to remove any elements you created within the map resource, while you **do** have to remove elements that are created within the gamemode resource, **if** they are specific to the map (which will be probably the case for those items you create based on information read from the map resource's .map files).
106108

107109
### Element browser
108110
You can start the resource *elementbrowser* to see a live view of the element tree on your server. Just start the resource and browser to your server's web page and choose the *Element browser* option in the sidebar (firefox only currently).
109111

112+
<SeeAlsoSection seeAlsoLinks={getSeeAlsoLinksFromList([
113+
'article:Element',
114+
])} currentId='' />
115+
110116
</StarlightPage>
111117

web/src/pages/OOP.mdx

Lines changed: 0 additions & 88 deletions
This file was deleted.

web/src/pages/OOP_Classes.astro

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
---
22
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
3+
import { getSeeAlsoLinksFromList } from '@src/utils/general';
4+
import SeeAlsoSection from '@src/components/SeeAlsoSection.astro';
35
import { getOOPClassesByCategory } from '@src/utils/classes';
46
const classesByCategory = getOOPClassesByCategory();
57
---
@@ -23,4 +25,9 @@ const classesByCategory = getOOPClassesByCategory();
2325
</details>
2426
</section>
2527
))}
28+
29+
30+
<SeeAlsoSection seeAlsoLinks={getSeeAlsoLinksFromList([
31+
'article:OOP_Introduction',
32+
])} currentId='' />
2633
</StarlightPage>

web/src/pages/OOP_Introduction.mdx

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ import SeeAlsoSection from '@src/components/SeeAlsoSection.astro';
66

77
<StarlightPage frontmatter={{
88
template: 'doc',
9-
title: 'OOP Introduction',
9+
title: 'About OOP',
1010
tableOfContents: false,
1111
}}>
1212

13-
This is a scripting tutorial explaining to you what **object orientated programming** is and teaching you how to use the **OOP features of MTA**. This was originally created by qaisjp on June 8, 2014 ([Forum post](https://forum.multitheftauto.com/topic/65029-wikitut-oop-introduction/)).
1413

15-
### Introduction to OOP
14+
Object Orientated Programming was introduced in MTA:SA 1.4 and comes with special utility classes like [Vector](/Vector) and [Matrix](/Matrix). This page contains general information about the OOP functions and provides useful links.
15+
16+
### Turning it on
17+
18+
By default, OOP is disabled (however, vectors and matrices are always available) - this is mainly because the vast majority of servers will prefer to stick to what they know - procedural programming. In fact, functions are still available even when OOP is enabled. Enabling OOP is as simple as adding the following line to the resource meta file:
19+
20+
```xml
21+
<oop>true</oop>
22+
```
23+
24+
### Introduction
25+
26+
This is a scripting tutorial explaining to you what **object orientated programming** is and teaching you how to use the **OOP features of MTA**. This was originally created by qaisjp on June 8, 2014 ([Forum post](https://forum.multitheftauto.com/topic/65029-wikitut-oop-introduction/)).
1627

1728
OOP stands for *object orientated programming*. Three simple words, and you'll probably understand the last word the most. OOP is where all functions relating to a single instance are called on that instance, an instance being a creation of a class — an element class, a database class, a player, a vehicle.
1829

@@ -103,6 +114,11 @@ Timer(incrementDimension, 60*1000, 10) -- set a timer for sixty thousand millise
103114

104115
This code would take a random player and move them to the next dimension every minute for the next ten minutes.
105116

117+
118+
### Vectors and Matrices
119+
120+
[Vectors](/Vector) and [Matrices](/Matrix) make it easier to drop the complex maths and go straight ahead with fun part of maths. As mentioned above, OOP does not have to be enabled in the server config for this to be enabled.
121+
106122
### Vectors
107123

108124
`player.position` works too! But how do you change three arguments... using one variable? **Vectors.**
@@ -154,8 +170,62 @@ The documentation for the OOP syntax intends to be very simplistic and is suppor
154170
* Methods can either start like `player:` or `Player.` — the former is only for a function on an instance (like `setPlayerHealth`) and the latter is a static method (like `getRandomPlayer`).
155171
* The counterpart section allows you to see at a glance how the variable can be used. In most cases, this can be inferred from the function page.
156172

173+
174+
### OOP Metatable Structure (Advanced)
175+
176+
You will understand this if you're proficient with Lua and have a decent understanding of metatables. Understanding this section is not necessary to use OOP.
177+
178+
```lua
179+
-- Exposed to global environment
180+
Element = {
181+
Element = createElement,
182+
setPosition = setElementPosition,
183+
...
184+
}
185+
186+
Vehicle = {
187+
Vehicle = createVehicle,
188+
setColor = setVehicleColor,
189+
...
190+
}
191+
192+
-- Hidden in lua registry, applied to userdata
193+
ElementMT = {
194+
__index = CLuaClassDefs::Index,
195+
__newindex = CLuaClassDefs::NewIndex,
196+
__class = Element,
197+
__call = __class.create,
198+
__set = {
199+
type = CLuaClassDefs::ReadOnly,
200+
health = setElementHealth,
201+
...
202+
},
203+
__get = {
204+
type = getElementType,
205+
health = getElementHealth,
206+
...
207+
},
208+
}
209+
210+
VehicleMT = {
211+
__index = CLuaClassDefs::Index,
212+
__newindex = CLuaClassDefs::NewIndex,
213+
__class = Vehicle,
214+
__parent = ElementMT,
215+
__call = __class.create,
216+
__set = {
217+
damageProof = setVehicleDamageProof
218+
...
219+
},
220+
__get = {
221+
damageProof = isVehicleDamageProof
222+
...
223+
},
224+
}
225+
```
226+
157227
<SeeAlsoSection seeAlsoLinks={getSeeAlsoLinksFromList([
158-
'article:OOP',
228+
'article:OOP_Classes',
159229
'classes:any:Vector',
160230
'classes:any:Matrix'
161231
])} currentId='' />

0 commit comments

Comments
 (0)