Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved]Why Isn't This Array Changing!?

Okay, someone PLEASE help before I rip out all my hair! T_T

This is going to be difficult to explain because I'm working on a system, but I'm going to try:

What I'm writing is a quest log system for my own use. (Yes, I know there are others out there, but I'm doing a simpler one). For testing purposes, I have a data structure set up that has 2 titles and descriptions already defined (elements 0 and 1 of their respective arrays.) Now, what this window is supposed to do is list the titles of the quests whose matching element in the array 'available' equal 1.

Code:
 

class Window_InProgress < Window_Selectable

  attr_accessor :list

  attr_accessor :data

  

  def initialize(qtitles)

    super(100, 80, 300, 400)

    self.contents = Bitmap.new(width - 32, height - 32)

    self.opacity = 0

    @quests = $data_quests

    @titles = @quests.title

    @description = @quests.description

    @available = @quests.available

    @list = []

    @data = []

    @column_max = 1

    refresh

    self.index = 0

  end

  

  def refresh

    self.contents.clear

    self.contents.font.color = Color.new(0,0,0,255)

    @list[0] = @titles[0]

    @data[0] = @description[0]

    for i in [email=1..@titles.size]1..@titles.size[/email] - 1

      if @available[i] == 1

        @list.push(@titles[i])

        @data.push(@description[i])

      end

    end

    @item_max = @list.size

    for i in 0..@item_max

      if @list[i] != nil

        draw_quest_title(i)

      end

    end

    return

  end

  

  def draw_quest_title(index)

    self.contents.draw_text(0, (index)*32, 500, 32, @list[index])

  end

end

 

However, it's only printing the first title and not the second, even though elements 0 and 1 of the 'available' array both equal 1.

Here's my data structure:

Code:
 

class Data_Quest

  attr_accessor :title

  attr_accessor :description

  attr_accessor :available

  

  def initialize

    @title = []

    @description = []

    @available = []

    #No Quest

    @title[0] = 'Nothing'

    @description[0] = ["Nothing to do at the moment..."]

    @available[0] = 1

    #Quest 1

    @title[1] = 'Wtf...?'

    @description[1] = ["Okay, so I'm supposed to take notes in here...",

                        "Let's see... I don't know where I am...",

                        "I can't find the others....",

                        "And there's some weird mage dude that knows",

                        "way more than he should....",

                        "I guess I should just get to work on finding",

                        "everyone else for now..."]

    

    for i in [email=1..@title.size]1..@title.size[/email] - 1

      @available[i] = 0

    end

  end

  

  def get_quest(quest)

    @available[quest] = 1

  end

  

  def complete(quest)

    @available[quest] = 0

    $data_quests2.complete(quest)

  end

end

 

class Game_System

  alias quest_log update

  def update

    quest_log

    $data_quests = Data_Quest.new

  end

end

 

Now, I know all the values of 'available' are set to 0, but during runtime, the method get_quest is called for element 1, so that data is changed during the game but doesn't seem to effect the @list array in Window_InProgress.

So... It's as if it's not performing the 'push' method even though element 1 should be added....

Does this make sense, or should I just put my entire system up here? Sorry it's not commented, btw...?
 
Have you tried changing line 26 of the first script to the following?

Code:
for i in 0...@titles.size

It appears that with the current setup, it only searches @available[1], because the loop starts at 1 and ends at 1. As a general rule, you set up a for statement with a starting point of 0, especially when searching arrays.
 
I was doing that for a while, but what I realized was happening was they by using the 'push' method, the 0 element of @list remained nil and the rest were just added on, so it didn't match up properly. This way, I'm declaring the 0 value and then adding the others on, and to get it to match up, I'm starting at 1. However, the value of @titles[1] isn't appearing, only the value of @titles[0].
 
PhoenixTilea":194s27yb said:
I was doing that for a while, but what I realized was happening was they by using the 'push' method, the 0 element of @list remained nil and the rest were just added on, so it didn't match up properly. This way, I'm declaring the 0 value and then adding the others on, and to get it to match up, I'm starting at 1. However, the value of @titles[1] isn't appearing, only the value of @titles[0].

I rechecked it, and I think I found your problem. You're setting the variable you're checking in lines 24 and 25, and then, because you have set the available variable to 0, you don't get to set the next set of values to what you want. I assume this changes later, but, for now, you have to set them to one for testing purposes. Here's what I've got as an edit, but, since I changed the default values back to 0, I want you to try modifying the available variable to see if that works.

Code:
class Window_InProgress < Window_Selectable

  attr_accessor :list

  attr_accessor :data

 

  def initialize

    super(100, 80, 300, 400)

    self.contents = Bitmap.new(width - 32, height - 32)

    self.opacity = 255

    @quests = $data_quests

    @titles = @quests.title

    @description = @quests.description

    @available = @quests.available

    @list = []

    @data = []

    @column_max = 1

    refresh

    self.index = 0

  end

 

  def refresh

    self.contents.clear

    self.contents.font.color = Color.new(0,0,0,255)

    @list = []

    @data = []

    p @titles

    p @available

    for i in 0..@titles.size

      if @available[i] == 1

        @list.push(@titles[i])

        @data.push(@description[i])

      end

    end

    @item_max = @list.size

    p @list

    p @data

    for i in 0..@item_max

      if @list[i] != nil

        draw_quest_title(i)

      end

    end

    return

  end

 

  def draw_quest_title(index)

    self.contents.draw_text(0, (index)*32, 500, 32, @list[index])

  end

