The Gnats Lua questions (creating an event using lua)

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

Moderator: Forum Moderators

User avatar
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

I unfortunately received a stack traceback error "The local variable the_killer must be assigned before being used"

However my variable was assigned! I even proved so by displaying a message where the narrator says: "The killer = $the_killer.side"
What would be causing this problem?

Here is a screenshot of the error:
error.png
error.png (9.76 KiB) Viewed 5851 times
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by Ravana »

The_Gnat wrote:I unfortunately received a stack traceback error "The local variable the_killer must be assigned before being used"

However my variable was assigned! I even proved so by displaying a message where the narrator says: "The killer = $the_killer.side"
That only means it exists as wml variable, not as Lua variable.

By not using quotes, you ask variable to be evaluated.
User avatar
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

Sorry about confusion my exact code is this:

Code: Select all

       {MESSAGE_SPEAKER narrator (The killer: $the_killer.id & Side: $the_killer.side) ()}

        [lua]
            code=<<
		    local victoriousSide wesnoth.get_variable(the_killer.side)

                    for counter = 1, 3 do

			if counter ~= victoriousSide then
				wesnoth.sides[wesnoth.current.side].defeat_condition = "always"
			end

                    end
            >>
        [/lua]
As you can see the message is Outside the Lua code. And the message confirmed that the variable the_killer was real and had a value.
For simple variables I can say use quotes, for objects see reference.
I will try doing it again with quotes: local victoriousSide wesnoth.get_variable("the_killer.side")
User avatar
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

:augh: How am i this stupid!
User avatar
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

Hmm... I have added an "=" but it still gives the same error...
Spoiler:
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by Ravana »

By not using quotes, you ask variable to be evaluated.
User avatar
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

Finnally!

Thank you ravana (very much) for helping me! The combination of problems (and my own stupidity) led me to spend far more time than it should have taken.

Finally working code:

Code: Select all

        [lua]
            code=<<
                    local victoriousSide = wesnoth.get_variable("the_killer.side")

                    for counter = 1, 3 do

                          if counter ~= victoriousSide then
                                 wesnoth.sides[wesnoth.current.side].defeat_condition = "always"
                          end

                    end
            >>
        [/lua]
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by Celtic_Minstrel »

By leaving out the = you are telling Lua: "I want to declare a local variable victoriousSide and set it to nil. Then I want to get the value of $the_killer.side but do nothing with it." In effect, Lua thinks you've just put two commands on the same line, rather than one command to get $the_killer.side and assign its value to victoriousSide.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

Yes i realized i needed a "=" but for some (stupid) reason i kept forgetting to add it when i rewrote my code. Thank you! :D
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by Celtic_Minstrel »

I was just pointing out why Wesnoth would have accepted that incorrect code without an error message.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply