Working lua need advice deciding which method is best

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

User avatar
Spannerbag
Posts: 537
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Working lua need advice deciding which method is best

Post by Spannerbag »

Hi everyone,
Thanks for all your help and patience, much appreciated.
I now have a short lua snippet in my campaign that does what I want (it's not fully tested yet and I need to check it's replay safe but the actual logic works).
I'm a long way from being familiar/confident with lua but at least I have some sort of basic ability now. :D

A question regarding scope if I may:
Am I right in thinking that lua code visibility/scope is the same as events?
That is if you load the lua code in _main.cfg it is available to all scenarios whereas if you put it into a scenario, it's only executed in that scenario?

That's what I get from the wiki but I'd like to be certain; I only need to use this bit of code in one scenario so I put it there rather than _main.cfg.

Cheers and thanks again,
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Celtic_Minstrel
Developer
Posts: 2241
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Working lua need advice deciding which method is best

Post by Celtic_Minstrel »

_main.cfg has no relevance to anything. You could put your scenario in _main.cfg if you really wanted to and there would be no issues.

If you put the Lua code inside a scenario, it will be executed in that scenario. If you put it in a campaign, it will be executed in every scenario in that campaign. If you put the Lua code in an era, it will be executed in any scenario using that era. If you put the Lua code in a modification or resource, it will be executed in any scenario using that modification or resource. And of course, if you put the Lua code in an event or menu item, it will be executed only when that event or menu item fires.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Spannerbag
Posts: 537
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Working lua need advice deciding which method is best

Post by Spannerbag »

Celtic_Minstrel wrote: April 15th, 2024, 12:49 pm ...If you put the Lua code inside a... And of course, if you put the Lua code in an event or menu item, it will be executed only when that event or menu item fires.
Interesting, thanks.
I hadn't realised you could "isolate" lua code from everywhere in a campaign except for one event...
But you've confirmed what I was wondering; lua inside scenario "A" will never be accessible/executed by scenario "B".

Thanks again,
cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Celtic_Minstrel
Developer
Posts: 2241
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Working lua need advice deciding which method is best

Post by Celtic_Minstrel »

Correct.

Note that putting Lua in an event doesn't fully isolate it from the rest of the scenario though. It will only be executed when that event fires, but that code can change global variables, move units around, and do many other things that affect code running elsewhere in the scenario. On the other hand, Lua code in different scenarios are fully isolated from each other and can't affect each other. Any global variables are wiped when you exit the scenario (even if you just reload).
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Spannerbag
Posts: 537
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Working lua need advice deciding which method is best

Post by Spannerbag »

Celtic_Minstrel wrote: April 15th, 2024, 1:08 pm Correct.

Note that putting Lua in an event doesn't fully isolate it from the rest of the scenario though. It will only be executed when that event fires, but that code can change global variables, move units around, and do many other things that affect code running elsewhere in the scenario. On the other hand, Lua code in different scenarios are fully isolated from each other and can't affect each other. Any global variables are wiped when you exit the scenario (even if you just reload).
Excellent, that's very clear. Thank you.

Had a success with lua earlier today, read the wiki, threw together lua and WML, wrapped up custom tags in an event... and it worked first time! :shock:
The whole task only took about 15 minutes (OK, it was a really simple task using the lua gui but that's the first clean run I've had). :D

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
Post Reply