DgsSetParent: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "{{Server client function}} __NOTOC__ This function is used to determine the parent of an ''element''. ==Syntax== <syntaxhighlight lang="lua"> element getElementParent ( elem...")
 
No edit summary
Line 1: Line 1:
{{Server client function}}
{{Server client function}}
__NOTOC__
__NOTOC__  
This function is used to determine the parent of an ''element''.
This function is used for setting an element as the parent of another element.
 
{{Important Note|The client-side version of this function can only be used on client-created elements. It cannot be used to modify the parent of server side elements.}}
{{Note|This function does not change when an element will be destroyed - Elements are always destroyed when the resource that created them is stopped.}}
{{Note|When an element is destroyed, its parent becomes the new parent of its children.}}
{{Note|setElementParent only works if new parent is the root element, map root, or ancestor of map root}}
{{Tip|This function does not affect the child elements position. To attach elements use the function [[attachElements]].}}
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
element getElementParent ( element theElement )   
bool dgsSetParent( element theElement, element parent )   
</syntaxhighlight>  
</syntaxhighlight>  
{{OOP||[[element]]:getParent|parent|setElementParent}}
{{OOP||[[element]]:setParent|parent|getElementParent}}


===Required Arguments===  
===Required Arguments===  
*'''theElement:''' The child of the parent element you want returned.
*'''theElement:''' The element that you wish to set the parent of.
*'''parent:''' The element you wish to be the parent of ''theElement''.


===Returns===
===Returns===
This returns the parent as an ''element''. It returns ''false'' if ''theElement'' is invalid, or is the root node.
Returns ''true'' if both [[element]]s are valid, ''false'' otherwise.


==Example==
==Example==  
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
Consider the following map file:
This example sets the parent of each spawnpoint to a dummy element:
<syntaxhighlight lang="lua">
dummyElem = createElement ( "spawngroup", "Group of spawn points" ) -- create a dummy element
local spawnpoints = getElementsByType ( "spawnpoint" ) -- get a table of spawn point elements
for k,v in ipairs (spawnpoints) do -- loop through the table of spawn points
  setElementParent ( v, dummyElem ) -- set the dummy element as the parent of the spawn point
end
-- all of the spawn points are now children of 'dummyElem'
</syntaxhighlight>
 
This is the equivalent of:
<syntaxhighlight lang="xml">
<syntaxhighlight lang="xml">
<spawngroup id="red">
<spawngroup id="Group of spawn points">
   <spawnpoint id="spawnpoint_0" posX="2507.8715820313" posY="2772.6071777344" posZ="10.8203125" rot="270" skin="285"/>
   <spawnpoint id="spawnpoint_0" posX="2507.8715820313" posY="2772.6071777344" posZ="10.8203125" rot="270" skin="285"/>
   <spawnpoint id="spawnpoint_1" posX="2508.060546875" posY="2780.3647460938" posZ="10.8203125" rot="270" skin="285"/>
   <spawnpoint id="spawnpoint_1" posX="2508.060546875" posY="2780.3647460938" posZ="10.8203125" rot="270" skin="285"/>
</spawngroup>
   <spawnpoint id="spawnpoint_2" posX="2508.0053710938" posY="2776.2897949219" posZ="10.8203125" rot="270" skin="285"/>
<spawngroup id="blue">
   <spawnpoint id="spawnpoint_3" posX="2510.6899414063" posY="2778.3745117188" posZ="10.8203125" rot="270" skin="285"/>
   <spawnpoint id="spawnpoint_5" posX="2733.4184570313" posY="2753.1276855469" posZ="10.8203125" rot="90" skin="124"/>
</spawngroup></syntaxhighlight>
   <spawnpoint id="spawnpoint_6" posX="2733.5258789063" posY="2748.1110839844" posZ="10.8203125" rot="90" skin="125"/>
</spawngroup>
</syntaxhighlight>
 
This function determines a spawnpoint's parent element, and announces its ID:
<syntaxhighlight lang="lua">
function spawnpointUse ( thePlayer )            -- this function gets called whenever a spawnpoint is used
  theSpawnGroup = getElementParent ( source )  -- get the spawnpoint's parent element
  -- announce the parent's ID and the player who spawned there:
  outputChatBox ( getPlayerName ( thePlayer ) .. " spawned at team " .. getElementID ( theSpawnGroup ) .. "'s spawnpoint." )
  -- Example output: "Joe spawned at team blue's spawnpoint."
end
addEventHandler ( "onSpawnpointUse", root, spawnpointUse )
</syntaxhighlight>
</section>
</section>


==See Also==
==See Also==
{{element_functions}}
{{element functions}}

Revision as of 09:13, 27 June 2017

This function is used for setting an element as the parent of another element.

[[{{{image}}}|link=|]] Important Note: The client-side version of this function can only be used on client-created elements. It cannot be used to modify the parent of server side elements.
[[{{{image}}}|link=|]] Note: This function does not change when an element will be destroyed - Elements are always destroyed when the resource that created them is stopped.
[[{{{image}}}|link=|]] Note: When an element is destroyed, its parent becomes the new parent of its children.
[[{{{image}}}|link=|]] Note: setElementParent only works if new parent is the root element, map root, or ancestor of map root
[[{{{image}}}|link=|]] Tip: This function does not affect the child elements position. To attach elements use the function attachElements.

Syntax

bool dgsSetParent( element theElement, element parent )  

OOP Syntax Help! I don't understand this!

Method: element:setParent(...)
Variable: .parent
Counterpart: getElementParent


Required Arguments

  • theElement: The element that you wish to set the parent of.
  • parent: The element you wish to be the parent of theElement.

Returns

Returns true if both elements are valid, false otherwise.

Example

Click to collapse [-]
Server

This example sets the parent of each spawnpoint to a dummy element:

dummyElem = createElement ( "spawngroup", "Group of spawn points" ) -- create a dummy element
local spawnpoints = getElementsByType ( "spawnpoint" ) -- get a table of spawn point elements
for k,v in ipairs (spawnpoints) do -- loop through the table of spawn points
   setElementParent ( v, dummyElem ) -- set the dummy element as the parent of the spawn point
end
-- all of the spawn points are now children of 'dummyElem'

This is the equivalent of:

<spawngroup id="Group of spawn points">
   <spawnpoint id="spawnpoint_0" posX="2507.8715820313" posY="2772.6071777344" posZ="10.8203125" rot="270" skin="285"/>
   <spawnpoint id="spawnpoint_1" posX="2508.060546875" posY="2780.3647460938" posZ="10.8203125" rot="270" skin="285"/>
   <spawnpoint id="spawnpoint_2" posX="2508.0053710938" posY="2776.2897949219" posZ="10.8203125" rot="270" skin="285"/>
   <spawnpoint id="spawnpoint_3" posX="2510.6899414063" posY="2778.3745117188" posZ="10.8203125" rot="270" skin="285"/>
</spawngroup>

See Also