how to work with array and random numbers

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
ultimatux
Posts: 15
Joined: May 29th, 2012, 4:02 am

how to work with array and random numbers

Post by ultimatux »

Basically, what I would need would be an example code to guide me with this, since although I understand programming, I am still a newbie in wml, I would like to create an array indexing different types of troops to then be able to add them as an enemy leader, troops to recruit, etc ... it should be clarified that the number of each index should be generated if randomized ... for example

Code: Select all

ArrayTroops [0 fighter elf, 1 loyal archer, 2 scout elf]

i = random number (0..2)

[side]
side = 2
controller = ai
team_name = "bad"
id = "EnemyLeade name = _" Villain "

type = "ArrayTrrops [i]"

[/ side]
thanks
User avatar
Ravana
Forum Moderator
Posts: 2952
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: how to work with array and random numbers

Post by Ravana »

If you understand programming, it might be easier to do the programming logic in Lua instead.

Whether the logic is done in Lua or WML, [side] does not support variable evaluation. Changing such values would be done with [modify_side]/[modify_unit]/[allow_recruit]/[disallow_recruit]/... during prestart event.

Lua example creating 1d array (table) and selecting random unit from it https://github.com/ProditorMagnus/Oroci ... t_list.cfg called as https://github.com/ProditorMagnus/Oroci ... ves.cfg#L2

In WML you would create array with [set_variables] or [set_variable].
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: how to work with array and random numbers

Post by gfgtdf »

Ravana wrote: September 22nd, 2019, 9:47 am
Whether the logic is done in Lua or WML, [side] does not support variable evaluation.
This is not 100% true since 1.14, with scenario_genreration=lua you can actually generate the whole wml code for the scenario with lua code, ( I do that in the 1.15 version of world conquest II), although I agree that that'd not be the easiest solution here.
Ravana wrote: September 22nd, 2019, 9:47 am Lua example creating 1d array (table) and selecting random unit from it https://github.com/ProditorMagnus/Oroci ... t_list.cfg
Also.ypur code it a little too complicated it could just call wesnoth.random(#level) instead of that set_variable stuff.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: how to work with array and random numbers

Post by Celtic_Minstrel »

Agreed with Ravana that it might be easier for you to do this in Lua. You can embed the Lua into the prestart event quite easily:

Code: Select all

[event]
	name=prestart
	[lua]
		code=<<
			local args = ...
			-- Lua code here, contents of [args] can now be accessed in the args variable
		>>
		[args]
			# WML data to pass to the Lua code (optional)
		[/args]
	[/lua]
[/event]
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply