Random Number of Traits

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
jeeree
Posts: 12
Joined: July 22nd, 2014, 2:42 am

Random Number of Traits

Post by jeeree »

Hi,

Is there a way to randomize the number of traits a race can have? I want the units built by a certain race have different numbers of traits. so some units may have only 1 while some may have 3. I tried num_traits={RANDOM 1..3} but it doesn't seem to work. Is it possible? Or is it just using a different syntax?
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: Random Number of Traits

Post by zookeeper »

Not possible directly.

What you can easily do is to make the race get 3 traits, but then make a recruit event which does a {RANDOM 1..3} and removes the third trait if the result is 2, and removes both the third and second traits if the result is 1. You'd have to [fire_event] that event manually for every unit created by the scenario, though, which might or might not be possible in your case.
jeeree
Posts: 12
Joined: July 22nd, 2014, 2:42 am

Re: Random Number of Traits

Post by jeeree »

Thanks for the help. But how do I remove an arbitrary trait? There doesn't seem to be a command for removing just 1 or 2 traits and the trait's effects.
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: Random Number of Traits

Post by zookeeper »

Store the unit, then [clear_variable] with name=storedunitvar.modifications.trait[2] to remove the second trait, then unstore. The changes to for example the unit's max moves might not get reflected in the unit immediately, so you might also need to clear whatever properties of the unit the traits might affect (moves, experience, hitpoints, attacks...) so that they'll get recalculated on unstore, or you might be able to do all of that automatically by using [transform_unit] after unstoring. I've never tried the latter myself, but it looks like it should do the job.
User avatar
ChaosRider
Posts: 1184
Joined: April 15th, 2012, 1:15 pm

Re: Random Number of Traits

Post by ChaosRider »

Isnt it better to use recruit event, and for just recruited unit randomly choose from 1 to 3 and then in switch set effects ?
Creator of WOTG (+5500 units), MWC (+615 units), SurvivorsArea, RandomColosseum, RC WOTG, RC MWC, ColosseumRandomClonesBattle, BetweenDarknessAndLight, StealingWeapons, MoreUnitsForms, MoreDamageTypes, CanBeOnlyOne, ColosseumOneWinner, BonusSpam, CriticalStrike - available at 1.18 Wesnoth server.
jeeree
Posts: 12
Joined: July 22nd, 2014, 2:42 am

Re: Random Number of Traits

Post by jeeree »

I was thinking about using prerecruit event, which hopefully generated the unit without applying any of the stat changes. but there doesn't seem to be an easy way to randomly assign traits to a new unit. If I can assign a random trait to any unit recruited, I can set the num_traits=0, and use the {RANDOM 0..3} to add a random number (0-3) of traits to the new unit. This will also avoid the issue of changing stats already made to the unit by existing traits. So does anyone know how to assign random traits to a newly created unit?
Wussel
Posts: 624
Joined: July 28th, 2012, 5:58 am

Re: Random Number of Traits

Post by Wussel »

Maybe if you use versions like the zombie/undead you could manage more easily different amount of traits. Nice idea anyway. There will be no real easy way since you breaking with the general logic of the game. Good luck.
User avatar
beetlenaut
Developer
Posts: 2867
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: Random Number of Traits

Post by beetlenaut »

jeeree wrote:So does anyone know how to assign random traits to a newly created unit?
You can modify the traits of a stored unit like zookeeper said (though traits are numbered starting at [0], so trait[2] would be the third one). Pick a random number between 1 and 4 (or however many traits you are using). Use a [switch] statement to check the variable, and add {TRAIT_STRONG} for 1, {TRAIT_QUICK} for 2, and so on. The second trait is the same, but this time you need to keep picking random numbers with [while] until you get a different one than you got the first time.

If you have problems while you are working on this, you can use the debug command :inspect to check the unit's variables and see what's happening.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
jeeree
Posts: 12
Joined: July 22nd, 2014, 2:42 am

Re: Random Number of Traits

Post by jeeree »

Thanks guys. I finally figured it out. I had to put all the traits into an array and use modify unit to assign traits to each unit created. I could never figure out the debug mode for this game though.
User avatar
beetlenaut
Developer
Posts: 2867
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: Random Number of Traits

Post by beetlenaut »

For the debug mode, hit a colon (:) to turn on the BfW command line, then type "debug". Type "help" for a list of commands. The command line is useful for other things as well, so give it a try.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
Post Reply