Code Snippets
From SA-MP
[edit]
A Simple join message
public OnPlayerConnect(playerid) { new string[MAX_PLAYER_NAME + 23]; GetPlayerName(playerid, string, MAX_PLAYER_NAME); format(string, sizeof (string), "Welcome %s to my server!", string); SendClientMessage(playerid, 0xFF9900AA, str); return 1; }
The length of the string without the name (including spaces and the NULL character to end the string) is 23, added to MAX_PLAYER_NAME gives the total required length for the string, longer than needed strings are just a waste of time (for the processor to blank them and space (in memory). Only one string is required here as you never need them to both exist at the same time.
If a player with the nick "Player" joined the server it would show "Welcome Player to my server!" to them.
[edit]
A Simple join/leave message (icon)
public OnPlayerConnect(playerid) { SendDeathMessage(INVALID_PLAYER_ID,playerid,200); return 1; } public OnPlayerDisconnect(playerid, reason) { SendDeathMessage(INVALID_PLAYER_ID,playerid,201); return 1; }
This will send a join/leave message in the form of a icon in the killlist - See also SendDeathMessage
[edit]
Simple car lock and unlock system
for(new i = 0; i < MAX_PLAYERS; i++) { SetVehicleParamsForPlayer(ID_OF_YOUR_CAR, i, 0, 1); // This is to lock a car for all players }
for(new i = 0; i < MAX_PLAYERS; i++) { SetVehicleParamsForPlayer(ID_OF_YOUR_CAR, i, 0, 0); // This is to unlock a car for all players }
See also GetPlayerVehicleID and SetVehicleParamsForPlayer
- Control_Structures: Basic program flow structures.
