CreateObject: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}
{{Server client function}}
Creates an object in the GTA world.
{{Note|Dynamic objects do not automatically have physics applied to them. Use setElementVelocity(object, 0, 0, 0) to fix this.}}
{{Note|Dynamic objects do not automatically have physics applied to them. Use setElementVelocity(object, 0, 0, 0) to fix this.}}
Creates an object in the GTA world.


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


===Required Arguments===
===Required Arguments===
Line 30: Line 30:
This example creates an object when the resource starts:
This example creates an object when the resource starts:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function mapLoad ( name )
function mapLoad(name)
   -- create an object at a specified position with a specified rotation
   -- create an object at a specified position with a specified rotation
   createObject ( 1337, 5540.6654, 1020.55122, 1240.545, 90, 0, 0 )
   createObject(1337, 5540.6654, 1020.55122, 1240.545, 90, 0, 0)
end
end
addEventHandler ( "onResourceStart", resourceRoot, mapLoad )
addEventHandler("onResourceStart", resourceRoot, mapLoad)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>


<section name="Server-only Example 2" class="server" show="true">
<section name="Server-only Example 2" class="server" show="true">
This example creates an object near player who write /createObject in the chat:
This example creates an object near the player who write /createObject in the chat:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- this function is called whenever someone types 'createObject' in the console:
-- this function is called whenever someone types 'createObject' in the console:
function consoleCreateObject ( thePlayer, commandName )
function createObjectCommand( thePlayer, commandName )
   if ( thePlayer ) then
   if ( thePlayer ) then
       local x, y, z = getElementPosition ( thePlayer ) -- get the player's position
       local x, y, z = getElementPosition ( thePlayer ) -- get the player's position
Line 48: Line 48:
       local theObject = createObject ( 980, x + 2, y + 2, z, 0, 0, 0 )
       local theObject = createObject ( 980, x + 2, y + 2, z, 0, 0, 0 )
       if ( theObject ) then -- check if the object was created successfully
       if ( theObject ) then -- check if the object was created successfully
         outputConsole ( "Object created successfully", thePlayer )
         outputConsole ( "Object created successfully.", thePlayer )
       else
       else
         outputConsole ( "Failed to create Object", thePlayer )
         outputConsole ( "Failed to create the object.", thePlayer )
       end
       end
   end
   end
end
end
addCommandHandler ( "createObject", consoleCreateObject )
addCommandHandler ( "createObject", createObjectCommand )
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 13:46, 2 February 2021

Creates an object in the GTA world.

[[{{{image}}}|link=|]] Note: Dynamic objects do not automatically have physics applied to them. Use setElementVelocity(object, 0, 0, 0) to fix this.

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 mapLoad(name)
   -- 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, mapLoad)
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 ) -- get the player's position
      -- 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
         outputConsole ( "Object created successfully.", thePlayer )
      else
         outputConsole ( "Failed to create the object.", thePlayer )
      end
   end
end
addCommandHandler ( "createObject", createObjectCommand )

See Also