[TUTORIAL] Obscure features you might not be aware.
[Snippet] Vietnam slide »

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,804
[TUTORIAL] Obscure features you might not be aware.
« on August 6th, 2017, 11:12 PM »
This is a topic where I'll be listing various features you might not be aware and which are likely to help your code be even more efficient.
[TUTORIAL] Re: Obscure features you might not be aware.
« Reply #1, on August 6th, 2017, 11:49 PM »Last edited on August 7th, 2017, 12:05 AM
Quick Entity Properties

Often you might want to retrieve a player's position color etc. or a vehicle/object/pickup's position, color, speed, rotation etc. Knowing you come from the official plugin you might be tempted to do the following:

Code: [Select]
print(player.Pos.x);

Unbeknownst to you is the fact that this approach is very inefficient. In therms of performance. Why? Because it has to create a Vector3 instance, put it in a table, then has to look up the .x property mutators in order to get your value. And finally, since this vector instance was temporary, it'll have to remove it from the tracking table it was added when it was created. And trust me, this is very inefficient.

Entities offer some shortcuts for these frequent types of requests. Which come with a similar name and sometimes shorter if no other name collisions may occur. The example above can be translated to:

Code: [Select]
print(player.PosX);

This time, you get the actual floating point value and no temporary Vector3 instance has to be created. They can both read and write in case you were curious.

There are many of these short aliases for frequent operations where the possibility of higher efficiency is likely to occur if using them.

Here is a list of some of them:

Blips
Code: [Select]
blip.PosX; // Equivalent of: blip.Pos.x
blip.PosY; // Equivalent of: blip.Pos.y
blip.PosZ; // Equivalent of: blip.Pos.z
blip.Red; // Equivalent of: blip.Color.r
blip.Green; // Equivalent of: blip.Color.g
blip.Blue; // Equivalent of: blip.Color.b
blip.Alpha; // Equivalent of: blip.Color.a

Checkpoints
Code: [Select]
checkpoint.PosX; // Equivalent of: checkpoint.Pos.x
checkpoint.PosY; // Equivalent of: checkpoint.Pos.y
checkpoint.PosZ; // Equivalent of: checkpoint.Pos.z
checkpoint.Red; // Equivalent of: checkpoint.Color.r
checkpoint.Green; // Equivalent of: checkpoint.Color.g
checkpoint.Blue; // Equivalent of: checkpoint.Color.b
checkpoint.Alpha; // Equivalent of: checkpoint.Color.a

Objects:
Code: [Select]
object.PosX; // Equivalent of: object.Pos.x
object.PosY; // Equivalent of: object.Pos.y
object.PosZ; // Equivalent of: object.Pos.z
object.RotX; // Equivalent of: object.Rot.x
object.RotY; // Equivalent of: object.Rot.y
object.RotZ; // Equivalent of: object.Rot.z
object.RotW; // Equivalent of: object.Rot.w
object.EulerRotX; // Equivalent of: object.EulerRot.x
object.EulerRotY; // Equivalent of: object.EulerRot.y
object.EulerRotZ; // Equivalent of: object.EulerRot.z
object.MoveToX; // Equivalent of: local pos = object.Pos; pos.x = x; object.MoveTo(pos, object.MoveToDuration)
object.MoveToY; // Equivalent of: local pos = object.Pos; pos.y = y; object.MoveTo(pos, object.MoveToDuration)
object.MoveToZ; // Equivalent of: local pos = object.Pos; pos.z = z; object.MoveTo(pos, object.MoveToDuration)
object.MoveByX; // Equivalent of: local pos = object.Pos; pos.x = x; object.MoveBy(pos, object.MoveByDuration)
object.MoveByY; // Equivalent of: local pos = object.Pos; pos.y = y; object.MoveBy(pos, object.MoveByDuration)
object.MoveByZ; // Equivalent of: local pos = object.Pos; pos.z = z; object.MoveBy(pos, object.MoveByDuration)
object.RotateToX; // Equivalent of: local rot = object.Rot; rot.x = x; object.RotateTo(rot, object.RotateToDuration)
object.RotateToY; // Equivalent of: local rot = object.Rot; rot.y = y; object.RotateTo(rot, object.RotateToDuration)
object.RotateToZ; // Equivalent of: local rot = object.Rot; rot.z = z; object.RotateTo(rot, object.RotateToDuration)
object.RotateToW; // Equivalent of: local rot = object.Rot; rot.w = w; object.RotateTo(rot, object.RotateToDuration)
object.RotateByX; // Equivalent of: local rot = object.Rot; rot.x = x; object.RotateBy(rot, object.RotateByDuration)
object.RotateByY; // Equivalent of: local rot = object.Rot; rot.y = y; object.RotateBy(rot, object.RotateByDuration)
object.RotateByZ; // Equivalent of: local rot = object.Rot; rot.z = z; object.RotateBy(rot, object.RotateByDuration)
object.RotateByW; // Equivalent of: local rot = object.Rot; rot.w = w; object.RotateBy(rot, object.RotateByDuration)
object.RotateToEulerX; // Equivalent of: local rot = object.EulerRot; rot.x = x; object.RotateToEuler(rot, object.RotateToEulerDuration)
object.RotateToEulerY; // Equivalent of: local rot = object.EulerRot; rot.y = y; object.RotateToEuler(rot, object.RotateToEulerDuration)
object.RotateToEulerZ; // Equivalent of: local rot = object.EulerRot; rot.z = z; object.RotateToEuler(rot, object.RotateToEulerDuration)
object.RotateByEulerX; // Equivalent of: local rot = object.EulerRot; rot.x = x; object.RotateByEuler(rot, object.RotateByEulerDuration)
object.RotateByEulerY; // Equivalent of: local rot = object.EulerRot; rot.y = y; object.RotateByEuler(rot, object.RotateByEulerDuration)
object.RotateByEulerZ; // Equivalent of: local rot = object.EulerRot; rot.z = z; object.RotateByEuler(rot, object.RotateByEulerDuration)

