I've created an ascii-art game that uses a two-dimensional array to keep track of the map, which refreshes each time the character moves. The game is also multiplayer, using io::socket::inet sockets to recieve the opponent's location and refresh the map when a change is made. To achieve this, I used threads so that both the keyboard and the sockets would wait for input simultaneously.

My problem is this: when the thread is created, the 2D array storing the map on the main level:

#@level is an array of strings; each string is a row of the map. # This breaks the entire thing into a 2D array where each element is a + character on the map. my @tempArray; my @twoDee; for(my $m = 0; $m <= $#level; $m++) { @tempArray = split(//, $level&#91;$m&#93;); push @twoDee, &#91;@tempArray&#93;; }


is duplicated and a copy is sent to the thread governing socket input. Thus, each time the thread calls the function I've created for redrawing the map, it sends it uses its own copy of the 2D array instead of the one on the main level. As it stands, I have set it up so that both copies of the map are updated identically using shared variables that contain the X and Y coordinates of the player and his / her opponent. The problem is, should I attempt to add other features to the game, like, say, more opponents, I will have to create yet another thread and track everyone's movements yet again.

I have read the man pages from threads::shared many times, but can't find / understand a means of sharing a 2D array, which would make my life much simpler. I've poked and prodded, but usually get an error message about inappropriate scalar values.

I'd appreciate any help anyone could offer -- also, let me know if you need to see more code blocks and I'll paste them here.

Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips


In reply to Using threads::shared with multidimensional arrays by fridayjones85

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.