Jump to content

Auto Positioning


Ashford

Recommended Posts

Hello guys, I just have a little question, I don't know if the title fits the subject, but I'm going to state my problem.

I'm planning to make an in game house maker ( Not markers and stuff, a real house building ) and I'm just asking about.. positioning..

How to get the coordinates automatically, the house will surely be a square, and just wondering how to build the objects there automatically..

What I exactly want is, how to make the script get the exact coordinates that the player wants ( he'll decided it with  his cursor "a house shape will be visible in the screen" ) and build it.

Thanks in advance.

Link to comment

The functions really speak for themselves.

If you're planning to make an "in-game house maker" as you said, it's going to be a lot trickier in terms of position, rotation, placement and all that stuff.

I can only help you a little bit with that but not that much, but I thought you just wanted to know how to get the coordinates?

Link to comment
Just now, Tails said:

The functions really speak for themselves.

If you're planning to make an "in-game house maker" as you said, it's going to be a lot trickier in terms of position, rotation, placement and all that stuff.

I can only help you a little bit with that but not that much, but I thought you just wanted to know how to get the coordinates?

Well I'd like to get your help, I've already answered your question below.

2 minutes ago, KariiiM said:

If I am not wrong. you want to get the player's  current position coordinations, right?

No, that's easy lol.

I'm not a bad scripter, I'm actually an in training developer, I just need to know more about this.

For example, there will be a GUI where a player can chose to build a house, he'll click on a button, which will attach a house to his Cursor ( with low alpha ) and he'll chose where to place it, more like Map Editor, but during the game.

Link to comment

You can create an object with CreateObject, then set its alpha with SetElementAlpha. Do that client-side (on player input). Tie it to the client's cursor using OnClientRenderand getCursorPostion and SetElementPosition.

After that use onClientClick to trigger a server event to create an object at the same position as the low alpha object using GetElementPosition and TriggerServerEvent.

Now remove the client object with DestroyElement.

 

I've actually done something similar to this before. I can provide you some examples later if you want when I get back.

Hope this helps.

Edited by Tails
Link to comment

Just quickly wrote this with the help of some old code of mine. It's nothing fancy. Just trying to give you an idea of how you could do it.

Hold ctrl to position and left click to place.

This is client side:

local objectID = 3877
local objectHeightOffset = 1.6

local objectInHand = createObject(objectID,0,0,0)
setElementAlpha(objectInHand,170)

function holdObject(objectID)
	if getKeyState("lctrl") then
		local cx,cy,cz = getCameraMatrix()
		local _,_,wx,wy,wz = getCursorPosition()
        local _,x,y,z = processLineOfSight(cx,cy,cz,wx,wy,wz,true,true,false,true,true,false,false,false,objectInHand)
		
		if x and y and z then
			setElementPosition(objectInHand,x,y,z+objectHeightOffset)
		end
	end
end
addEventHandler("onClientRender",root,holdObject)

function toggleCursor(key,pressed)
	if key == "lctrl" then
		if pressed then
			showCursor(pressed)
		else
			showCursor(pressed)
		end
	end
end
addEventHandler("onClientKey",root,toggleCursor)

function placeObject(button,state)
	if button == "left" and state == "up" and getKeyState("lctrl") then
		local x,y,z = getElementPosition(objectInHand)
		local rx,ry,rz = getElementRotation(objectInHand)
		
		triggerServerEvent("onClientPlaceObject",localPlayer,objectID,x,y,z,rx,ry,rz)
		--destroyElement(objectInHand)
	end
end
addEventHandler("onClientClick",root,placeObject)

And this server side:

function placeObject(id,x,y,z,rx,ry,rz)
	createObject(id,x,y,z,rx,ry,rz)
	outputChatBox(getPlayerName(source).. " has placed an object at "..x..", "..y..", "..z.."!")
end
addEvent("onClientPlaceObject",true)
addEventHandler("onClientPlaceObject",root,placeObject)

The rest you can probably figure out on your own. If not feel free to ask more questions, I'll do my best to answer them. :-)

Edited by Tails
Link to comment

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...