Skip to content

Commit

Permalink
feat: add example.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Yehonal committed Oct 9, 2022
1 parent cb02ef9 commit 06c1706
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
!/scripts/*/
/scripts/*/*
!/scripts/*/.gitkeep
!/scripts/typescript/examples
!/scripts/typescript/tsconfig.json
!/scripts/typescript/package.json
/var/*
Expand Down
65 changes: 65 additions & 0 deletions scripts/typescript/examples/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Example with arrow function
*
* This is just an Hello World
*/
export const OnPlayerLogin: player_event_on_login = (event, player) => {
player.SendChatMessageToPlayer(
ChatMsg.CHAT_MSG_WHISPER,
Language.LANG_UNIVERSAL,
"Hello World",
player
);
};

RegisterPlayerEvent(PlayerEvents.PLAYER_EVENT_ON_LOGIN, (...args) =>
OnPlayerLogin(...args)
);

/**
*
* This is an example of how to use TypeScript classes
* and implement hook methods inside it.
*
* A class can be useful to implement more complex
* scripts that can be instantiated or extended
*
*/
export class PlayerChat {
previousMessage: string;

/**
*
* @param firstMessage The first message to display
*/
constructor(firstMessage: string) {
this.previousMessage = firstMessage;
}

OnPlayerChat: player_event_on_chat = (
event: number,
player: Player,
msg: string
): string | boolean => {
const sanitized = msg;

msg = sanitized != msg ? `${sanitized} (Sanitized)` : msg;

player.SendChatMessageToPlayer(
ChatMsg.CHAT_MSG_WHISPER,
Language.LANG_UNIVERSAL,
`Previous message: ${this.previousMessage}, current message: ${msg}`,
player
);

this.previousMessage = msg;

return true;
};
}

const playerChat = new PlayerChat("First Message");

RegisterPlayerEvent(PlayerEvents.PLAYER_EVENT_ON_CHAT, (...args) =>
playerChat.OnPlayerChat(...args)
);

0 comments on commit 06c1706

Please sign in to comment.