Lua function not being accepted

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

Moderator: Forum Moderators

Post Reply
User avatar
Lord-Knightmare
Discord Moderator
Posts: 1305
Joined: May 24th, 2010, 5:26 pm
Location: Somewhere in the depths of Irdya, gathering my army to eventually destroy the known world.
Contact:

Lua function not being accepted

Post by Lord-Knightmare »

Hello,

I have this Lua function which is not being accepted by the lua_function= key and this generates the following error:
Screenshot 2021-02-07 004528.png
Screenshot 2021-02-07 004528.png (94.11 KiB) Viewed 433 times
LuaWML code

Code: Select all

        [lua]
            code=<<
                function high_level_unit(unit)
                    if (unit.level >= 2) then
                        return true
                    else
                        return false
                    end
                end
            >>
        [/lua]

Code: Select all

        # more MAI
        [micro_ai]
            side=6
            ai_type=simple_attack
            action=add

            ca_score=110001
            [filter]
                type="Lich Lord"
            [/filter]
            [filter_second]
                lua_function = "high_level_unit"
            [/filter_second]
        [/micro_ai]
What am I doing wrong in the Lua function such that I keep getting this error?

This is where I got the example
mattsc
Developer
Posts: 1182
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Lua function not being accepted

Post by mattsc »

Where did you put the [lua] tag? In the Simple Attack MAI test scenario (which is where the code example on the wiki comes from) it's in a preload event.
User avatar
Lord-Knightmare
Discord Moderator
Posts: 1305
Joined: May 24th, 2010, 5:26 pm
Location: Somewhere in the depths of Irdya, gathering my army to eventually destroy the known world.
Contact:

Re: Lua function not being accepted

Post by Lord-Knightmare »

mattsc wrote: February 6th, 2021, 7:11 pm Where did you put the [lua] tag? In the Simple Attack MAI test scenario (which is where the code example on the wiki comes from) it's in a preload event.
Oh, that solved it! Thank you! Initially, I had assumed that including it in the prestart event would have made it available but seems [lua][/lua] only works in preload, or inside a [filter_condition]
mattsc
Developer
Posts: 1182
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Lua function not being accepted

Post by mattsc »

In principle it should work in a prestart event, I think, but then it will be lost if you start from a not-start-of-scenario save.
User avatar
Celtic_Minstrel
Developer
Posts: 1775
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Lua function not being accepted

Post by Celtic_Minstrel »

[lua] works in any event, but if it's defining something that needs to be available globally, then it needs to be in preload.
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.
Post Reply