Random number not being generated on startup

Hi Everyone,

For the Star Trek vs. Homeworld mod, there’s a random number that determines which TOTALLY COOL background gets shown in the menus. It works after every game ends, but on startup it always chooses the middle value. Any ideas on what could cause this or how to fix it?

Thanks!

Heres the file: (it always chooses 4 on startup, but if you set the random number to 3 then it chooses 2 for example)

-- LUA CONFIG FOR THE FE BACKGROUND

--dofilepath("player:STC_DATE.TXT")

local bgnr = random(6)
--local bgnr = Rand(2)

Background = {
	size = {0, 0, 1200, 800},
	stylesheet = "HW2StyleSheet",
	
	RootElementSettings = {
		--backgroundColor = {0,0,0,255},
	Layout = {
				size_WH = { w = 1, h = 1, wr = "scr", hr = "scr" },
		},

	},
	
	-- Flags
	pixelUVCoords = 1, -- Enter pixel coords for texture coords
	preUI = 1,

	;
	
	{
		type = "Frame",
		visible = 1,
		
		Layout = {
			--pos_XY = { x = 0.5, y = 0.5, xr = "par", yr = "par" },
			size_WH = { w = 1, h = 1, wr = "scr", hr = "scr" },
			pivot_XY = { 0.5, 0.5 },
			pos_XY = { x = .5, y = .5, xr = "scr", yr = "scr" },
			--size_WH = { w = 1, h = 1, wr = "scr", hr = "scr" },

			lockAspect=-2,
		},
		
		--local bgnr = (Rand(2))
		
		
		BackgroundGraphic = {
			type = "Graphic",
			--size = {2048, 1024},
			--textureUV = { 0, 0, 2048, 1024},
			uvRect = { 0, 1, 1, 0 },

			--texture = "Data:UI\\NewUI\\Background\\main_bg_"..dateMonth..".tga", -- multires texture

			texture = "Data:UI\\NewUI\\Background\\main_bg_"..bgnr..".tga", -- multires texture

			--texture = "Data:UI\\NewUI\\Background\\main_bg_6.tga", -- multires texture
		},
		
		-- BackgroundVideo = {
			-- filenameV = "data:animatics/mainmenu.webm",
			-- loop = 1,
			-- fitScreen = 1,
			-- fixedAspectRatio = 1,
		-- },
	},
}

Hi,

I ran into the same issue when recoding the music system for Stargate Space Conflict.

It seems to load up the exact same random numbers in the exact same order on boot, so I gave up as clock() isn’t available in the scope of where my code was meaning I couldn’t use the time for a number - it might be available for you in the UI scope though so its worth a try (and the way id recommend).

My predecessors code looked like this (which I immediately deleted, admittedly I didn’t know random was broken at the time but even so its not the best code I’ve laid my eyes upon)

Don’t think it works, haven’t checked however, but thought id share just in case! (id be interested to know if it does!)

function RandomTask()
	local compteurUn = 0
	local compteurDeux = 0
	local compteurTrois = 0
	local compteurQuatre = 0
	local compteurCinq = 0
	local randTest = 0
	
	for i = 1, 1000 do
		randTest = random(5)
		if randTest == 1 then
			compteurUn = compteurUn + 1
		else
			if randTest == 2 then
				compteurDeux = compteurDeux + 1
			else
				if randTest == 3 then
					compteurTrois = compteurTrois + 1
				else
					if randTest == 4 then
						compteurQuatre = compteurQuatre + 1
					else
						if randTest == 5 then
							compteurCinq = compteurCinq + 1
						end
					end
				end
			end
		end
	end
	
	num = random(0, compteurCinq)
	return num
end

Not sure what is going on.

random should be producing different results each time. But keep in mind that random is not multiplayer-safe. It will cause desyncs because each player will generate a different random number.

Or at least that’s what happens in Homeworld 2.

Yeah, it works after the first game. Maybe it’s because the opening logos have been taken out.

(The compatibility mod started out as a copy of the STC, so sometimes I have to look for how things work.)

Anybody know where those things are in the normal files? I can’t find the one for when the game first starts up.

You know, maybe the starts so fast that it doesn’t have a chance to generate the number.

Instead of random, try one of these

number RandomRange(number min, number max)
integer RandomIntMax(integer max)

I don’t know that it’ll produce different results, but it’s probably worth trying.

the ins and outs of randomness in HWR were explored and in some cases explained in some depth back in Syncing random events in multiplayer? which might contain some useful information for you. Or might not, who knows.

