SetTrainPosition: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (fix example mistake)
m (comment code, fix example bug)
Line 19: Line 19:
This example adds a command called "/settrainpos", allowing you to set the position of the train.
This example adds a command called "/settrainpos", allowing you to set the position of the train.
<syntaxhighlight lang="lua">local function setMyPosition(player, _, position)
<syntaxhighlight lang="lua">local function setMyPosition(player, _, position)
-- grab the vehicle, if it exists
local veh = player.vehicle
local veh = player.vehicle
-- make sure we're actually in a vehicle
if not veh then
if not veh then
player:outputChat("You are not in a vehicle!")
player:outputChat("You are not in a vehicle!")
Line 25: Line 28:
end
end


-- make sure we're in a train
if veh.vehicleType ~= "Train" then
if veh.vehicleType ~= "Train" then
player:outputChat("You are not in a train!")
player:outputChat("You are not in a train!")
Line 40: Line 44:
end
end


-- check if the position argument is between 0 and 1
if (position < 0) or (position > 1) then
if (position < 0) or (position > 1) then
player:outputChat("position must be between 0 and 1!")
player:outputChat("position must be between 0 and 1!")
Line 45: Line 50:
end
end


veh.position = position
-- finally set the train position
veh.trainPosition = position
end
end
addCommandHandler("settrainpos", setMyPosition)
addCommandHandler("settrainpos", setMyPosition)</syntaxhighlight></section>
</syntaxhighlight></section>


==See Also==
==See Also==
{{Vehicle functions}}
{{Vehicle functions}}

Revision as of 16:10, 2 January 2016

Sets the position the train is currently on the track

Syntax

bool setTrainPosition ( vehicle train, float position )

OOP Syntax Help! I don't understand this!

Method: vehicle:setTrainPosition(...)
Variable: .trainPosition
Counterpart: getTrainPosition


Required Arguments

  • train: the train of which to set the track
  • position: the position along the track

Returns

Returns true if the train position was set, false otherwise.

Example

Click to collapse [-]
OOP Example 1

This example adds a command called "/settrainpos", allowing you to set the position of the train.

local function setMyPosition(player, _, position)
	-- grab the vehicle, if it exists
	local veh = player.vehicle

	-- make sure we're actually in a vehicle
	if not veh then
		player:outputChat("You are not in a vehicle!")
		return
	end

	-- make sure we're in a train
	if veh.vehicleType ~= "Train" then
		player:outputChat("You are not in a train!")
		return
	end

	-- convert position to a number
	position = tonumber(position)

	-- if position cannot be converted to a number
	if not position then
		player:outputChat("Syntax: /settrainpos <position>")
		player:outputChat("position: number between 0 and 1")
		return
	end

	-- check if the position argument is between 0 and 1
	if (position < 0) or (position > 1) then
		player:outputChat("position must be between 0 and 1!")
		return
	end

	-- finally set the train position
	veh.trainPosition = position
end
addCommandHandler("settrainpos", setMyPosition)

See Also