Jump to content

Load and Play a sound by CODE (without noise.lua and without using any Pivots)


Russell
 Share

Recommended Posts

Hi there team!!

I have another programing/sintax question to answer...

I want load and play a sound file by LUA pressing 3 keys on my keyboard (without using any PIVOT. Without using NOISE.LUA script. And without using the Flowgraph).

Only executed from code of my MAIN.LUA on my project...

I have write this, but i don't know if i need anything more, or the sintax is bad writen. The code doesn't give me any errors, but i can't ear anything:

if window:KeyDown(Key.J) and window:KeyDown(Key.F) and window:KeyDown(Key.V) then
	local SonidoJFV = Sound:Load("Sound/MisSonidos/ValidarTICKET.wav")
	SonidoJFV.source=Source:Create()
	SonidoJFV.source:SetSound(sound)
	SonidoJFV.source:SetVolume(100)
	SonidoJFV.source:SetPitch(1)
	SonidoJFV.source:SetLoopMode(false)
	SonidoJFV.source:SetRange(900000)
  	SonidoJFV.source:SetPosition(Vec3(0,0,0))
	SonidoJFV.source:Play()
	--changemapname="1-Tutorial"
end

Any tip or answer that could help me?

Thanks a lot!!

Link to comment
Share on other sites

12 minutes ago, Lethal Raptor Games said:

For a source to be heard you will need a Listener.  I can't be sure, but I think you parent it to the camera.  You're probably better off loading your sound and creating the source at the game startup, and just play it on keypress.

Thanks for your answer.

Im new in LUA. Where in MAIN.LUA and how can i make that??

(Anyway, as i said i don't wanna create or use any entities. Only Play a sound by code from MAIN.LUA when i press J, F and V.)

No Pivot. No Listener. No noise.lua. No use the Flowgraph...

Only code. Is it possible?? ?

Link to comment
Share on other sites

According to the docs, the example code looks like this.

sound = Sound:Load("Sound/Music/menutheme.wav")
sound:Play()

This should just play a straightforward sound.  No effects possible.  Don't think you can even set the volume but it sounds like you wanted to start with something simpler.

I don't use Lua so I can't tell you where the code goes.

  • Thanks 1
Link to comment
Share on other sites

7 hours ago, Lethal Raptor Games said:

I think you can play a sound without creating a source.  But if you want source you need a Listener, if you dont have a camera just set the position of the listener.

 

3 hours ago, gamecreator said:

According to the docs, the example code looks like this.


sound = Sound:Load("Sound/Music/menutheme.wav")
sound:Play()

This should just play a straightforward sound.  No effects possible.  Don't think you can even set the volume but it sounds like you wanted to start with something simpler.

I don't use Lua so I can't tell you where the code goes.

OOOHHH YEAH!!!

It works without "a source"!! As Lethal Raptor said, my problem was that i was creating a source in the sintax. I write the example code (I had not seen it on the Learn page) and it works for what I need to do.

Restart all the game and change map in the game when the user press 3 keys in a row:

if window:KeyDown(Key.J) and window:KeyDown(Key.F) and window:KeyDown(Key.V) then
	sound = Sound:Load("Sound/MisSonidos/ValidarTICKET.wav")
	sound:Play()
	changemapname="1-Tutorial"
end

Thank you very much to you both!! ?

  • Like 2
Link to comment
Share on other sites

Hi

You can use source without problems, just create it in start() function like this for example: (out of the car script - available in workshop)

Quote

       self.sound={}
        self.sound.engineloop=Sound:Load("Addons/Vehicles/run.wav")
        self.sound.enginestart=Sound:Load("Addons/Vehicles/carstart.wav")
        self.sound.brake=Sound:Load("Addons/Vehicles/brake.wav")
        self.sound.CarLights=Sound:Load("Addons/Vehicles/CarLights.wav")
        
        Portiere=Sound:Load("Addons/Vehicles/portiere.wav")
        self.MetalHit=Sound:Load("Sound/Impact/impact_metal12.wav")
     
        self.source={}
    
        if self.source.engineloop==nil then
                    if self.sound.engineloop~=nil then             
                                        self.source.engineloop=Source:Create()
                                        self.source.engineloop:SetSound(self.sound.engineloop)
                                        self.source.engineloop:SetLoopMode(true)                                        
                    end
        end    

--------

After this you can use the source with more options available in UpdateWorld()

Quote

        if window:KeyHit(Key.NumPad1) then
            if self.MotorAN==0 then                --Start the engine                
        
                self.MotorAN=0.5
                self.sound.enginestart:Play()
                self.source.engineloop:SetPitch(0.3)                
                self.source.engineloop:Play()
                self.StartTime=Time:GetCurrent()
                self.Benzin=self.Benzin-0.5
                self.Fumee:Show()    -- smoke
    
            else
                self.MotorAN=0        
                self.source.engineloop:Stop()    
                self.Fumee:Hide()        
            end
            
        end

this should run ok and let you adjust volume, distance, pitch....

 

 

Link to comment
Share on other sites

36 minutes ago, Marcousik said:

Hi

You can use source without problems, just create it in start() function like this for example: (out of the car script - available in workshop)

--------

After this you can use the source with more options available in UpdateWorld()

this should run ok and let you adjust volume, distance, pitch....

Thank you very much, but I needed the code without any Source or any configuration like volume, distance or pitch. For this reason the code:

if window:KeyDown(Key.J) and window:KeyDown(Key.F) and window:KeyDown(Key.V) then
	sound = Sound:Load("Sound/MisSonidos/ValidarTICKET.wav")
	sound:Play()
	changemapname="1-Tutorial"
end

It's finally what I needed for the Main Menu of my game. If we press 3 specific keys, the sound is eared (without any element or entity) and the Map i need is loaded.

However, I have saved your code as an example in case I need it in another project.

Thanks!! ?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...