Pickups
Code: [Select]
pickup.PosX; // Equivalent of: pickup.Pos.x
pickup.PosY; // Equivalent of: pickup.Pos.y
pickup.PosZ; // Equivalent of: pickup.Pos.z

Players
Code: [Select]
player.PosX; // Equivalent of: player.Pos.x
player.PosY; // Equivalent of: player.Pos.y
player.PosZ; // Equivalent of: player.Pos.z
player.Red; // Equivalent of: player.Color.r
player.Green; // Equivalent of: player.Color.g
player.Blue; // Equivalent of: player.Color.b

Vehicles
Code: [Select]
vehicle.PosX; // Equivalent of: vehicle.Pos.x;
vehicle.PosY; // Equivalent of: vehicle.Pos.y;
vehicle.PosZ; // Equivalent of: vehicle.Pos.z;
vehicle.RotX; // Equivalent of: vehicle.Rot.x;
vehicle.RotY; // Equivalent of: vehicle.Rot.y;
vehicle.RotZ; // Equivalent of: vehicle.Rot.z;
vehicle.RotW; // Equivalent of: vehicle.Rot.w;
vehicle.EulerX; // Equivalent of: vehicle.EulerRot.x;
vehicle.EulerY; // Equivalent of: vehicle.EulerRot.y;
vehicle.EulerZ; // Equivalent of: vehicle.EulerRot.z;
vehicle.SpeedX; // Equivalent of: vehicle.Speed.x;
vehicle.SpeedY; // Equivalent of: vehicle.Speed.y;
vehicle.SpeedZ; // Equivalent of: vehicle.Speed.z;
vehicle.RelSpeedX; // Equivalent of: vehicle.RelSpeed.x;
vehicle.RelSpeedY; // Equivalent of: vehicle.RelSpeed.y;
vehicle.RelSpeedZ; // Equivalent of: vehicle.RelSpeed.z;
vehicle.TurnSpeedX; // Equivalent of: vehicle.TurnSpeed.x;
vehicle.TurnSpeedY; // Equivalent of: vehicle.TurnSpeed.y;
vehicle.TurnSpeedZ; // Equivalent of: vehicle.TurnSpeed.z;
vehicle.RelTurnSpeedX; // Equivalent of: vehicle.RelTurnSpeed.x;
vehicle.RelTurnSpeedY; // Equivalent of: vehicle.RelTurnSpeed.y;
vehicle.RelTurnSpeedZ; // Equivalent of: vehicle.RelTurnSpeed.z;

Like I said. There may be other various shortcuts as long as performance is likely to be gained if available.

Please note that when you try to set these values, if more than one value is necessary when setting them. Like a vector where you have x,y,z components or color where you have r,g,b,a components. The plugin will first retrieve the current values of the missing components and then apply the changes you requested.

For example, if you were to try and set a value with player.PosX. Then the plugin would first have to retrieve the current Y and Z values in order to form the new vector value. And then apply the resulted position using your supplied value. Example:

Code: [Select]
// Take the following example
player.PosX = 10;

// Which can be thought of as the following code

// Grab the remaining Y and Z components in order to form a vector
local y = player.PosY, z player.PosZ;
// Now form a vector and apply it
player.Pos = Vector3(10, y, z);
.

[Snippet] Vietnam slide »