Working lua need advice deciding which method is best
Moderator: Forum Moderators
- Spannerbag
- Posts: 556
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Working lua need advice deciding which method is best
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.
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
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
Cheers and thanks again,
-- Spannerbag
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.
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
- Celtic_Minstrel
- Developer
- Posts: 2278
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Working lua need advice deciding which method is best
_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.
- Spannerbag
- Posts: 556
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Working lua need advice deciding which method is best
Interesting, thanks.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.
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
- Celtic_Minstrel
- Developer
- Posts: 2278
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Working lua need advice deciding which method is best
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).
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).
- Spannerbag
- Posts: 556
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Working lua need advice deciding which method is best
Excellent, that's very clear. Thank you.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).
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!
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).
Cheers!
-- Spannerbag