How to call a function from one file in another?

Meister Maggi

Warlord
Joined
Jul 16, 2007
Messages
187
I am trying to invoke a function in cityview.lua from productionpopup.lua but do not manage to do so. (Basically, the matter is related to buying stuff in Vox Populi not giving you the item right away, but rather a discount on the hammers, yet, if I do this for the item currently being built, the buy is not visualized right away in the production meter in the city view).

My idea was to make the function DoUpdateProductionInfo() from cityview global, but so far I miserably failed. In any case, making the function global is a rather drastic solution (it seems to me), so I wonder if it would not be possible to use the Events. ... system applied for other purposes in these files already, but I cannot get a hold on these either.

Does anyone know how to tackle this problem? Thanks a lot in advance.
 
just to push this up again - I did unfortunately not manage to do this yet. Am I just missing something (and the reply is trivial, which is why nobody has answered) or is it so absurd that no one has ever done it (or is the problem description unclear)?
 
My idea was to make the function DoUpdateProductionInfo() from cityview global,

No such thing in CivV Lua - all the lua contexts are sand-boxed (intentionally) from each other. So you can't call a function in one lua file from another.

You can work around this by hooking the function in one file up to a LuaEvent and then triggering the event from the other file. There are also tricks for sharing data structures between contexts. But you need to make changes in BOTH files.
 
No such thing in CivV Lua - all the lua contexts are sand-boxed (intentionally) from each other. So you can't call a function in one lua file from another.

You can work around this by hooking the function in one file up to a LuaEvent and then triggering the event from the other file. There are also tricks for sharing data structures between contexts. But you need to make changes in BOTH files.

Well, I suppose that was bound to be the answer (no global contexts) - thanks for the clarification.

No problem in doing changes in both files, is there some example/documentation (even from the used code) which exemplifies how to hook up a function in one lua file to make it usable in another?
 
Code:
function X(a, b)
  -- do something useful with a and b
end
LuaEvents.RemoteCallX.Add(function (...) X(...) end)

-- Call X from the same context
X(1,3)

Code:
-- Call X from a different context
LuaEvents.RemoteCallX(2, 4)
 
Code:
function X(a, b)
  -- do something useful with a and b
end
LuaEvents.RemoteCallX.Add(function (...) X(...) end)

-- Call X from the same context
X(1,3)

Code:
-- Call X from a different context
LuaEvents.RemoteCallX(2, 4)

Much appreciated - I will try this out. Just wondering on a side note, how did you figure all this out - I mean, afaik, all this is really poorly documented...
 
Much appreciated - I will try this out. Just wondering on a side note, how did you figure all this out - I mean, afaik, all this is really poorly documented...
30+ years of programming experience
 
30+ years of programming experience

That certainly does help!

I finally managed to have a look and try it out, and at least my first attempt did not work as expected - but I did not get an error message either. So just to make sure whether I interpreted two things correctly:
- is the naming of function X and RemoteCallX on purpose, i.e., do I have to provide a call of the form RemoteCallDoUpdateProductionInfo (admittedly, I find that strange..)?
- the two (...) in the argument of RemoteCallX.Add - are these meant to represent the repetition of the entire code of that function (X here) as defined before?

Thanks.
 
... is the Lua notation for "any number of parameters" and should appear as is
 
... is the Lua notation for "any number of parameters" and should appear as is

Right - so it seems I got that one right. I will post the code segments I used - that makes it easier to verify quickly if anything went wrong there.

Code:
--In the definining file (I only added the LuaEvents line):
function DoUpdateProductionInfo( bNoProduction )
...
end
LuaEvents.RemoteCallX.Add(function (...) DoUpdateProductionInfo(...) end)

-- sample call (same file)
DoUpdateProductionInfo( noProduction );

-- call in the other file
LuaEvents.RemoteCallX(false)

Is this correct or should I change the X in RemoteCall as well?
 
Top Bottom