how to work with array and random numbers
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.
how to work with array and random numbers
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
thanks
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]
Re: how to work with array and random numbers
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].
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].
Re: how to work with array and random numbers
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.Also.ypur code it a little too complicated it could just call wesnoth.random(#level) instead of that set_variable stuff.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
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.
- Celtic_Minstrel
- Developer
- Posts: 2166
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: how to work with array and random numbers
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]