Best way to pass data between Lua files?

Smeg

Chieftain
Joined
Sep 11, 2005
Messages
33
Hi,

What I'm trying to do is to trigger a popup when a specific event is triggered and then execute custom functions.

So what I'm struggling with is passing data from one Lua to another. The popup triggers but does not take any notice of anything after that.

If I call this function a popup appears, I can't really customise it. I've had a look at the mod UI tutorials (dialog 1 through to 6) and ideally I am looking to call this functionality from another file. However, nothing gets passed to the other file in order to trigger it.

Spoiler :

function TestPopup()

local popupInfo = {
Data1 = 500,
Type = ButtonPopupTypes.BUTTONPOPUP_TEXT,
}
popupInfo.Text = "HELLOWORLD";
UI.AddPopup(popupInfo);

end
Events.SerialEventGameMessagePopup.Add( OnPopup );


My understanding is that the Events.SerialEventGameMessagePopup.Add is adding the test function functionality to the (OnPopup), so If I the call just Events.SerialEventGameMessagePopup( OnPopup ) then it should execute the above, right? I've tried this but can't seem to get it to work.

Is it because everything is 'local' here and I need to pass any data to a global to be seen by another Lua file? Couldn't I just add an include instead?

Am I making sense to anyone? Can anyone point me in the right direction?

Thanks,
Smeg
 
If you just need to share data between Lua contexts, search for MapModData (or ModMapData can never remember which one it is!) over on the CivV C&C forums.

However, if you want to pass data from one Lua context as part of starting an action (showing a popup) in another, LuaEvents are the way to go.

The core UI.AddPopup() method seems like it should do everything you need, but it has several "gotchas"
  • you must use it EXACTLY as designed, with all the correct ShowHideHandler processing or it has a habit of crashing the game core
  • despite passing a Lua table(hash), as the call goes in and out of the DLL only a few keys are actually handled. So if you add popup.myData, it won't be there on the other side of the call, as the DLL is hard-coded to only handle the standard keys
  • your popup is queued (with the lowest priority), not called immediately, so you may receive other "higher priority" popups first

So, LuaEvents ...

In your popup context, register a listener for your custom event
Code:
LuaEvents.MyPopupEventName.Add(function () {
  -- Do some pre-popup processing

  -- Show the popup
  ContextPtr.SetHide(false)
})

then in the other Lua context, call the event to display the popup
Code:
-- Display my popup in the other context
LuaEvents.MyPopupEventName()

To pass data, just add parameters to the function and the call
Code:
LuaEvents.MyPopupEventName.Add(function (iWidth, iHeight, sMessage) {
  -- Set the popup width and height

  -- Set the text to display

  -- Show the popup
  ContextPtr.SetHide(false)
})

Code:
-- Display my popup in the other context
LuaEvents.MyPopupEventName(400, 250, "Hello World")

if you have lots of parameters to pass, just put them in a table
Code:
LuaEvents.MyPopupEventName.Add(function (data) {
  -- Set the popup width and height, from data.Width and data.Height

  -- Set the text to display from data.Text

  -- Show the popup
  ContextPtr.SetHide(false)
})

Code:
-- Display my popup in the other context
LuaEvents.MyPopupEventName({Width:400, Height:250, Text:"Hello World"})
 
Hi whoward69,

Thanks for this I've tried it with a simple hello world from one to another and it seems to be working. - I think you are the creator of the dialog pdfs i've been reading?!

If I wanted to call say Dialog4.xml which is in your pdf - what's the best way of doing it without adding the UIAddon thing at the start?

The reason being is if I edit the XMl files I can get some nice popups with multiple selection/boxes as per the tutorial. However, I can't call them other than it appearing at the start of the game. I have tried using the Lua context method as suggested for this and it doesn't seem to work.

Nonetheless, this has been most useful thanks.

Thanks,
Smeg
 
Hi,

So I've figured this out based on your previous post and your information on contexts.

If I load another context as per below code:

Spoiler :

path = "assets/popups/test";
local newContext = ContextPtr:LoadNewContext(path);


I can now see whatever I put in the XML file. So problem solved.

Is the above method an okay to do things or is it messy? :)

Thanks again
Smeg
 
No more or less messy than the UI in general ;)

I tend to go with "whatever I can get working!"
 
Back
Top Bottom