Bushmanni Posted September 30, 2013 Posted September 30, 2013 I have been having strange issues with my DCS Battlefield mission. One of the strangest has been some variables being assigned the value of another variable in a semi random fashion. I say semi random as the mission executes always the same as long as I don't change the code, but if I add or remove stuff before the problem line the behavior changes in a seemingly random fashion. I'm suspecting this is somehow related to scheduled functions interrupting the flow of other functions and maybe altering the variables being evaluated in other functions. My question is if my problems can be caused by function scheduling or must it be something else? I'd like to know also if alteration of a table is even possible to happen because of other function starting to run when other one is still executing. I have already figured out a possible solution to scheduling problems but it would be quite a lot of work to make the changes and as such I'd like to get some verification that trying to fix scheduling conflicts has at least a theoretical chance to help. Basically I'm planning on using scheduling and events to only raise a flag when some function needs to run and calling all the functions inside a loop that reschedules itself after it has run through instead of right in the beginning. It would also have some kind of watchdog system to reschedule it if it stops for some reason. This way I think it's guaranteed to never have one function to run and change data some other currently running function is also using and changing. DCS Finland: Suomalainen DCS yhteisö -- Finnish DCS community -------------------------------------------------- SF Squadron
Grimes Posted September 30, 2013 Posted September 30, 2013 Well it depends on what the functions being scheduled actually do. If they all manipulate the same data then it is easily possible that the functions might be causing unexpected behavior. Its generally a good idea to have one main function that monitors and runs everything else. So instead of having: function 1 rescheduled 5 seconds from now function 2 rescheduled 5 seconds from now function 3 rescheduled 5 seconds from now main() function main () main reschedule 5 seconds from now func1() func2() func3() end The right man in the wrong place makes all the difference in the world. Current Projects: Grayflag Server, Scripting Wiki Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread) SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum
St3v3f Posted October 1, 2013 Posted October 1, 2013 The random number generator used by DCS / LUA is pretty bad. It'll always generate the same sequence unless you do something about it. So, there is the function math.randomseed(number) to give it a seed and a different sequence to generate, but there's two problems: 1: What to use? An easy way would be to use the current system time, but that ain't possible in DCS because functions like os.time are disabled for security reasons. 2: Oh, did I mention that the randomseed function doesn't exist in the SSE? What I did in one of my missions, was to iterate through all the client slots, add their coordinates and then generate some unused numbers. After that, there is at least some sort of randomization. Sort of like this: local seed = 0 for i, unit in pairs(mist_DBs_humansById) do for i, unit in pairs(race[this].units) do local position = Unit.getByName(unit.unitName) if position ~= nil then position = position:getPosition().p seed = (seed + position.x + position.y + position.z) % 1000 end end end for i = 0, seed do math.random(i) end aka: Baron [sIGPIC][/sIGPIC]
Grimes Posted October 1, 2013 Posted October 1, 2013 math.random is actually a custom function the sim uses that overwrites the default lua math.random Random seed is generated by the simulator on mission start and then affects all the queries in the simulator including math.random() function Random seed is stored in track to be sure the things happen the same way it was in the original mission. But yeah, popping a few math.random() off at the start of the mission generally gets around the issue with random number generators based off of the same seed. The right man in the wrong place makes all the difference in the world. Current Projects: Grayflag Server, Scripting Wiki Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread) SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum
Recommended Posts