Jump to content

Moving a gate


jkub

Recommended Posts

Hy again... I have been heavily using the moveobject function on my server in a script. But I only know how to script where it moves everyobject in the game that I placed... I want to make it to where it only moves one object like a gate. Look down below here is what i did...

allObjects = getElementsByType ( "object" ) 
  
for key,object in pairs(allObjects) do 
        local origX,origY,origZ = getElementPosition ( object )  
        local newY = origY + 10000 
        moveObject ( object, 6000000, origX, newY, origZ ) 
end 

I got that as an example from the wiki and altered it a bit.. .I use it to move a boat north that i created.

But the problem is as i said before it moves every object I have placed with a map editor. I want it to move only one object such as a gate.

Like if I made an island And put a gate on the entrance. I want it to be able to open and close without having to use my map editor but instead use a moveobject script so it will be easier please help me :(

Link to comment

Well you need to specify which objects are gates in some way. You might be best putting you own element type into a MAP file and creating your gates on loading it - then you could just do allObjects = getElementsByType ( "gate" ). If not, you could check for a certain model maybe and only move objects with the correct one - see getObjectModel().

Link to comment

You can specify them in map file, eg:

<gates id="1" posX="1" posY="1" posZ="1" /> 

Server doesn't know such element typ, but you can make your server know it. Using getElementsByType( "gates" ) would return all the gates element you specified in map file. You can then use getElementData( gateelement, "posX" ) to get it's X coord, or getElementData( gateelement, "id" ) to get its ID. So in attach a function to onResourceStart event, loop through all the gates and create object using these x,y,z (if you need you can add more attributes for rotation).

Useful functions:

- getElementData

- getElementsByType

Link to comment

The element system in MTA is confusing new comers. "gates" would be a dummy element type and therefore the game doesn't recognize it as an object element.

Make a table where object elements (the gates) will be stored and then you can move specific gate using that table.

eg:

local gatesTable = { } -- it defines a table 
addEventHandler( "onResourceStart", getResourceRootElement( getThisResource( ) ), 
    function( ) 
        local gates = getElementsByType( "gates" ) 
        for k in pairs( gates ) do 
            local x, y, z = getElementData( v, "posX" ), getElementData( v, "posY" ), getElementData( v, "posZ" ) 
            local gateObject = createObject( GATEID, x, y, z ) 
            table.insert( gatesTable, gateObject ) 
        end 
    end 
) 
  
-- now gatesTable will contain all object elements (returned from createObject) and now you can use this table to move a gate 
moveObject( gatesTable[ 1 ], 5000, 5, 0, 0 ) -- this will move the 1st gate created 
  

Link to comment

I put all that in a lua

then ran it i got serveral bad argument lines in the server. But I went into my server anyway I turned it on The gate wasent even created.

Im sorry for the trouble but I have spent hours studying the wiki And it is just so confusing I cant get much out of it.

I feel kinda stupid since you pretty much did the whole thing yourself lol. For some reason i though i might be able to fix the lines myself so I altered the script a little and now i get these errors

3 bad argument notes for 'getelementdata' line 6

and a bad argument for moveobject'. As i said i tryed it ripped strait from here. But it didnt seem towork... :(

I just edited one line and that was the createobject line I specified what id i would use and the location. here it is now still saying getelement data is messed up and moveobject too

local gatesTable = { } -- it defines a table 
addEventHandler( "onResourceStart", getResourceRootElement( getThisResource( ) ), 
    function( ) 
        local gates = getElementsByType( "gates" ) 
        for k in pairs( gates ) do 
            local x, y, z = getElementData( v, "posX" ), getElementData( v, "posY" ),  
  
getElementData( v, "posZ" ) 
            local gateObject = createObject( 975, 0, 0, 0 ) 
            table.insert( gatesTable, gateObject ) 
        end 
    end 
) 
  
-- now gatesTable will contain all object elements (returned from createObject) and now you can use  
  
this table to move a gate 
moveObject( gatesTable[ 1 ], 5000, 5, 0, 0 ) -- this will move the 1st gate created 
  

Edited by Guest
Link to comment

Have you created a map file with the tags (Nodes) as in my post a few post above?

<map> 
    <gates id="1" posX="0" posY="0" pozZ="0" /> 
</map> 

This file must be added to meta.xml too, like so:

<map src="mapfile.map" /> 

Link to comment

I did that as soon as i read the post and it still gives errors... anyway here it is now

local gatesTable = { } -- it defines a table 
addEventHandler( "onResourceStart", getResourceRootElement( getThisResource( ) ), 
    function( ) 
        local gates = getElementsByType( "gates" ) 
        for k in pairs( gates ) do 
            local x, y, z = getElementData( v, "0" ), getElementData( v, "0" ), getElementData( v, "5" ) 
            local gateObject = createObject( 975, 0, 0, 5 ) 
            table.insert( gatesTable, gateObject ) 
        end 
    end 
) 
  
-- now gatesTable will contain all object elements (returned from createObject) and now you can use this table to move a gate 
moveObject( gatesTable [ 1 ], 5000, 5, 0, 0 ) -- this will move the 1st gate created 
  
  

can you tell me what is wrong with this and what i shouldent have messed with

Link to comment

I don't know what else is right/wrong but you've also getting the x,y,z values incorrectly and not actually using them. What you want is something like:

  
. 
            local x, y, z = getElementData( v, "posX" ), getElementData( v, "posY" ), getElementData( v, "posZ" ) 
            local gateObject = createObject( 975, x,y,z) 
. 
  

Link to comment

Like Mr.Hankey said the code as multiple syntax errors. You have v defined in the for loop as value but it never has a value assigned to it.

for k in 

Should be:

for k, v in 

Secondly getElementsByType returns a indexed table and not a paired table. Therefore:

for k, v in pairs 

Should be:

for k, v in ipairs 

Codelisting (how it should work).

server.lua

  
local gatesTable = { } -- it defines a table 
addEventHandler( "onResourceStart", getResourceRootElement( getThisResource( ) ), 
    function( ) 
        local gates = getElementsByType( "gate" ) 
        for _, gate in ipairs( gates ) do 
            local x, y, z = tonumber((getElementData( gate, "posX" ) or 0)), tonumber((getElementData( gate, "posY" ) or 0)), tonumber((getElementData( gate, "posZ" ) or 0)) 
            table.insert( gatesTable, createObject( 975, x, y, z ) ) 
        end 
    end 
) 
  
-- now gatesTable will contain all object elements (returned from createObject) and now you can use this table to move a gate 
if (gatesTable [ 1 ]) then 
moveObject( gatesTable [ 1 ], 5000, 5, 0, 0 ) -- this will move the 1st gate created 
end 
  

meta.xml

  

  "" type="gamemode" author="" name="gates" version="" /> 
  

mapfile.map

  

  "" type="map" gamemodes="gates" author=""  name="" version="" /> 
  "1" posX="0" posY="0" posZ="0" /> 

  

Link to comment

There are no commands to do. I just start the script. But I should of asked for assistance on creating commands to open and close it when i started this thread. And btw when I did start the script nothing came up.

Link to comment
  • 2 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...