Jump to content

Modding missionbulletins.lua


FuryoftheStars

Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...