-
Notifications
You must be signed in to change notification settings - Fork 2
Context API Services
dev2alert edited this page Feb 4, 2022
·
1 revision
Home ▸ Context API ▸ Services
Service is a class that contains logic for the context.
The service is similar to contexts, except that it does not have events running and it cannot be frozen.
Make sure that the compilation flag "emitDecoratorMetadata": true is enabled, without this flag you will have to pass the class argument to Import and ImportService!
Example:
// mode.gsv.ts
import {GameMode} from "@sa-mp/core";
import {Service} from "@sa-mp/decorators";
@Service()
export class ModeService extends GameMode.Service {
public divide(a: number, b: number): number {
return a / b;
}
}Example:
// mode.gctx.ts
import {GameMode} from "@sa-mp/core";
import {Context, Import, ImportService} from "@sa-mp/decorators";
import {A} from "./a.gtx";
import {B} from "./b.gtx";
import {ModeService} from "./mode.gsv";
@Context({
children: [A, B],
services: [ModeService]
})
export class Mode extends GameMode.Context {
@Import()
public readonly a: A;
@ImportService()
public readonly modeService: ModeService;
public onInit(): void {
console.log("[Mode] Init!");
console.log(`[Mode] 2 + 3 = ${this.a.plus(2, 3)};`);
console.log(`[Mode] 12 / 4 = ${this.modeService.divide(12, 4)};`);
}
public multi(a: number, b: number): number {
return a * b;
}
}Example:
// mode.gctx.ts
import {GameMode} from "@sa-mp/core";
import {Context, ImportService} from "@sa-mp/decorators";
import {MinusService} from "./minus.gsv";
import {PlusService} from "./plus.gsv";
import {SumService} from "./sum.gsv";
@Context({
services: [PlusService, MinusService, SumService]
})
export class Mode extends GameMode.Context {
@ImportService()
public readonly sum: SumService;
@ImportService()
public readonly minusService: MinusService;
public onInit(): void {
console.log("[Mode] Init!");
console.log(`[Mode] 2 + 3 = ${this.sum.sum(2, 3)};`);
console.log(`[Mode] 10 - 3 = ${this.minusService.minus(10, 3)};`);
}
public minus(a: number, b: number): number {
return a - b;
}
}
// plus.gsv.ts
import {GameMode} from "@sa-mp/core";
import {Service} from "@sa-mp/decorators";
@Service()
export class PlusService extends GameMode.Service {
public plus(a: number, b: number): number {
return a + b;
}
}
// minus.gsv.ts
import {GameMode} from "@sa-mp/core";
import {Import, Service} from "@sa-mp/decorators";
import {Mode} from "./mode.gctx";
@Service()
export class MinusService extends GameMode.Service {
@Import(() => Mode)
public readonly mode: Mode;
public minus(a: number, b: number): number {
return this.mode.minus(a, b);
}
}
// sum.gsv.ts
import {GameMode} from "@sa-mp/core";
import {ImportService, Service} from "@sa-mp/decorators";
import {PlusService} from "./plus.gsv";
@Service()
export class SumService extends GameMode.Service {
@ImportService()
public readonly plus: PlusService;
public sum(a: number, b: number): number {
return this.plus.plus(a, b);
}
}- Getting started [RU]
- Configuration [RU]
- Command line interface (CLI) [RU]
- Examples [RU]
- Player commands [RU]
- Keyboard [RU]
- Dialogs [RU]
- Groups [RU]
- Context API [RU]
- AMX API [RU]