LuaEntity::play_sound()

Things that we aren't going to implement
User avatar
kumpu
Fast Inserter
Fast Inserter
Posts: 111
Joined: Wed Jun 07, 2017 10:10 am
Contact:

LuaEntity::play_sound()

Post by kumpu »

Yes you can play sounds at a position, but the sound's position can't be locked to vehicles, like e.g. engine noise. And rather than having to do a crazy entity hack it would be great if you implement it :)

Additionally, if it's not too inconvenient to implement, return a handle to the new sound instance with which you can stop and destroy it. I want full control over it.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14266
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: LuaEntity::play_sound()

Post by Rseding91 »

Unfortunately due to how sound work in the game this isn't possible. Sounds associated with an entity only work if the entity itself asks the game to play it and keeps asking it to play it each frame.

Additionally they aren't saved/loaded so they aren't deterministic meaning Lua can never know if a sound is playing or not meaning there will be no handle to stop/destroy it.
If you want to get ahold of me I'm almost always on Discord.
User avatar
kumpu
Fast Inserter
Fast Inserter
Posts: 111
Joined: Wed Jun 07, 2017 10:10 am
Contact:

Re: LuaEntity::play_sound()

Post by kumpu »

Can you just make it so that I can trigger custom sounds at will for an entity, it's clearly possible from C++ to play car open, close and no fuel sounds. Maybe you'd define some custom sounds in the prototype, like you can with the afore mentioned. I really don't want to resort to entity hacks again... It's a mess to deal with. :(
Bilka
Factorio Staff
Factorio Staff
Posts: 3309
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: LuaEntity::play_sound()

Post by Bilka »

https://lua-api.factorio.com/latest/Con ... #SoundPath already supports build, close and various other sounds.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
User avatar
kumpu
Fast Inserter
Fast Inserter
Posts: 111
Joined: Wed Jun 07, 2017 10:10 am
Contact:

Re: LuaEntity::play_sound()

Post by kumpu »

Bilka wrote: ↑Mon Jan 07, 2019 9:36 pm https://lua-api.factorio.com/latest/Con ... #SoundPath already supports build, close and various other sounds.
Yes but the sound needs to follow the car.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14266
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: LuaEntity::play_sound()

Post by Rseding91 »

I'm open to talking about this more if someone wants to write up a more formal API and how they'd expect these things to work. I would do it myself but I never needed to play sounds in any of the mods I made so I'm not sure what all people would want out of such a system.

A few things to mention up front:

Anything implemented will be "best guess" by the game. It will never actually know how far along a given audio clip is or if it lagged behind due to UPS problems because there's no way to make that deterministic.

I'm not guaranteeing anything but it does seem like there's a valid use-case for this.
If you want to get ahold of me I'm almost always on Discord.
User avatar
kumpu
Fast Inserter
Fast Inserter
Posts: 111
Joined: Wed Jun 07, 2017 10:10 am
Contact:

Re: LuaEntity::play_sound()

Post by kumpu »

So to be clear are we talking about stopping sounds too or just starting custom ones? You said you can't return a handle because sounds aren't saved. But I think that would be fine, if you're able to check if they're valid during load.
Stopping would be cool but I can live without it. I would mainly use it for transitions. Actually on second thought, being able to change volume dynamically would be sufficient and even better for that.
User avatar
kumpu
Fast Inserter
Fast Inserter
Posts: 111
Joined: Wed Jun 07, 2017 10:10 am
Contact:

Re: LuaEntity::play_sound()

Post by kumpu »

Bump.
User avatar
kumpu
Fast Inserter
Fast Inserter
Posts: 111
Joined: Wed Jun 07, 2017 10:10 am
Contact:

Re: LuaEntity::play_sound()

Post by kumpu »

Uhm hello? Was my question stupid or anything? You said you're open to talking.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14266
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: LuaEntity::play_sound()

Post by Rseding91 »

I've been busy with other stuff.
If you want to get ahold of me I'm almost always on Discord.
Anyone
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Mon Apr 25, 2016 11:02 am
Contact:

Re: LuaEntity::play_sound()

Post by Anyone »

Heya,

I would like to propose some ideas for sounds. Calling play_sound({...}) would return a new object: LuaSound. This object would have several functions: play(), pause(), stop(), destroy(), and like every other LuaObject, has a valid check and it can be serialized.

This would allow us to control sounds for things like cutscenes. One example would be going back and forward or stopping cutcenes. Additionally it would be really nice to be able to tell a sound to dim or stop the ambient music playing while a sound is being played. This will let us play certain sounds without the ambient messing up.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: LuaEntity::play_sound()

Post by eradicator »

The thing i've most often wanted is being able to change the volume later. Though as my main usecase for that is emulating buildings/things being hearable from very far away i'd already be happy if the engine supported changing the default sound cutoff of 16 tiles to something higher for some entities.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: ζ—₯本θͺž, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
User avatar
slikts
Burner Inserter
Burner Inserter
Posts: 13
Joined: Mon Apr 11, 2016 2:13 pm
Contact:

Re: LuaEntity::play_sound()

Post by slikts »

An example use case for adding a sound to entity would be this mod.

The LuaSound API idea sounds good. The current sound APIs are very limiting, so it'd be nice to have this issue revisited.
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5267
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: LuaEntity::play_sound()

Post by Klonan »

slikts wrote: ↑Sun May 10, 2020 9:55 am An example use case for adding a sound to entity would be this mod.

The LuaSound API idea sounds good. The current sound APIs are very limiting, so it'd be nice to have this issue revisited.
You can add sounds to entities no problem, how do you think the base entities have sounds?

This API request it just about playing sounds during runtime with control scripting
User avatar
slikts
Burner Inserter
Burner Inserter
Posts: 13
Joined: Mon Apr 11, 2016 2:13 pm
Contact:

Re: LuaEntity::play_sound()

Post by slikts »

The base entities have sounds for specific events like being opened, mined, dying, working, etc. If a use case doesn't fit those events (like for the mod I linked to), then I don't see how it could be done, at least in a simple way without creating a dummy entity and moving it with the character. The surface sound API also doesn't allow stopping sounds, so it's overall very limiting.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14266
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: LuaEntity::play_sound()

Post by Rseding91 »

slikts wrote: ↑Sun May 10, 2020 11:30 am The base entities have sounds for specific events like being opened, mined, dying, working, etc. If a use case doesn't fit those events (like for the mod I linked to), then I don't see how it could be done, at least in a simple way without creating a dummy entity and moving it with the character. The surface sound API also doesn't allow stopping sounds, so it's overall very limiting.
The way virtually all of those one-shot sounds work is the game just does "play sound" and then doesn't care about ever stopping it. It plays until the sound ends and then it's done playing.

Things like entity working sounds have a lot more logic (that barely works as is) - none of which is part of the game state so mods can't read any of it.
If you want to get ahold of me I'm almost always on Discord.
credomane
Filter Inserter
Filter Inserter
Posts: 307
Joined: Tue Apr 12, 2016 6:21 pm
Contact:

Re: LuaEntity::play_sound()

Post by credomane »

I think a LuaSound class similar to LuaRendering would be awesome. Since the LuaRendering stuff isn't deterministic and/or saved with the game I suggest using that as a template for how LuaSound should and/or could work.

Here is my idea for the LuaSound API


sound :: LuaSound: Allows playing of sounds in the game world. Is a global class just like game, script, remote and the rest


SOUND_TABLE is a table with the following fields:

sound :: SoundPath (NOTE: Would be nice to have SoundPath be able to use "file" type with the same limitation/rules as SpritePath)
volume :: float (optional): The volume of the sound. Default is 1 (100%). Follow same rules as the speaker entity?
global_playback :: Should the sound playback at a consistent volume for the entire surface? Default is false. Follow same rules as the speaker entity?
pitch :: double Changes pitch of the sound. Default is nil (no adustment). Follow same rules as the speaker entity? Don't allow this at all?
target :: Position or LuaEntity: Where the sound should play from.
target_offset :: Vector (optional): Only used if target is a LuaEntity.
surface :: SurfaceSpecification
forces :: array of ForceSpecification (optional): The forces that this sound is played to.
players :: array of PlayerSpecification (optional): The players that this sound is played to.
autoplay :: boolean (optional): If this sound should play immediately. Defaults to true.
loop :: boolean (optional): Should the sound loop endlessly? Default is false.


FUNCTIONS of LuaSound:

load(SOUND_TABLE); -- Returns uint64 that is unique for the entire lifetime of the save. Designed this way to emulate how the LuaRendering.draw_* functions work. So that play and stop can attempt to act like sound equivalent of set_visiblity() in LuaRendering.
play(id) β†’ Plays the sound attached to this id from the beginning, sets paused=false.
stop(id) β†’ Stops the sound attached to this id.
destroy(id) Destroy the sound with the given id.
is_valid(id) β†’ boolean Does a valid sound with this id exist?
is_playing(id) β†’ boolean Is the sound with this id actively playing?
get_all_ids(mod_name) β†’ array of uint64 Gets an array of all valid sound ids.
clear(mod_name) Destroys all render objects.
get_autoplay(id) β†’ boolean Get whether sound with this id plays immediately after loading.
get_surface(id) β†’ LuaSurface The surface the sound with this id is on.
get_type(id) β†’ string Gets the type of the given sound. Returns the "type" from SoundPath (file, ambient, utility, etc)
get_loop(id) β†’ boolean Gets if the sound with this id loops endlessly or not.
set_loop(id, looping) Sets if sound loops endlessly or not.
get_paused(id) β†’ boolean Gets if the sound with this id is paused or not.
set_paused(id, paused) Sets if sound is paused or not.
get_pitch(id) β†’ double or nil Gets the pitch value of the sound.
set_pitch(id, pitch) Sets the pitch value of the sound. nil removes all pitch adjustments.
get_forces(id) β†’ array of LuaForce Get the forces that the sound with this id is played to or nil if visible to all forces.
set_forces(id, forces) Set the forces that the sound with this id is played to.
get_players(id) β†’ array of LuaPlayer Get the players that the sound with this id is played to or nil if visible to all players.
set_players(id, players) Set the players that the sound with this id is played to.
get_target(id) β†’ ScriptRenderTarget Get where the sound with this id is played.
set_target(id, target, target_offset) Set where the sound with this id is played.
object_name :: string [R] This objects name.
Last edited by Bilka on Thu Aug 06, 2020 8:52 am, edited 1 time in total.
Reason: Split off unrelated suggestion into https://forums.factorio.com/87608
Rseding91
Factorio Staff
Factorio Staff
Posts: 14266
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: LuaEntity::play_sound()

Post by Rseding91 »

Uhhh.... LuaRendering *is* saved/loaded and is deterministic.
If you want to get ahold of me I'm almost always on Discord.
Bilka
Factorio Staff
Factorio Staff
Posts: 3309
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: LuaEntity::play_sound()

Post by Bilka »

Credomane, I split off your LuaRendering suggestion to a different post since it is a good point/idea, but completely unrelated to sounds: 87608

Other than that, I'd like to put emphasis on the fact that LuaRendering is save/loaded and deterministic, as Rseding says. I hope no mod is attempting to recreate render objects on every tick or on load or something because they assumed the objects are not save/loaded.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
credomane
Filter Inserter
Filter Inserter
Posts: 307
Joined: Tue Apr 12, 2016 6:21 pm
Contact:

Re: LuaEntity::play_sound()

Post by credomane »

Rseding91 wrote: ↑Thu Aug 06, 2020 5:00 am Uhhh.... LuaRendering *is* saved/loaded and is deterministic.
Saying it wasn't deterministic was completely incorrect on my part no idea what I was thinking as it has to be to work in multiplayer. As for the saved/loaded with the game. I swear I read somewhere that LuaRendering wasn't saved with the game (probably imagined it in my sleepiness last night or dreamt it after going to bed) while trying to figure out why my sprite would disappear after every server restart. Testing in singleplayer yield the same result.

Was trying to draw a sprite of a friend's server logo at spawn (400x400 pixel png) which worked but like I said after a server restart it is gone. The id that was returned by draw_sprite still exists in the lua variable I stored it in and it matches the one I printed to console when drawing the sprite. but the sprite isn't rendered anymore and rendering.is_valid(myspriteid) says it isn't valid anymore.
Bilka wrote: ↑Thu Aug 06, 2020 8:51 am Credomane, I split off your LuaRendering suggestion to a different post since it is a good point/idea, but completely unrelated to sounds: 87608

Other than that, I'd like to put emphasis on the fact that LuaRendering is save/loaded and deterministic, as Rseding says. I hope no mod is attempting to recreate render objects on every tick or on load or something because they assumed the objects are not save/loaded.
Sorry and thanks. It was in my train of thought while making my post and they ended up posted together.

Now that I think about it, it probably is a misbehaving mod as Bilka suggested, I had the same mods enabled in sp as on the server when trying to figure out the problem in SP. Never considered a mod screwing me over like that. :/
Post Reply

Return to β€œWon't implement”