Clearing nested array

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
djinius
Newbie
Posts: 11
Joined: Fri Oct 23, 2020 7:42 pm
Contact:

Clearing nested array

#1 Post by djinius »

I'm trying to clear a two-dimensional array as all 0's.
I have tried this code, but this does not work.

Code: Select all

define unitmatrix = [
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
    
label clearunit:
    for i in range(0, 10):
        for j in range(0, 5):
            $ unitmatrix[j][i] = 0
    return
Only first row was cleared. :(
So, I have changed the code as;

Code: Select all

label clearunit:
    $ unitmatrix = [
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
    return
and it worked.

Please let me know why first code did not work.

Mutive
Veteran
Posts: 344
Joined: Tue Nov 21, 2017 2:23 am
Completed: Eidolon, Minion!, Love Furever, Epilogue
Contact:

Re: Clearing nested array

#2 Post by Mutive »

I wonder whether writing is the best place for this post or whether you might be more likely to get a helpful response in Ren'Py questions and announcements.
Enjoy Eidolon, my free to play game at: https://mutive.itch.io/eidolon, Minion! at: https://mutive.itch.io/minion or Epilogue at: https://mutive.itch.io/epilogue

djinius
Newbie
Posts: 11
Joined: Fri Oct 23, 2020 7:42 pm
Contact:

Re: Clearing nested array

#3 Post by djinius »

Oh, I got it. Thank you for your direction.
This post will be deleted in an hour.

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Clearing nested array

#4 Post by Alex »

djinius wrote: Tue Nov 17, 2020 9:35 am ... Please let me know why first code did not work.
Try it like

Code: Select all

label clearunit:
    python:
        for i in range(0, 10):
            for j in range(0, 5):
                unitmatrix[j][i] = 0
                renpy.say(None, "[unitmatrix]") # will show you the progress
    return
https://www.renpy.org/doc/html/python.html#
https://www.renpy.org/doc/html/statemen ... lents.html

djinius
Newbie
Posts: 11
Joined: Fri Oct 23, 2020 7:42 pm
Contact:

Re: Clearing nested array

#5 Post by djinius »

Alex wrote: Tue Nov 17, 2020 5:23 pm
djinius wrote: Tue Nov 17, 2020 9:35 am ... Please let me know why first code did not work.
Try it like

Code: Select all

label clearunit:
    python:
        for i in range(0, 10):
            for j in range(0, 5):
                unitmatrix[j][i] = 0
                renpy.say(None, "[unitmatrix]") # will show you the progress
    return
https://www.renpy.org/doc/html/python.html#
https://www.renpy.org/doc/html/statemen ... lents.html
Wow! Thank you! Your code did work.
I have posted same question on 'Ren'Py Questions and Announcements'. I'll copy your answer there and delete this post.
I appreciate for your advice.

verysunshine
Veteran
Posts: 339
Joined: Wed Sep 24, 2014 5:03 pm
Organization: Wild Rose Interactive
Contact:

Re: Clearing nested array

#6 Post by verysunshine »

Mutive wrote: Tue Nov 17, 2020 12:08 pm I wonder whether writing is the best place for this post or whether you might be more likely to get a helpful response in Ren'Py questions and announcements.
You can flag threads to be moved by clicking on the exclamation point.

Build the basics first, then add all the fun bits.

Please check out my games on my itch.io page!

Post Reply

Who is online

Users browsing this forum: Bing [Bot]