• We are currently performing site maintenance, parts of civfanatics are currently offline, but will come back online in the coming days (this includes any time you see the message "account suspended"). For more updates please see here.

Using timer.performWithDelay inside lua script

MadSysop

Chieftain
Joined
Jun 24, 2017
Messages
6
Hi

I'm trying to use the timer module (https://docs.coronalabs.com/api/library/timer/performWithDelay.html) inside lua script. I have tried, but failed with error "function expected instead of nil" on the line, where I call "timer.performWithDelay(1000, listener)". Could the timer be restricted by Civ or I'm doing something incorrectly?
I have loaded the file as VFS=true.
--------------------------
local timer = include( "timer" )

if timer == nil then
print("timer is nil")
else
print("timer exists")
end


function listener( event )
print( "listener called" )
end

function OnPlayerDoTurn(iPlayer)
print("Running OnPlayerDoTurn for " .. iPlayer)
timer.performWithDelay( 1000, listener )
end
GameEvents.PlayerDoTurn.Add(OnPlayerDoTurn)
--------------------------
 
the include command in civ5 literally copies the entire contents of file "X" into file "A" when file "A" has the command:
Code:
include("X")
It copies the code from file "X" into file "A" at the place where the include command is shown within file "A".

Are you getting "timer is nil" or "timer exists" printed into the lua log when you load the game?

Which file is set as "Import Into VFS= true" ? In order for the include command to work file "X" must be set as VFS=true.

Also, local variables created in file "X" will be nil in file "A".

----------------------------

The actual mod itself would make it easier to understand what it is you are attempting and what it is you may be doing wrong.

However, one question: why do you want to create or use timed delays between when an event occurs in the game and when your lua-code executes? I would think this would only tend toward a condition wherein de-synchs between active current game-state and the code you are executing after a delay are rampant.
 
I'm getting "timer exists". VFS=true is set for the lua script that includes the code.

The mod is just this one lua script file right now. I'm testing out if my idea is feasible technically.
I'm trying to create a better combat system for multiplayer for simultaneous turns. Idea is to activate units in predictable order, depending on their initiative (cavalry would have higher initiative than ranged etc). So the players could move higher initiative units before lower initiative ones. My idea is remove units move points at the start and to use the timer, so the units would get their move points back sooner or later, depending on the initiative. If the timer cannot be used, I need to figure out some other system.
 
The include("X") command in civ5 lua copies the contents of file "X.lua" (which must be imported into the game's Virtual File System) into the code of the lua file where command include("X") occurs.

----------------------------------------------------------------------

This line is attempting to add the contents of file "timer.lua" into the code of file "TestScript.lua":
Code:
local timer = include( "timer" )
No such file exists, however.

-----------------------------------------------------------------------

Setting this is incorrect because file TestScript.lua is being loaded into the game as a "InGameUIAddin" file:
Code:
<File md5="1B6BAB6F3F9C94700CA8F19D40B07E36" import="1">TestScript.lua</File>
No file is ever designated as both VFS=true and "InGameUIAddin". See William Howard's guide on modbuddy file settings for civ5: whoward69's what ModBuddy setting for what file types tutorial.

-------------------------------------------------------------------------

There is no pre-existing file called "timer.lua" that is part of Civ5's set of base game-files, therefore there is no "content" that can be loaded into file TestScript.lua from the "timer" file.

--------------------------------------------------------------------------

I suspect that what is occuring is that this line
Code:
local timer = include( "timer" )
is creating local variable "timer" and setting its value to boolean false which != nil. You can verify whether this is the case by checking for variable "timer"s lua-type:
Code:
if (type(timer) == "boolean") then print("timer is boolean") end
You can also just do
Code:
print(tostring(timer))
 
I see. I did the following test (removed the lua script file from VFS now):
------------------
local my_timer = include( "timer" )
print("os: " .. tostring(os))
print("timer: " .. tostring(timer))
print("my_timer: " .. tostring(my_timer))
------------------
And got to Lua.log:
[517747.765] TestScript: os: table: 1B6C8630
[517747.765] TestScript: timer: nil
[517747.765] TestScript: my_timer: table: 1B6CE9B0

The question is, if the "os" is included automatically and the "timer" is not, should I conlcude that I cannot use the Corona timer module?

Do you have any idea how I could activate units in a predictable fashion from the turn start?

If I could do efficient sleep (I think "while (os.clock() < sec) do ... end" wouldn't be the best solution), then I could construct my own timer module. I understand that "os" package is quite restricted, but has access to clock, date, time and timediff functions.
 
'os' is standard to lua.

--------------------------------

Dumping your last code into one of my mods what I get is that "my_timer" is an empty table.

-------------------------------

In order to use the functionality you are talking about, you would need to copy the lua code-contents of the "timer" module into file "timer.lua" and you would need to state your include command as like this, previous to any attempt to use those functions
Code:
include("timer.lua")
You don't want to attempt to send the parsed contents of the "timer.lua" file into a variable, but rather you want to make the functions defined within the "timer.lua" file available to the code in the file where the include command occurs.

---------------------------------

And you really do need to show the file extension name in an include command because otherwise the specially-written include(X) that is part of civ5's lua might find and use a file with extention ".xml" if one exists in the VFS.

Civ5's include(x) command is also a bit idiosyncratic in that both a file called "My_timer.lua" and a file called "timer.lua" can satisfy the string-matching conditions of the "include" command if you state include("timer") or include("timer.lua"), and if two such files exist in the game's VFS system you will have no control whatsoever over which of the two files is used.

And if you simply state include("timer") you could retrieve the contents of an lua file that is named "Joes_timer_that_is_timing_times.lua"

------------------------------------

If the contents of the timer code is not too lengthy you can also just copy the functions from that Carona place into your lua file, or create your own versions of the timer functions and place them directly in the file where you intend to use them.
 
Thank you for the explanations about the include strange syntax.

Did I understand correctly that Civ is using bare Lua, not the Corona SDK? At least it's not offering the Corona modules for Mods lua scripts.

I cannot really copy-paste the timer.lua module, becuase it's the Corona SDK module. Probably implemented in C++ or something else low level. Is there a possibility to add some low level code to the Mod? Is there a tutorial for that kind of modding?
 
If by low level you mean DLL level, then, yes, you can create and import into the game via a mod a custom DLL. But it is an all-or-none proposition. To make even the smallest changes via a mod to the standard DLL you are replacing the existing DLL file with a custom one with your additions/alterations added to the file.

But DLL-level modding is not anything I am conversant in. There were only a handful of people who modded at DLL level for civ5, and so far as I know the only one still active is Gazebo who runs the CP/CBP/Vox Populi thingie. William Howard seems to have retired from modding, but you could check his Various Mod Components DLL mod for hints on what a DLL-level mod needs to have in order to alter the game's DLL. And there can only be one DLL mod active at any one time.

-----------------------------

re whether civ5 is using "bare" lua as opposed to a Corona version of it, to be honest I never heard of Corona and its whatsis before you linked to it. My understanding is that Firaxis is using standard lua 5.1 (or perhaps a somewhat later version) with a few specific changes such as the "include" command as implemented by Firaxis, and of course the API methods and functions they've added to the game's version of lua.
 
Back
Top Bottom