end

 

 

class Data_Quest

  attr_accessor :title

  attr_accessor :description

  attr_accessor :available

 

  def initialize

    @title = []

    @description = []

    @available = []

    #No Quest

    @title[0] = 'Nothing'

    @description[0] = ["Nothing to do at the moment..."]

    @available[0] = 1

    #Quest 1

    @title[1] = 'Wtf...?'

    @description[1] = ["Okay, so I'm supposed to take notes in here...",

                        "Let's see... I don't know where I am...",

                        "I can't find the others....",

                        "And there's some weird mage dude that knows",

                        "way more than he should....",

                        "I guess I should just get to work on finding",

                        "everyone else for now..."]

    for i in 0..@title.size

      @available[i] = 1

    end

  end

 

  def get_quest(quest)

    @available[quest] = 1

  end

 

  def complete(quest)

    @available[quest] = 0

    $data_quests2.complete(quest)

  end

end

 

class Game_System

  alias quest_log initialize

  def initialize

    $data_quests = Data_Quest.new

    quest_log

  end

end

 

begin

$data_quests = Data_Quest.new

end
 
Uhm... It appears to KIND OF work... only before it'll let me into the quest log, a bunch of windows come up printing some of the array information, and not game windows either. Operating system windows.
 
PhoenixTilea":32w7bpgj said:
Uhm... It appears to KIND OF work... only before it'll let me into the quest log, a bunch of windows come up printing some of the array information, and not game windows either. Operating system windows.

Sorry. I forgot to remove the print commands after testing it. I was using those to test the values of the various arrays, so I knew it would work. Here, I'll give you one without those.

Code:
class Window_InProgress < Window_Selectable

  attr_accessor :list

  attr_accessor :data

  

  def initialize

    super(100, 80, 300, 400)

    self.contents = Bitmap.new(width - 32, height - 32)

    self.opacity = 255

    @quests = $data_quests

    @titles = @quests.title

    @description = @quests.description

    @available = @quests.available

    p @available

    @list = []

    @data = []

    @column_max = 1

    refresh

    self.index = 0

  end

 

  def refresh

    self.contents.clear

    self.contents.font.color = Color.new(0,0,0,255)

    @list = []

    @data = []

    for i in 0..@titles.size

      if @available[i] == 1

        @list.push(@titles[i])

        @data.push(@description[i])

      end

    end

    @item_max = @list.size

    for i in 0..@item_max

      if @list[i] != nil

        draw_quest_title(i)

      end

    end

    return

  end

 

  def draw_quest_title(index)

    self.contents.draw_text(0, (index)*32, 500, 32, @list[index])

  end

end

 

class Data_Quest

  attr_accessor :title

  attr_accessor :description

  attr_accessor :available

 

  def initialize

    @title = []

    @description = []

    @available = []

    #No Quest

    @title[0] = 'Nothing'

    @description[0] = ["Nothing to do at the moment..."]

    @available[0] = 1

    #Quest 1

    @title[1] = 'Wtf...?'

    @description[1] = ["Okay, so I'm supposed to take notes in here...",

                        "Let's see... I don't know where I am...",

                        "I can't find the others....",

                        "And there's some weird mage dude that knows",

                        "way more than he should....",

                        "I guess I should just get to work on finding",

                        "everyone else for now..."]

    for i in 1..@title.size - 1

      @available[i] = 0

    end

  end

 

  def get_quest(quest)

    @available[quest] = 1

  end

 

  def complete(quest)

    @available[quest] = 0

    $data_quests2.complete(quest)

  end

end

 

begin

$data_quests = Data_Quest.new

end

Just so you know, using p "something" or print "something" will make a window pop up with whatever you told it to print, be it a variable or string. This is very useful for testing various aspects of a script, and can be used in a variety of situations. In this case, I used it to see whether the arrays had been filled with the values you wanted them to have, but I often use them for other functions, such as testing whether or not a broken script gets to a specific point, to find the cut off that it stops at, or to tell me various other bits of information. They can also be used to form error messages, provided you add the line exit right after the print command.
 
Well, it appears to be working now! Thanks for the help, and the tip. I was setting game variables equal to arrays to check them, and that wasn't too efficient. LOL

Boy, have you been my savior so far, Glitch. ^_^ I'll probably end up putting you down as one of the main programmers for the project. LOL
 
PhoenixTilea":kij7a9pu said:
Well, it appears to be working now! Thanks for the help, and the tip. I was setting game variables equal to arrays to check them, and that wasn't too efficient. LOL

Boy, have you been my savior so far, Glitch. ^_^ I'll probably end up putting you down as one of the main programmers for the project. LOL

I'm glad I could help. I hope that you can continue learning to script, as that gives you the ability to do so much more with your project.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top