On a side note sadly neither of these work in the soundconfig.lua file scope (whatever that maybe)

Yeah, okay. This makes NO sense.

I could get the ‘random’ number to come up with different results on starting, but none that were actually logical, and then when I set it back the way it was, it was returning 1 instead of 4. So I exchanged the background.lua out for the old one and it was back to 4 again. =D

Whatever man. At least background 4 is the one with the infamous Commander Kruge, so at least that’s probably the coolest one. ;D

Here’s some of the stuff I tried out that you guys recommended, but I couldn’t get any of it to work:

-- LUA CONFIG FOR THE FE BACKGROUND

--dofilepath("player:STC_DATE.TXT")


--local spacer = (random(1000000) + random(1000000) + random(1000000) + random(1000000) +1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1)

--local spacer2 = (random(1000000) + random(1000000) + random(1000000) + random(1000000) +1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1)



--returns 1 with intro movie
local bgnr = random(6)


--this always returns 5 at startup:
--local bgnr = (((random(6))))


--these always returns 1 at startup:
--local bgnr = (1-1+(random(6)))

--local bgnr = (1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 +1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 +1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 +1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 +1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 +1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 + 1 -1 +random(6) + 1 -1) 



--these didn't work:

--local bgnr = number RandomRange(1, 6)

--local bgnr = RandomRange(1, 6)

--local bgnr = RandomIntMax(6)

--local bgnr = integer RandomIntMax(6)

--local bgnr = RandomIntMax(6)
--local bgnr = Rand(2)

Background = {
	size = {0, 0, 1200, 800},
	stylesheet = "HW2StyleSheet",
	
	RootElementSettings = {
		--backgroundColor = {0,0,0,255},
	Layout = {
				size_WH = { w = 1, h = 1, wr = "scr", hr = "scr" },
		},

	},
	
	-- Flags
	pixelUVCoords = 1, -- Enter pixel coords for texture coords
	preUI = 1,

	;
	
	{
		type = "Frame",
		visible = 1,
		
		Layout = {
			--pos_XY = { x = 0.5, y = 0.5, xr = "par", yr = "par" },
			size_WH = { w = 1, h = 1, wr = "scr", hr = "scr" },
			pivot_XY = { 0.5, 0.5 },
			pos_XY = { x = .5, y = .5, xr = "scr", yr = "scr" },
			--size_WH = { w = 1, h = 1, wr = "scr", hr = "scr" },

			lockAspect=-2,
		},
		
		--local bgnr = (Rand(2))
		
		
		BackgroundGraphic = {
			type = "Graphic",
			--size = {2048, 1024},
			--textureUV = { 0, 0, 2048, 1024},
			uvRect = { 0, 1, 1, 0 },

			--texture = "Data:UI\\NewUI\\Background\\main_bg_"..dateMonth..".tga", -- multires texture

			texture = "Data:UI\\NewUI\\Background\\main_bg_"..bgnr..".tga", -- multires texture

			--texture = "Data:UI\\NewUI\\Background\\main_bg_6.tga", -- multires texture
		},
		
		-- BackgroundVideo = {
			-- filenameV = "data:animatics/mainmenu.webm",
			-- loop = 1,
			-- fitScreen = 1,
			-- fixedAspectRatio = 1,
		-- },
	},
}

I found the intro movie, but adding it back in didn’t help. That’s what those ‘spacer’ locals were too; trying to give the random # more time to be generated, but that didn’t work either.

The intro movie is set up in animatics\logo.lua but I couldn’t find the file that triggers it, so I think it’s hardwired into the game.

Thanks for the input anyway you guys!

Is it possible to read/write to config file in that part of the code? If so, you could simply go in rotation and store the last shown.

You could try something like this instead:

floor(random() * 6 + 1/2)

That sounds like a good idea. Is there an example of how that might work in the normal files? I see all the menus for setting up stuff like the player’s colors, but I don’t really see how that stuff gets written to the player’s configuration file.

Sadly I had thought of this yesterday too and tried it, but writeto() and readfrom() aren’t available in the UI scope.

1 Like

That’s too bad!

Thanks anyway you guys, but I think it’s hopeless!

I spent a lot of time today trying to set it up like the Continuum background, where it changes every month, but I wanted to make it so that it changes every hour.

Alas, the Star Trek vs. Homeworld mod can’t write to text files! I guess it’s because you have to load more than one mod file at a time. I seriously can’t think of what else it could be, because I could totally get it working in some local test files, but not over steam.

CRUD!