Inadvertently creating normal variables

The place to post your WML questions and answers.

Moderator: Forum Moderators

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
zaimoni
Posts: 281
Joined: January 27th, 2005, 7:00 am
Location: Linn Valley, KS U.S.A.
Contact:

Inadvertently creating normal variables

Post by zaimoni »

Trying to create array variable elements directly creates syntax-invalid normal variables instead. Is there a clean way to create array variables without recourse to a filter? [HTTT and UTBS always use filters to create array variables, but that isn't appropriate for this context.]
-----
I'm taking a pass at bringing up Mystery Campaign on Wesnoth 1.1.2a. The savefiles are broken. When loading, I get messages like these:

Code: Select all

error general: Warning: The file you have tried to load is corrupt. Loading anyway.
Unexpected characters after variable name (expected , or =) at <unknown>:460
Unexpected characters after variable name (expected , or =) at <unknown>:461
Unexpected characters after variable name (expected , or =) at <unknown>:462
Unexpected characters after variable name (expected , or =) at <unknown>:463
Unexpected characters after variable name (expected , or =) at <unknown>:465
Unexpected characters after variable name (expected , or =) at <unknown>:466
Which correspond to these lines in the plaintext savefile:

Code: Select all

		side_faction[1]="dwarves"
		side_faction[2]="drakes"
		side_leader[1]=_ "Dwarvish Runemaster"
		side_leader[2]=_ "Drake Flare"
		side_number="1"
		side_recruits[1]="Drake Glider,Drake Burner,Saurian Skirmisher,Dwarvish Fighter,Dwarvish Thunderer,Dwarvish Guardsman,Gryphon Rider,Dwarvish Ulfserker"
		side_recruits[2]="Drake Glider,Drake Burner,Saurian Skirmisher"
(Yes, this is invalid WML in the savefile. No hand-editing required.) This is after immediately modifying the scenario start to try to clean the side_leader and side_recruits array variables:

Code: Select all

		{CLEAR_VARIABLE side_leader}
		{CLEAR_VARIABLE side_recruits}
Less than surprisingly, trying to clear the individual array elements in the following way doesn't dispose of these name-invalid variables either:

Code: Select all

		{CLEAR_VARIABLE side_leader[1]}
		{CLEAR_VARIABLE side_leader[2]}
		{CLEAR_VARIABLE side_recruits[1]}
		{CLEAR_VARIABLE side_recruits[2]}
So (since the code is intended to be extended to more than two sides): is there a clean way to initialize array variable elements without a filter? This kind of code is giving the syntax-invalid side_leader[1] and side_leader[2]:

Code: Select all

#define FACTION_LEADER TYPE WESTYPE
	[if]
	[have_unit]
	side=$side
	[/have_unit]
	[then]
	[/then]
	[else]
		[unit]
		side=$side
		canrecruit=1
		type={TYPE}
		x=$location.x
		y=$location.y
		[/unit]
		[set_variable]
		name=side_leader[$side]
		value={WESTYPE}
		[/set_variable]
		{DEBUGMSG (Side $side is under a {TYPE})}
	[/else]
	[/if]
#enddef
toms
Posts: 1717
Joined: November 6th, 2005, 2:15 pm

Post by toms »

Maybe does this...otherwise it should not hurt.

Code: Select all

#define FACTION_LEADER TYPE WESTYPE 
   [if] 
   [have_unit] 
   side=$side 
   [/have_unit] 
   [else]    # << [then][/then] is not needed
      [unit] 
      side=$side 
      canrecruit=1 
      type={TYPE} 
      x=$location.x 
      y=$location.y 
      [/unit] 
      {VARIABLE_OP westype format side_leader[$side]}
      {VARIABLE $westype ({WESTYPE})} 
 # I advice you to use the macro to initialize variables, it avoids bugs
      {DEBUGMSG (Side $side is under a {TYPE})} 
   [/else] 
   [/if] 
#enddef
First read, then think. Read again, think again. And then post!
zaimoni
Posts: 281
Joined: January 27th, 2005, 7:00 am
Location: Linn Valley, KS U.S.A.
Contact:

Post by zaimoni »

Thank you for your time.

Yes, more legible source is always good. I have put in macros everywhere they don't break the campaign. [FACTION_RECRUIT causes crashes when the inlined set_variable tags are replaced with macros. Probably something to do with grouping macro parameters. It's not worth adjusting for.]

However, it's not helping the savefile. This

Code: Select all

#define FACTION_LEADER TYPE WESTYPE
	[if]
	[have_unit]
	side=$side
	[/have_unit]
	[else]
		[unit]
		side=$side
		canrecruit=1
		type={TYPE}
		x=$location.x
		y=$location.y
		[/unit]
#	having problems with faux array variables, so bypass
#	should initialize side_leader[$side]
		{VARIABLE side_leader[$side] {WESTYPE}}
		[if]
		[variable]
		name=side
		equals=1
		[/variable]
		[then]
		{VARIABLE player_leader {WESTYPE}}
		[/then]
		[/if]
		{DEBUGMSG (Side $side is under a {TYPE})}
	[/else]
	[/if]
#enddef
puts invalid variable names like this into the savefile.

Code: Select all

		side_leader[1]=_ "Bandit"
		side_leader[2]=_ "Drake Warrior"
Commenting out the {VARIABLE side_leader[$side] {WESTYPE}} macro simply errors out with the following, crashing Wesnoth 1.1.2a afterwards.

Code: Select all

Unexpected characters at line start at D:/Program\ Files/Wesnoth/userdata/data/campaigns/Mystery_Campaign/utils/factions.cfg:111 included from D:/Program\ Files/Wesnoth/userdata/data/campaigns//Mystery_Campaign.cfg:64
....
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Post by zookeeper »

My guess would be that you just simply can't create a WML array manually like that - it has to be produced via something else (like storing units or locations) first. This should be changed, so it would be a good idea to write a bug report / feature request about it.
zaimoni
Posts: 281
Joined: January 27th, 2005, 7:00 am
Location: Linn Valley, KS U.S.A.
Contact:

Post by zaimoni »

Hmm...to me, it's more of a bug that an invalid variable name is being created. Without proper variable declaration, this can't be a syntax error. I wonder what the customary severity for this kind of bug as a runtime error is.

The format for arrays in the savefile doesn't suggest a clean way to random-access initialize them. [Default-initializing interpolated entries to empty can bloat things very quickly.]
Xan
Inactive Developer
Posts: 258
Joined: August 28th, 2005, 3:05 pm
Contact:

Post by Xan »

Code: Select all

      [set_variable] 
      name=side_leader[$side] 
      value={WESTYPE} 
      [/set_variable]
should be something like

Code: Select all

      [set_variable] 
      name=side_leader[$side].variable
      value={WESTYPE} 
      [/set_variable]
"It is time people learned about their failures and my successes."
zaimoni
Posts: 281
Joined: January 27th, 2005, 7:00 am
Location: Linn Valley, KS U.S.A.
Contact:

Post by zaimoni »

That works. Thank you.
Post Reply