Scripting an on/off radio - Printable Version +- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum) +-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html) +--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html) +---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html) +---- Thread: Scripting an on/off radio (/thread-21382.html) Pages:
1
2
|
Scripting an on/off radio - Kalidus - 05-03-2013 Hey! I have now hit my first scripting wall and hope you guys can help. I have a map where I have a static model radio placed at a table. I want to be able to interact with it to turn it on and off. So far I have the following "items" in my scene: 1. The radio - a static model. 2. Script Area around the radio. Area name field = radioarea. PlayerInteractCallback field = Radio. 3. Sound entity (the little speaker icon). Name field = radio1. It is set to Active, and I have not defined a Sound Entity file to it, as I will do this through the script. I want the music to play only within the area I have defined on the sound entity, and not across the entire map. I have looked through the wiki and searched the forum and Google, but I haven't been able to get it working. Any help would be appreciated! HPS file: Code: void OnStart() SNT file: Code: <SOUNDENTITY> RE: Scripting an on/off radio - Tomato Cat - 05-03-2013 As far as turning the radio on/off goes, you can try something like this: PHP Code: void OnStart() That *should* work. RE: Scripting an on/off radio - PutraenusAlivius - 05-04-2013 PHP Code: void OnStart() RE: Scripting an on/off radio - TheGreatCthulhu - 05-04-2013 Because of the way sound entities work(the ones you place in the level editor), if you happen to spawn inside their area of audibility, they start their playback as soon as the level starts. So, to have a more precise control of timing, use the PlaySoundAtEntity function as explained above. You can also use PlaySoundAtEntity to play any snt file; you don't have to have a corresponding sound entity in the level (essentially, it dynamically creates one for you). To control the volume, and how far the sound is audible, you have to uncheck the "use default" checkbox on the Sound tab, or the sound entity will use the values defined in the snt file. If that doesn't work by any chance, or if you want to make the sound loop, you'll have to create your own snt file, but that's not hard to do. These are just plain text files which contain some additional information about the sound. You can make a copy of an existing one, rename it, open it in a plain text editor such as Notepad, Notepad++, Geany, or similar, and then just change a few values. Here's how 00_creak.snt looks like (in redist\sounds\00\) - note the PROPERTIES tag: Code: <SOUNDENTITY> Here's just the PROPERTIES tag in a more readable format, so that I can tell you what each the attributes is for: Code: <PROPERTIES
P.S. In the code snippet in the previous post, there's a minor mistake, that you must correct for the code to work. In the SetEntityPlayerInteractCallback function call, one of the parameters says "RadioPlay", but the corresponding callback function, a few lines bellow, is named Radio. The two must have the same name. So either change PHP Code: void Radio(string &in asEntity) PHP Code: void RadioPlay(string &in asEntity) or leave it as is and change "RadioPlay" parameter to "Radio". RE: Scripting an on/off radio - Daemian - 05-04-2013 Hey Cthulhu, What if this radio is not an entity but an item in your inventory. How do you capture its activation to turn music on/off? Thank you. RE: Scripting an on/off radio - TheGreatCthulhu - 05-04-2013 Hm... You mean, like, click on the item in the inventory to turn the radio on or off (silent hill style)? I think that's somewhat problematic, since items are really designed for two things - to be used on the world entities, and to be combined. These are the events that the game can "detect", IIRC, there's really no way for the script to detect a click on an inventory item. So, I guess you'd have to work something out using those. For example, you could have the items "Radio" and "Radio Batteries", combine them, and use AddCombineCallback to detect this event. Then, under the excuse that the batteries are nearly empty, you could use a timer to turn off the sound after a short while (and replace the combined item "Radio with batteries", with the empty "Radio"). To play the sound, you could simply use PlaySoundAtEntity, passing "Player" as the 3rd parameter (asEntity), or even the PlayGuiSound function. Anyway, it's just an idea, you'll need to test it out a bit. I'll see if I can come up with something and get back to you, and in the meantime, maybe someone else will have an idea as well. P.S. If you're making a full conversion: I've seen people were able to replace the lantern with a flashlight; I suppose you can also replace it with other things - like with a radio. In such a scenario, you could use SetLanternLitCallback to turn the radio on or off. RE: Scripting an on/off radio - Kalidus - 05-04-2013 Thanks for the nice and detailed answers so far guys! I have tried both Mr Credits and JustAnotherPlayer's scripts. Both of them makes me able to turn the radio on, but not off. When I turn it on, music plays. When I turn it off, the music keeps playing and if I turn it on again, the music track loads again behind the other one so it plays double... I can't find the reason for this, as the code seems logical and correct enough. I have removed the Sound entity from the map so I just have the Script Area now, to emit the sound. RE: Scripting an on/off radio - Tomato Cat - 05-04-2013 Does playing/stopping it from the script area work? RE: Scripting an on/off radio - TheGreatCthulhu - 05-04-2013 Both scripts are setup so that you can both start and stop the playback. In the first script, you have to add your own code for playback start and end. The second script (by JustAnotherPlayer) has all the code in place, but there's another problem with it that I overlooked earlier: PHP Code: PlaySoundAtEntity("", "radio.snt", "radioarea", 0.0, false); The StopSound function takes this ID as a parameter, not the name of the sound file. (This is because you may have the same sound file played on several places, and you don't want to stop them all at once.) So, change this: PHP Code: PlaySoundAtEntity("", "radio.snt", "radioarea", 0.0, false); to this (I've chosen to use "id.sound.radio"): PHP Code: PlaySoundAtEntity("id.sound.radio", "radio.snt", "radioarea", 0.0, false); and then, a in the else branch, change this: PHP Code: StopSound("radio.snt", 0.5f); to PHP Code: StopSound("id.sound.radio", 0.5f); Here's the revised version of the entire script: PHP Code: void OnStart() That should do it. RE: Scripting an on/off radio - Tomato Cat - 05-04-2013 Wouldn't it also be necessary to increment/decrement the Radio variable in the case that the radio sound was to be toggled? Unless I had misinterpreted and this is not what the OP specified. |