Red-Pitbull Posted February 18, 2014 Share Posted February 18, 2014 hi all i am new in lua i make a custom sound vehicle i dont khow this script is right but it not working please tell where is problem --client-- speedx, speedy, speedz = getElementVelocity ( getVehicleID, 411() ) function onResourceStart() local sound = playSound3D("sounds/song.mp3", true) setSoundProperties(sound, 48000.0, 128.00, 440.0, false) setSoundSpeed ( sound, 1.2 ) end addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), onResourceStart) Link to comment
Castillo Posted February 18, 2014 Share Posted February 18, 2014 That doesn't make any sense. Link to comment
Red-Pitbull Posted February 18, 2014 Author Share Posted February 18, 2014 yes i khow i learn lua but i dont not where i put functions and element and which functions and element i use.hope you help thanks Link to comment
Moderators Citizen Posted February 18, 2014 Moderators Share Posted February 18, 2014 Ok so, first I assume that the sound you are using is a sample that loop nicely. Let's then wait and think before going in the code. The sound must start when the engine is on and only with vehicles with the modelid 411(infernus). Then we want to play the sound in 3D and playing it for all Infernus on the server. We need to update the speed of the sound according the speed of the vehicle. We also need to update the position of the 3D sound to follow the vehicle. If the engine is off, then we have to pause the sound and resume it if the engine is on again. Here is a code I just made. With a lot of comments to let you understand what is going on (it's a client script): function updateEngineSound() -- We get a table of all vehicles on the server: local allVehicles = getElementsByType("vehicle") -- we then do a loop that will iterate over that table: for index, veh in ipairs (allVehicles) do --It's like saying: "For each vehicle, do this" -- we get the model of the vehicle local model = getElementModel(veh) -- if the model is equal to 411 then do this if model == 411 then -- if the engine is on if getVehicleEngineState(veh) then -- we get the position of that vehicle local x, y, z = getElementPosition(veh) -- we then try to get the sound we maybe created and stored as an element data -- for that vehicle. If we didn't yet, then it will return false. local sound = getElementData(veh, "engineSound") -- if the inverse of sound is true (same as: if sound is equal to false) -- Note that "not false" will give true if not sound then -- we create the sound at the veh position and loop it sound = playSound3D("sounds/song.mp3", x, y, z, true) -- we store that sound in an element data I decided to call "engineSound" setElementData(veh, "engineSound", sound) end -- if the sound is paused (because the engine was off) then if isSoundPaused(veh) then -- we "unpause" the sound setSoundPaused(sound, false) end -- we then need to get the speed local velocityX, velocityY, velocityZ = getElementVelocity(veh) -- calculate the global speed in mph from the 3 velocities: -- use pythagorean theorem to get actual velocity -- raising something to the exponent of 0.5 is the same thing as taking a square root. local actualspeed = (velocityX^2 + velocityY^2 + velocityZ^2)^(0.5) -- multiply by 50 to obtain the speed in meters per second and -- then by 111.847 to obtain the speed in meters per hour local mph = actualspeed * 50 * 111.847 -- we try to calculate the sound speed according to the mph -- I guess 80mph is the speed that the original sound sounds like and -- I also guess 0.25 is the min speed that the sound can be at to still sounds like an engine. -- I added 80/minSoundSpeed to 80 to compensate the minSoundSpeed we added local minSoundSpeed = 0.25 local soundSpeed = mph/(80+80/minSoundSpeed) + minSoundSpeed -- and then we apply it to the sound setSoundSpeed (sound, soundSpeed) -- and finally update the sound position to follow the vehicle setElementPosition(sound, x, y, z) else -- otherwise (so if the engine is off) -- we pause the sound setSoundPaused(sound, true) end -- close the "if engine was on" end -- close the "if model was 411" end -- end of the loop end setTimer(updateEngineSound, 50, 0) --repeat that function every 50 ms Hope it helps. Regards, Citizen Link to comment
Sergo86 Posted February 19, 2014 Share Posted February 19, 2014 works, but swears debug, that's what gives: 29 lines bad 'sound/player' pointer @ 'isSoundPaused'(1) Link to comment
Anubhav Posted February 19, 2014 Share Posted February 19, 2014 function updateEngineSound() -- We get a table of all vehicles on the server: local allVehicles = getElementsByType("vehicle") -- we then do a loop that will iterate over that table: for index, veh in ipairs (allVehicles) do --It's like saying: "For each vehicle, do this" -- we get the model of the vehicle local model = getElementModel(veh) -- if the model is equal to 411 then do this if model == 411 then -- if the engine is on if getVehicleEngineState(veh) then -- we get the position of that vehicle local x, y, z = getElementPosition(veh) -- we then try to get the sound we maybe created and stored as an element data -- for that vehicle. If we didn't yet, then it will return false. local sound = getElementData(veh, "engineSound") -- if the inverse of sound is true (same as: if sound is equal to false) -- Note that "not false" will give true if not sound then -- we create the sound at the veh position and loop it sound = playSound3D("sounds/song.mp3", x, y, z, true) -- we store that sound in an element data I decided to call "engineSound" setElementData(veh, "engineSound", sound) end -- if the sound is paused (because the engine was off) then if isSoundPaused(sound) then -- we "unpause" the sound setSoundPaused(sound, false) end -- we then need to get the speed local velocityX, velocityY, velocityZ = getElementVelocity(veh) -- calculate the global speed in mph from the 3 velocities: -- use pythagorean theorem to get actual velocity -- raising something to the exponent of 0.5 is the same thing as taking a square root. local actualspeed = (velocityX^2 + velocityY^2 + velocityZ^2)^(0.5) -- multiply by 50 to obtain the speed in meters per second and -- then by 111.847 to obtain the speed in meters per hour local mph = actualspeed * 50 * 111.847 -- we try to calculate the sound speed according to the mph -- I guess 80mph is the speed that the original sound sounds like and -- I also guess 0.25 is the min speed that the sound can be at to still sounds like an engine. -- I added 80/minSoundSpeed to 80 to compensate the minSoundSpeed we added local minSoundSpeed = 0.25 local soundSpeed = mph/(80+80/minSoundSpeed) + minSoundSpeed -- and then we apply it to the sound setSoundSpeed (sound, soundSpeed) -- and finally update the sound position to follow the vehicle setElementPosition(sound, x, y, z) else -- otherwise (so if the engine is off) -- we pause the sound setSoundPaused(sound, true) end -- close the "if engine was on" end -- close the "if model was 411" end -- end of the loop end setTimer(updateEngineSound, 50, 0) --repeat that function every 50 ms 1 Link to comment
Moderators Citizen Posted February 19, 2014 Moderators Share Posted February 19, 2014 Wups, my bad yeah, Anubhav fixed it. I wrote: if isSoundPaused(veh) then instead of if isSoundPaused(sound) then Link to comment
Sergo86 Posted February 20, 2014 Share Posted February 20, 2014 again error, 58 lines bad 'sound/player' pointer @ 'SetSoundPaused'(1) Link to comment
Moderators Citizen Posted February 20, 2014 Moderators Share Posted February 20, 2014 again error, 58 lines bad 'sound/player' pointer @ 'SetSoundPaused'(1) Please post again the full code now you modified it and quote the line 58 if you are removing useless code from the file when copying-pasting here. Also, be sure you had restarted the resource before testing the code. Since it's a new day, I guess you restarted it. Link to comment
Red-Pitbull Posted February 20, 2014 Author Share Posted February 20, 2014 not working notting in debugscript 3 Link to comment
Moderators Citizen Posted February 20, 2014 Moderators Share Posted February 20, 2014 not working notting in debugscript 3 Read my post above. @SERGEY86RUS: Who dafuq are you ?! Didn't even noticed you weren't the guy I was helping since the begining nor the author of this thread. Link to comment
Sergo86 Posted February 21, 2014 Share Posted February 21, 2014 not working notting in debugscript 3 Read my post above. @SERGEY86RUS: Who dafuq are you ?! Didn't even noticed you weren't the guy I was helping since the begining nor the author of this thread. what it means dafug ???? i don't understand)) Link to comment
Red-Pitbull Posted February 21, 2014 Author Share Posted February 21, 2014 maybe problem is song file BASS ERROR 2 in LoadMedia path:C:\Program Files\MTA San Andreas 1.3\mods\deathmatch\resources\music_lisening_script\sounds\song.mp3 3d:1 loop:1 Link to comment
Sergo86 Posted February 21, 2014 Share Posted February 21, 2014 maybe problem is song file BASS ERROR 2 in LoadMedia path:C:\Program Files\MTA San Andreas 1.3\mods\deathmatch\resources\music_lisening_script\sounds\song.mp3 3d:1 loop:1 I have everything working, but debug swears Here is the complete script with sound --> http://rghost.ru/52566320 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now