write array to file

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
csarmi
Posts: 288
Joined: August 13th, 2007, 1:57 pm

write array to file

Post by csarmi »

Sapient wrote:I imagine something like the Fisher-Yates shuffle would be easy enough to implement in WML:

Code: Select all

#define SHUFFLE_ARRAY ARR
{VARIABLE n ${ARR}.length}
[while]
 [variable]
  name=n
  greater_than=1
 [/variable]
 [do]
  {VARIABLE_OP n add "-1"}
  {VARIABLE_OP k rand "0..$n"}
  [set_variables]
   name=temp
   to_variable={ARR}[$k]
  [/set_variables]
  [set_variables]
   name={ARR}[$k]
   to_variable={ARR}[$n]
  [/set_variables]
  [set_variables]
   name={ARR}[$n]
   to_variable=temp
  [/set_variables]
 [/do]
[/while]
#enddef
disclaimer: untested
I've got a simple question. Suppose I create an array, and shuffle it.
I'd like to write out the results (what ended up where) in some easy-to-read format when the game is running. Is it possible to write it out into some logfile, savefile, plain text file or smth like that?

If its only possible to write it out into the save/replay file, how to store them so that it'd be easy to export?
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: write array to file

Post by Sapient »

1. [set_variable][join] can be used to join array contents into a single string

2. [wml_message] can be used to write console output from WML
(however, on recent windows builds it will go to stderr.txt or stdout.txt)

3. console output can be redirected to a file when starting Wesnoth

There are a couple of alternatives to consider:

A. use :inspect or [inspect] to inspect variable contents

B. use persistent variables (http://wiki.wesnoth.org/PersistenceWML)
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
csarmi
Posts: 288
Joined: August 13th, 2007, 1:57 pm

Re: write array to file

Post by csarmi »

The point is to input a player list and sort them into groups then have that list in a readable format so that a wesnoth noob can read it even.

Thanks :)
Post Reply