Where/How to get the Month number and Day of the month


(Nathanius) #1

Hi all,

I’ve been trying to add some date-based randomisation to the UI and am getting crashes. I am not sure if the date() function is just not accessible outside of the gamerule script or what…

To start with I was just trying to get two numbers, the day of the month and the number of the month into the log. I added the following to *\ui\newui\loadingscreen.lua

	local dateMonth = date("%m")
	local dateDay = date("%d")
		
	print("Month: "..dateMonth..", Day: "..dateDay) 

Log just spits this out

 LUA 'DATA:UI/NewUI/LoadingScreen.lua' -- parameter: <name> or `[' expected;
  last token read: `local' at line 16 in string "" 

I can see date() used successfully elsewhere… just want to know if I can get these values for the UI to use :frowning:


(ajlsunrise) #2

Don’t use local?


(Nathanius) #3

Tried that of course :wink:

 LUA 'DATA:UI/NewUI/LoadingScreen.lua' -- parameter: `}' expected (to close `{' at line 1);
  last token read: `dateDay' at line 17 in string ""

(Trebic) #4

Are you adding these to an array ? The errors seem to indicate you are trying to …


(Nathanius) #5

I don’t intend to add them to an array… I just want the day # of the month and the month number :stuck_out_tongue:


(Trebic) #6

Place your edits at the beginning of the file, before the first {


(Nathanius) #7

Looks like it’s just not accessible from the UI :frowning:


(Snake_B5) #8

What’s the result if you do this :

local dateMonth = MainUI_ScarEvent([[date("%m")]])
local dateDay = MainUI_ScarEvent([[date("%d")]])

print("Month: "..dateMonth..", Day: "..dateDay)

or this :

MainUI_ScarEvent([[
	local dateMonth = date("%m");
	local dateDay = date("%d");
	print("Month: "..dateMonth..", Day: "..dateDay);
]])

(the two with or without “local”)

Edit :
I’m pretty sure the first one won’t work (it’s been a long time, sorry), but the second one should.
The problem is that dateMonth and dateDay will be declared in the MainUI_ScarEvent scope, and won’t be visible outside of it.


(Nathanius) #9

So for the SCAR event I get this

 LUA 'DATA:\UI\NewUI\Main\Background.lua' -- parameter: attempt to call global `MainUI_ScarEvent' (a nil value)
stack traceback:
   1:  main of string "" at line 3 

And for the interestingly formatted second example, I get this

 LUA 'DATA:\UI\NewUI\Main\Background.lua' -- parameter: attempt to call global `MainUI_ScarEvent' (a nil value)
stack traceback:
   1:  main of string "" at line 8 

I think that on load of the “main” UI most of the LUA functions are not yet accessible :frowning:


(Snake_B5) #10

Argghh, yeah, I totally forgot that MainUI_ScarEvent is not accessible at the root of an UI file :’(


(Nathanius) #11

I could possibly read dates from a file that’s been updated by the gamerule when you fire up the skirmish setup… Won’t be precise for the main background per say but it could at least give a rotation over time and it would be reliable for the loading screen :thinking:

//EDIT
No gamescript LUA can be run before the game starts… oh well :frowning:


(Snake_B5) #12

You can use this trick elsewhere !

In your ui file, you can add your code just after
youruiscreen = {

onShow = [[
    	MainUI_ScarEvent([[
    		local dateMonth = date("%m");
    		local dateDay = date("%d");
    		print("Month: "..dateMonth..", Day: "..dateDay);
    	]];
]],

You could even try the following because the date() function perhaps works under the onShow/onHide/onMouseClicked/etc… scope :

onShow = [[
	local dateMonth = date("%m");
	local dateDay = date("%d");
	print("Month: "..dateMonth..", Day: "..dateDay);
]],

(Nathanius) #13

Did you get this actually spitting something out into a log? I tried adding it into background.lua and it just gave me more of the same :thinking:


(Snake_B5) #14

I didn’t try it. I just looked at my old code.
I’ll try to try it in the following days.