How to keep a variable syncronized through 2 clientside lua files?
16 replies, posted
I have a addon (duh) which does..alot of things. All those things result in a sound being playd. For example one of these results happend when the player takes damage, and the other when he dies. Problem is, when you take damage, and in the next 1 or 2 second die, while the "damage" sound plays, the "death" sound will play too, making the sounds overlap and you have 2 sounds playing at the same time..
The way i would want to fix this is with a variable thats false if no sound plays, and true if a sound plays. Both, death and die sounds will only play when the variable is false. And its true for as long as a play sounds. It resets itself when the sound is done. That way a damage sound can start, and a die sound will not play because theres already the damage sound. BUT i cannot manage to get a variable that stays syncronized in both files. Include doesnt work it instantly crashes my game for some reason...
Both files are clientside, so networking wont really be a thing.
I mean...it COULD send the variable to the server just to send it back to the player in the other lua file but that would be a huge waste of networking, right?
So how would this be possible?
Im not really sure
All you have to do is make the variable global, as in not add "local" to the start
But, couldn't you just check if the damage that the player took drops him to or below 0 and not run it if it
does?
You're definitely over-complicating this.
i tryd with a global variable and it simply did not work
how would i even do that? theres no hook or function that runs when the player takes so much damages that its to or below 0
that also wont work because lets say the player has 11 health. he falls down a building, with sandbox damage he has 1 left and right after that (when also the damage sound started) he gets shot by a npc and dies. the death sound starts as well and tadaa, you have 2 overlapping sounds. again
health_left = ply:GetHealth() - damage_info:GetDamage()
if health_left > 0 then
play_sound()
end
i forgot to mention somethinh. the addon has WAY more functions that just damage and death. And it all plays a sound. Stuff like empty ammo, attacking someone, getting attacked. etc.
And if some stuff happens fast enough after each other the sounds can overlap. So the health thing might fix the dead/damage thing, but i NEED a variable for check, so i can use it in ALL files. so they all can check if a sound is playing right now. So which action even comes first gets to play a sound. if right after that anything else happens no sound will be playd for that. until the first one is done
im using mp3. wav would we way too big.. the addon has alot of sound files, it would be way too much in file size
I hope you realize that you can still use it... all you have to do is check the sound duration manually...
i know..i use timers in there that needed the mp3 duration. Also the code you showed me again will not help. i think...
i simply need a variable that can be used by all files of the addon. and also can be changed by all files, so every file is syncronized with that one variable
Mate, did you even read the code that I spent time to write?
All you have to do is replace your sound playing function with the new PlaySound function and specify
the duration of the sound, it's exactly what you asked for.
You "think"? Why don't you try it and then tell me? It works, I ran a sound through it in a Think hook to
test it.
Yes i looked at your code, and this essentially works. Theres still a problem tho. my addon has for damage/deaths and everything else several sounds. not only one. all of them with a different duration. while i can give the function only one duraction.
https://i.gyazo.com/28333c866f283e96ae6d3e1a1cb935d2.png
I told you, it checks the duration for you... Just look at it
Anyways, I updated the gist and added some functions that make use of the previous ones to produce a
"queue".
https://gist.github.com/JohnsonMcBig/d4393e6265e7645c91bd1e6dd9883981
i have the sounds in a table, but the tables are located in each lua file, each file for a different kind of action the addon makes a sound to
so what's the problem?
if i make all sounds wav theres no problem anymore.
I implemented your first code in the addon. Since the SoundDuration
apparently only works correctly on wav, im gonna try converting the mp3 files to wav. I added a local variable with is SoundDuration
and the timer i used to remove the HUDPaint hook for the screen element gets removed after that duration. so now the HUDPaint element starts when a sound starts, and alos gets removed when the sound stops. ....assuming t he wav files are not too huge and dont make the gma file huge
but i have to ask...how the hell does the code work? XD Ive been reading through it for a long while now, trying to understand it. but i cant figure it out...its using technuiqes i never used or saw before so maybe thats why...but still, i have no idea what it does..specifally with the first 2 functions
btw, one coin for you because that function(s) saved my addons a huge amount of lines of codes. And made everything so much simpler
Sorry, you need to Log In to post a reply to this thread.