CreateObject: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
(33 intermediate revisions by 20 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
Creates any GTASA world object at a specified location using x,y,z. The object rotation can also be set with the optional arguments rx, ry, rz.
{{Server client function}}
Creates an [[object]] [[element]] at a given position and rotation.
{{Note|Dynamic objects do not automatically have physics applied to them. Use [[setElementVelocity]](object, 0, 0, 0) to fix this.}}
{{Note|Creating objects server-side can help for syncing the objects with all clients (for example, if you create a glass object client-side, it will break only for the client that broke the glass. Server-side, it will broke for all clients.}}


==Syntax==  
==Syntax==
<syntaxhighlight lang="lua">createObject ( id, x, y, z, [rx, ry, rz] )</syntaxhighlight>  
<syntaxhighlight lang="lua">object createObject ( int modelId, float x, float y, float z, [ float rx, float ry, float rz, bool isLowLOD = false ] )</syntaxhighlight>  
{{OOP||[[Object]]||}}


===Required Arguments===  
===Required Arguments===
*'''ID:''' A Whole integer specifying the GTASA object model ID
*'''modelId:''' A whole integer specifying the GTA:SA object model ID.
*'''X:''' A Float value that specifies the X coordinate where the object is spawned at in the GTASA world
*'''x:''' A floating point number representing the X coordinate on the map.
*'''Y:''' A Float value that specifies the Y coordinate where the object is spawned at in the GTASA world
*'''y:''' A floating point number representing the Y coordinate on the map.
*'''Z:''' A Float value that specifies the Z coordinate where the object is spawned at in the GTASA world
*'''z:''' A floating point number representing the Z coordinate on the map.


===Optional Arguments===  
===Optional Arguments===
{{OptionalArg}}  
{{OptionalArg}}  
*'''rx:''' A Float value that specifies the created objects X rotation
*'''rx:''' A floating point number representing the rotation about the X axis in degrees.
*'''ry:''' A Float value that specifies the created objects Y rotation
*'''ry:''' A floating point number representing the rotation about the Y axis in degrees.
*'''rz:''' A Float value that specifies the created objects Z rotation
*'''rz:''' A floating point number representing the rotation about the Z axis in degrees.
{{New items|3.0120|1.2|
*'''isLowLOD:''' A bool value specifying if the object will be low LOD. A low LOD object has no collision and a longer draw distance.
}}


==Example==  
===Returns===
Explain what the example does here
* Returns the [[object]] element if the creation was successful, ''false'' otherwise.
<syntaxhighlight lang="lua">root = getRootElement ()
 
addEventHandler ( "onMapLoad", root, "onMapLoad" )
==Example==
function onMapLoad ( name, root )
<section name="Server-only Example 1" class="server" show="true">
createObject ( 5540.6654, 1020.55122, 1240.545, 15, 10, 4.4, 6.2] )
This example creates an object when the resource starts:
end</syntaxhighlight>
<syntaxhighlight lang="lua">
function loadMapObjects()
  -- create an object at a specified position with a specified rotation
  createObject(1337, 5540.6654, 1020.55122, 1240.545, 90, 0, 0)
end
addEventHandler("onResourceStart", resourceRoot, loadMapObjects)
</syntaxhighlight>
</section>
 
<section name="Server-only Example 2" class="server" show="true">
This example creates an object near the player who write /createObject in the chat:
<syntaxhighlight lang="lua">
-- this function is called whenever someone types 'createObject' in the console:
function createObjectCommand(thePlayer, commandName)
  if (thePlayer) then
      local x, y, z = getElementPosition(thePlayer)
      -- create a object next to the player:
      local theObject = createObject(980, x + 2, y + 2, z, 0, 0, 0)
      if (theObject) then -- check if the object was created successfully
        outputChatBox("Object created successfully.", thePlayer)
      else
        outputChatBox("Failed to create the object.", thePlayer)
      end
  end
end
addCommandHandler("createobject", createObjectCommand)
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Object}}
{{Object functions}}

Revision as of 14:11, 2 February 2021

Creates an object element at a given position and rotation.

[[{{{image}}}|link=|]] Note: Dynamic objects do not automatically have physics applied to them. Use setElementVelocity(object, 0, 0, 0) to fix this.
[[{{{image}}}|link=|]] Note: Creating objects server-side can help for syncing the objects with all clients (for example, if you create a glass object client-side, it will break only for the client that broke the glass. Server-side, it will broke for all clients.

Syntax

object createObject ( int modelId, float x, float y, float z, [ float rx, float ry, float rz, bool isLowLOD = false ] )

OOP Syntax Help! I don't understand this!

Method: Object(...)


Required Arguments

  • modelId: A whole integer specifying the GTA:SA object model ID.
  • x: A floating point number representing the X coordinate on the map.
  • y: A floating point number representing the Y coordinate on the map.
  • z: A floating point number representing the Z coordinate on the map.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • rx: A floating point number representing the rotation about the X axis in degrees.
  • ry: A floating point number representing the rotation about the Y axis in degrees.
  • rz: A floating point number representing the rotation about the Z axis in degrees.
  • isLowLOD: A bool value specifying if the object will be low LOD. A low LOD object has no collision and a longer draw distance.

Returns

  • Returns the object element if the creation was successful, false otherwise.

Example

Click to collapse [-]
Server-only Example 1

This example creates an object when the resource starts:

function loadMapObjects()
   -- create an object at a specified position with a specified rotation
   createObject(1337, 5540.6654, 1020.55122, 1240.545, 90, 0, 0)
end
addEventHandler("onResourceStart", resourceRoot, loadMapObjects)
Click to collapse [-]
Server-only Example 2

This example creates an object near the player who write /createObject in the chat:

-- this function is called whenever someone types 'createObject' in the console:
function createObjectCommand(thePlayer, commandName)
   if (thePlayer) then
      local x, y, z = getElementPosition(thePlayer)
      -- create a object next to the player:
      local theObject = createObject(980, x + 2, y + 2, z, 0, 0, 0)
      if (theObject) then -- check if the object was created successfully
         outputChatBox("Object created successfully.", thePlayer)
      else
         outputChatBox("Failed to create the object.", thePlayer)
      end
   end
end
addCommandHandler("createobject", createObjectCommand)

See Also