Jump to content

playSound3D attached to vehicle?


madis

Recommended Posts

Is it possible to attach playSound3D to vehicle? Idea would be that people would hear whatever player is listening in a vehicle (probably some song from server side selection of music).

I have an idea to make a resource similar to Battlefield Vietnam (yeah, there is battlefield69 or something but it doesn't work with latest MTA and I actually want to do it myself - which probably means I'll never get it done).

Link to comment

You can't stream music from other vehicles' radio channel to other players... playSound3D plays sound file not radio.

If you want to attach some sound (sound file you added to your resource) then use these:

- onClientRender or onClientPreRender - to get vehicle's position and set the sound position to vehicle's position every frame

- getElementPosition - to get vehicle's position

- setElementPosition - to set sound position to where the vehicle is (coordinates returned from getElementPosition)

Link to comment

Another question, maybe a bit off topic, but needed for the same reason...

What is the best method to get a race spawnpoint element via lua script (by what I actually mean the moving vehicle) which is defined in .map?

Edit:

Oh I guess I found it

https://wiki.multitheftauto.com/wiki/Writing_Gamemodes

-- retrieve a table with all flag elements

local flagElements = getElementsByType ( "flag" )

-- loop through them

for key, value in pairs(flagElements) do

-- get our info

local posX = getElementData ( value, "posX" )

local posY = getElementData ( value, "posY" )

local posZ = getElementData ( value, "posZ" )

local team = getElementData ( value, "team" )

-- create an object according to the flag position

createObject ( 1337, posX, posY, posZ )

-- output the team that we created a base for

outputChatBox ( "Base for team " .. team .. " created" )

end

EDIT2: Mhm, yeah, of course, the spawnpoint itself isn't updated, so I need to get the vehicle or ped (player) position instead.

Any help on that :) ?

Link to comment

getElementsByType("vehicle") will give you all the vehicles (even ones without players in). getElementsByType("player") will give you all the players, from them you can get which vehicle they are in using getPedOccupiedVehicle.

Link to comment

Yes, with highering SetSoundMaxDistance and SetSoundMinDistance values, the audio can be heared to greater distance.

I usually don't like to ask too much... but still, I just haven't figured out following.

I've implemented a song selector for players when they are in vehicles.

Now, there are two possibilities, as eAi pointed out, I could check vehicles or players. I would actually prefer vehicles - which means:

Every player can set a musicfile playing in his/her vehicle and optionally leave it playing when leaving vehicle (by default this would be off, though) - which means that every vehicle needs to be checked, instead of players and their vehicles.

So, I have this:

1) On every frame, play3DSound element needs to be repositioned, so it follows a vehicle. This is done by clientside script:

function reposition3DSound(soundSource, x, y, z)
setElementPosition(soundSource, x, y, z)
end
 
function vehiclesWithDriver()
 
-- getElementsWithinColShape might be better in future
 
local vehicles = getElementsByType("vehicle")
 
for theKey, aVehicle in ipairs(vehicles) do
	vehicleOccupant = getVehicleOccupant(aVehicle, 0)
-- if someone is driving the vehicle
if vehicleOccupant then
local songId = getElementData(playerVehicle, "song_id")
local songPosition = getElementData(playerVehicle, "song_pos")
 
if songId then
local x, y, z = getElementPosition(aVehicle)
 
 				reposition3DSound(musicSound, x, y, z)
end
end
end
end
 
addEventHandler("onClientRender", root,
function()
	vehiclesWithDriver()
end
)

It's a bit messy code...

When a client is in vehicle and starts a song, it will set element data for that vehicle

setElementData(playerVehicle, "song_id", "a")
setElementData(playerVehicle, "song_pos", false)

where, song_id is the song I'm playing (in this example "a" and song_pos is the seeking position of song (so that it would be synced)

Okay, I can't understand my post either... well my problem is:

How should/could I sync the playing sound, so that when someone starts a song for a vehicle, others would hear it, included players who join the server when the song is already playing. One of the problems is that in one case I need to initialize the sound and later on update the position.

EDIT: nvm, I guess I've figured it out... I'll get back when it's done.

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