Unexplained Sudden Defeat when Moving a Unit

The place to post your WML questions and answers.

Moderators: Forum Moderators, Developers

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
User avatar
Shield
Posts: 57
Joined: January 23rd, 2011, 5:58 pm

Unexplained Sudden Defeat when Moving a Unit

Post by Shield » November 10th, 2019, 5:21 am

I am trying to code a scenario for a campaign-in-progress (will create a topic for it once it's complete enough, and this topic can be moved if necessary), but have already run into a mysterious problem.

I move any of my units, then suddenly "You Have Been Defeated!" pops up and every unit disappears off the map. No error message, no explanation. I do intend to kill every player-controlled unit that's not in the recall list by the end of the scenario, but I hadn't even tried adding that particular piece of code yet!
scenario_1.cfg
the problematic scenario in question
(7.33 KiB) Downloaded 42 times
I'm trying a lot of things at once, so I'm not sure what's causing the defeat. Everything seems to be working perfectly until I move a unit.

What's supposed to happen is you run past the undead, stuff all your Skirmishers into the cave (in actuality, the recall list), and then once at least five Skirmishers are inside you send your leader after them, and the scenario ends. Anybody know what's going wrong?
Creator of Lonely Era.
Here's my art!
I'm also on Tumblr.

User avatar
Pentarctagon
Forum Administrator
Posts: 4091
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Unexplained Sudden Defeat when Moving a Unit

Post by Pentarctagon » November 10th, 2019, 5:30 am

The x and y should be part of the [filter], I believe, not directly under the [event].
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code

User avatar
beetlenaut
Developer
Posts: 2391
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: Unexplained Sudden Defeat when Moving a Unit

Post by beetlenaut » November 10th, 2019, 7:39 am

Shield wrote:
November 10th, 2019, 5:21 am
I'm trying a lot of things at once, so I'm not sure what's causing the defeat.
You shouldn't do this. You should program one thing at a time and test as often as possible. That way, if a problem pops up, you know where to look for it. I would temporarily remove all the events but one, and work on that one until you know it works correctly.

Pentarctagon is right about x and y. They are being ignored where they are, which is why every move triggers the events. There are coordinates you can use directly in the event, but those are x1 and y1.

I'm pretty sure you are getting defeated because of a problem with [put_to_recall_list]. The wiki says to use the standard unit filter tags and keys, but it doesn't say to wrap them in a [filter] tag. Since you did, that part is being ignored as well, and every unit is being put to the recall list--including your leader. By default, having no leader causes an automatic defeat. (There are other problems with the logic that I noticed, but without the automatic defeat, you should be able to find them.)
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide

User avatar
Ravana
Forum Moderator
Posts: 2261
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Unexplained Sudden Defeat when Moving a Unit

Post by Ravana » November 10th, 2019, 11:11 am

I have proposed accepting that extra [filter] https://github.com/wesnoth/wesnoth/issues/4316

User avatar
Shield
Posts: 57
Joined: January 23rd, 2011, 5:58 pm

Re: Unexplained Sudden Defeat when Moving a Unit

Post by Shield » November 10th, 2019, 11:00 pm

Pentarctagon wrote:
November 10th, 2019, 5:30 am
The x and y should be part of the [filter], I believe, not directly under the [event].
Wow, that's not what I was guessing the problem was at all!
beetlenaut wrote:
November 10th, 2019, 7:39 am
Shield wrote:
November 10th, 2019, 5:21 am
I'm trying a lot of things at once, so I'm not sure what's causing the defeat.
You shouldn't do this. You should program one thing at a time and test as often as possible. That way, if a problem pops up, you know where to look for it. I would temporarily remove all the events but one, and work on that one until you know it works correctly.
Good advice, and advice I really should follow more often (sometimes I get on a roll typing in code and try to do everything at once). It's what I was going to do, if I couldn't puzzle it out on my own soon.
I'm pretty sure you are getting defeated because of a problem with [put_to_recall_list]. The wiki says to use the standard unit filter tags and keys, but it doesn't say to wrap them in a [filter] tag. Since you did, that part is being ignored as well, and every unit is being put to the recall list--including your leader. By default, having no leader causes an automatic defeat. (There are other problems with the logic that I noticed, but without the automatic defeat, you should be able to find them.)
Ha, that's kind of a funny one. Wouldn't have expected it. [put_to_recall_list] isn't a line I've used before, and it's cool that it can function like [filter] does in cases like this. Ravana's proposition would still be a good idea to accept though, imo; using both [put_to_recall_list] and [filter] together looks less streamlined, but allowing it could prevent confusion for people running into problems similar to this one. :hmm:

In any case, this problem is solved! Thanks for helping out.

...but as long as I'm here, I have another one, if it's not too much trouble. It's kind of an opposite problem. You're supposed to lose the level if you enter the cave before 5 Skirmishers do, but no matter how I've shuffled the code around, it only ever counts moving the leader to the cave as a victory.

One try at it. Tried making it two events.

Code: Select all

# moving Bud into the Cave
	[event]
		name=moveto
		[filter]
			x=50
			y=4
			id=Bud
		[/filter]
		[if]
			[variable]
				name=caved_saurians
				less_than=5
			[/variable]
			[then]
				[endlevel]
					result=defeat
				[/endlevel]
			[/then]
		[/if]
	[/event]
	[event]
		name=moveto
		[filter]
			x=50
			y=4
			id=Bud
		[/filter]
		[if]
			[variable]
				name=caved_saurians
				greater_than_equal_to=5
			[/variable]
			[then]
				[endlevel]
					result=victory
					bonus=no
				[/endlevel]
				[clear_variable]
					name=caved_saurians
				[/clear_variable]
			[/then]
		[/if]
	[/event]
My latest attempt, using an elseif.

Code: Select all

# moving Bud into the Cave
    [event]
        name=moveto
            [filter]
                x=50
                y=4
                id=Bud
            [/filter]
            [if]
                [variable]
                    name=caved_saurians
                    less_than=5
                [/variable]
                [then]
                    [endlevel]
                        result=defeat
                    [/endlevel]
                [/then]
			[elseif]
				[variable]
					name=caved_saurians
					greater_than_equal_to=5
				[/variable]
				[then]
					[endlevel]
						result=victory
						bonus=no
					[/endlevel]
					[/then]
			[/elseif]
		[/if]
	[/event]
(very sorry if it's at all difficult to read; the indents don't seem to be displaying and I can't remember how to put code into neat scroll-able boxes. I cut out the dialogue to save some space for clarity's sake)

The player should lose the scenario if the variable is less than 5, and yet they don't. What can be done to fix this?
Last edited by Ravana on November 10th, 2019, 11:49 pm, edited 1 time in total.
Reason: [code]
Creator of Lonely Era.
Here's my art!
I'm also on Tumblr.

User avatar
Adamant14
Posts: 897
Joined: April 24th, 2010, 1:14 pm

Re: Unexplained Sudden Defeat when Moving a Unit

Post by Adamant14 » November 11th, 2019, 4:13 am

Your events need a first_time_only=no
Author of Antar, Son of Rheor ( SP Campaign) | Development Thread + Feedback Thread + Replays of ASoR

User avatar
beetlenaut
Developer
Posts: 2391
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: Unexplained Sudden Defeat when Moving a Unit

Post by beetlenaut » November 11th, 2019, 10:49 am

Actually, both of these look good to me as they are (and the code could be shortened even further). I suspect that the problem is that caved_saurians doesn't equal what you think it does. Did you verify that? If not, check it by using :inspect or by having the game print the value in a message right before the [if] statement.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide

User avatar
Shield
Posts: 57
Joined: January 23rd, 2011, 5:58 pm

Re: Unexplained Sudden Defeat when Moving a Unit

Post by Shield » November 12th, 2019, 9:00 pm

You know what? I think I figured out what's causing it, though I still don't know how to fix it.

After what was causing the unexplained defeat, I eventually started getting a sneaking suspicion about the movements of my units. So I tried having the variable of cave-entering saurians displayed on screen and...
Screenshot (290).png
...it wasn't my leader's movement causing the problem, it was the event adding up the variable! It's putting the skirmishers into the recall list just fine, but it's counting every unit's movement to the total variable count. I've surpassed the minimum victory requirement's quantity before I've even ended my first turn!

Here's the code for that particular event:

Code: Select all

# put cave-entering Skirmishers into recall
	[event]
		name=moveto
		first_time_only=no
		[put_to_recall_list]
			x=50
			y=4
			[not]
				id=Bud
			[/not]
			[and]
				type=Saurian Skirmisher,Saurian Ambusher,Saurian Flanker
			[/and]
			heal=yes
		[/put_to_recall_list]
		[set_variable]
			name=caved_saurians
			add=1
		[/set_variable]
	[/event]
Could this be fixed and functional by putting the [set_variable] box somewhere inside the [put_to_recall_list] one? Or does [put_to_recall_list] not work like that and would ignore it?
Creator of Lonely Era.
Here's my art!
I'm also on Tumblr.

User avatar
Ravana
Forum Moderator
Posts: 2261
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Unexplained Sudden Defeat when Moving a Unit

Post by Ravana » November 12th, 2019, 9:05 pm

You set variable regardless of what moves where. Should use filter or conditional.

User avatar
lhybrideur
Posts: 75
Joined: July 9th, 2019, 1:46 pm

Re: Unexplained Sudden Defeat when Moving a Unit

Post by lhybrideur » November 13th, 2019, 1:00 pm

I would use a filter before the put_to_recall_list. Something like that :

Code: Select all

[event]
    name=moveto
    first_time_only=no
    [filter]
        side=1
        x=54
        y=4
        type=Saurian Skirmisher,Saurian Ambusher,Saurian Flanker
        [not]
            id=Bud
        [/not]
    [/filter]
    [put_to_recall_list]
        x=$x1
        y=$y1
        heal=yes
    [/put_to_recall_list]
    [set_variable]
        name=caved_saurians
        add=1
    [/set_variable]
[/event]
I didn't check if this exact code work though

Post Reply