FuryoftheStars Posted April 2, 2020 Share Posted April 2, 2020 Hey, I'm trying to mod missionbulletins.lua, but because of the way this file was written, I'm having some trouble hooking the functions. Anyone have an idea on this without my overwriting the whole thing? The functions themselves as well as some values they use are wrapped in an if statement. Here's the start of it: package.path = package.path .. ";data/scripts/lib/?.lua" include ("stringutility") include ("randomext") -- Don't remove or alter the following comment, it tells the game the namespace this script lives in. If you remove it, the script will break. -- namespace MissionBulletins MissionBulletins = {} if onServer() then local r = Random(Seed(os.time())) function MissionBulletins.initialize() end function MissionBulletins.getUpdateInterval() return 1 end function MissionBulletins.updateServer(timeStep) MissionBulletins.updateBulletins(timeStep) end local updateFrequency = 60 * 60 local updateTime function MissionBulletins.updateBulletins(timeStep) if not updateTime then -- by adding half the time here, we have a chance that a military outpost immediately has a bulletin updateTime = 0 local minutesSimulated = r:getInt(10, 80) minutesSimulated = 65 for i = 1, minutesSimulated do -- simulate bulletin posting / removing MissionBulletins.updateBulletins(60) end end updateTime = updateTime + timeStep -- don't execute the following code if the time hasn't exceeded the posting frequency if updateTime < updateFrequency then return end updateTime = updateTime - updateFrequency MissionBulletins.addOrRemoveMissionBulletin() end --etc Here's what I'm trying: -- Don't remove or alter the following comment, it tells the game the namespace this script lives in. If you remove it, the script will break. -- namespace MissionBulletins if onServer() then local ostime = os.time() local oldMissionBulletins_updateBulletins = MissionBulletins.updateBulletins function MissionBulletins.updateBulletins(timeStep) updateFrequency = 15 * 60 -- I'm hoping this changes the updateFrequency value.... oldMissionBulletins_updateBulletins(timeStep) end -- Complete overwrite function MissionBulletins.addOrRemoveMissionBulletin() local scripts = MissionBulletins.getPossibleMissions() local count = r:getInt(0,5) if count > 0 then for i = 1, count do local scriptPath = MissionBulletins.getWeightedRandomEntry(r, scripts) local ok, bulletin = run(scriptPath, "getBulletin", Entity()) if ok == 0 and bulletin then bulletin["TimeAdded"] = ostime Entity():invokeFunction("bulletinboard", "postBulletin", bulletin) end end end Entity():invokeFunction("bulletinboard", "checkRemoveBulletins", ostime, r) end end The actual error I'm getting is that "r" is a nil value from this line: local count = r:getInt(0,5) "r" is defined in the original file, though as a local value after the start of the if onServer() statement. Link to comment Share on other sites More sharing options...
Madranis Posted April 9, 2020 Share Posted April 9, 2020 As you mentioned, r is a local variable in the original "if onServer()" block. Once the original onServer() block closes, r is out of scope. It's not accessible in the mod as the mod's "if onServer()" block is a separate scope from the one in the base game's code. Instead, you are referencing a global variable named r, which has no value so returns nil. So there really isn't a good way to try to grab and reuse r from the base game code that I'm aware of. You're pretty much stuck having to re-create it in the mod's code. Link to comment Share on other sites More sharing options...
FuryoftheStars Posted April 9, 2020 Author Share Posted April 9, 2020 I was afraid of that. Dangit, devs. Well, hopefully my recreating r in my scope doesn’t cause other issues. I’ll have to double check where it’s used to make sure everything stays sync’d up properly. Link to comment Share on other sites More sharing options...
Madranis Posted April 9, 2020 Share Posted April 9, 2020 Is there a reason you copied the function MissionBulletins.getWeightedRandomEntry() into the mod? It doesn't look to be modified, so you should be able to call and use the base game's instance of it. Link to comment Share on other sites More sharing options...
FuryoftheStars Posted April 9, 2020 Author Share Posted April 9, 2020 Yeah, because of the issue, I just copied the whole file at the time. That said, if that function is the one I'm thinking of, I will be making edits into it later. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now