As the title asks, I've been stomped on the for a while and so far, a lot of the tuts I have looked up haven't really steered me in the right direction.
Also I am scripting this inside of RPG Maker VX Ace.
With a specific system I am trying to create, I'm currently stuck using a mix of using events and scripting to try to get it work and be less confusing when driven by events.
Also please understand that I'm completely new to rgss scripting and have been trying to do a crash study course into scripting since two days ago. X_X
Lets say for example array 1 is like this:
$game_variables[24] = array = [tilled, crop_id, days, eventid = 12]
and array 2 is like this:
$game_variables[25] = array.new = [tilled, crop_id, days, eventid=13]
How would I do a search for what is called 'eventid' within the script?
I keep thinking either a do while loop or a while loop but I don't see how that would work. X_X
A point in the right direction would be a good start for me and for the love of RPGs please don't point me to a game tut download that has had too much traffic and has been shut down temporarily. X_X Ran into one already.
Also, long story short, I'm trying to create a farming system within my project that isn't so event heavy... and try to make it lighter using a set of script calls and event switches to do most of the work and so my eyes can read both easier.
Array Data Search VX ACE
● ARCHIVED · READ-ONLY
-
-
Ok I'll try to make a little sense for you.
It'd be easier if you showed a screen shot of your event and maybe the script, but I see a few things I'd like to try to clear up for you :)
$game_variables[24] = array = [tilled, crop_id, days, eventid = 12]is a bit confusing looking. What is says is you're setting the variable to array, and then set it to [tilled, crop, days, event_id]
You could simplify it a bit and just do
$game_variables[24] = [tilled, crop_id, days, 12]I condensed the last entry down as well, it'd work either way. You could use a comment line if you want so you can keep track of what all the entries are like so:
# game_variable[id] = [Tilled, Crop, Days, EventID]$game_variables[24] = [tilled, crop_id, days, 12]Now how you want to check the event id's will determine how you search for them. If you just want to check what the set even id is, you'd want to do this:
$game_variables[24][3]Arrays always count the number of items inside the array starting with 0. So tilled would be [0], crop would be 1, etc. So the above statement says to check game variable 24 for array item 3 (aka the event id).
Now if you wanted to check all your variables for their event id then you could do a loop type of method. First you'd want to set up the variables you want to check. Like your post says it's game variables 24 and 25. If you're always storing your information in game variables then you can set it up something like this:
ids = [24, 25] # Set all the game variables id's you're usingids.each do |id| # Go through the array of ids event_id = $game_variables[id][3] # Get the event ID for each variableend # end the search loop.now the above code will get the event_id each time, so you'd normally do some more code inside that search.. like when event_id is the one you're trying to match, then execute some more code.
Here's a couple of documents you can read about the arrays:
http://www.tutorialspoint.com/ruby/ruby_arrays.htm
http://www.ruby-doc.org/core-2.1.2/Array.html
They might take a few read throughs before it makes a whole lot of sense, but once you get the hang of arrays that last link will be a great help :) -
Below is what I've done so far... and it's a bit of a mess right now. Also to note, I do plan to add more variables later and from what I calculated, at least 132 variables alone just for the farming part.
class Game_Interpreter #sets the farm blocks up to intial states -- meaning EMPTY! def set_farm_up tilled = false #if land is tilled. crop_id = 0 #What crop id is planted. days = 0 #Days set for crop to mature #Stores the information the numbers is for the event id is in relation to the event. $game_variables[25] = [tilled, crop_id, days, 9] $game_variables[24] = [tilled, crop_id, days, 12] end def tool_check a = $game_variables[100] #Gets actor id/party leader #Check value of equipment to make sure it is not empty. #Then to check if the proper tool at the right place is being used. #To be figured out later. #Then take apporate action. if ($game_actors[a].equips[1] == nil) $game_variables[2] = 0 else $game_variables[2] = $game_actors[a].equips[1].id end end #Call below to see if land can be tilled. def hoe_till #First get plot being checked. #check via event id. e = $game_variables[23] #Event Id called in Event end #Updates crop events that have their crop vars not eqaul to zero. def update_plot end end For some of it, I couldn't think/find/learn of a better way to set them up. And my mind keeps making me thing that the language is like C++ needed a lot of the values to be given a value first (when I was learning) before you can use it. I still have a lot of it to rewrite as I have kept changing the code a lot when I hit my wall and experimenting with other events. -
It's a good start to the script :) Planning is always a good first step and trying to figure out all the things you'll need to get done.
you can change you tool check method to this:
if $game_party.leader.equips[1] # If party leader has tool $game_variables[2] = $game_party.leader.equips[1].id else $game_variables[2] = 0 endI know it'll take a while to learn all the already predefined stuff in the game :) So $game_party.leader is just that. It is always set to the current party leader so you can skip setting that variable up. Since it returns and actor and all the actor's info, then you can check their equips on the same line. Remember arrays will always start with 0. So when you're going through the equips array, weapons is 0, offhand/shields are 1. So just want to make double sure you have the right item you're looking for :)
Now we're doing all of this on one line and threw an if in front of it. If statements are kind of neat because you can check how one object compares to another object. or in the case of the code above, its just checking if the object exists. If the party leader has an item equiped in the 1 slot, then execute the code, else if the slot is empty then do the other code.
edit: Also might consider moving your code to another section of the scripts. Game_Interpreter won't save information (it just processes information). You could start your own class and store all your information there, but since you are starting out with the scripting, I'd suggest adding your code into one of the following existing classes:
Game_System - This class handles settings for the game like window color, current music playing, battle counts, etc
Game_Party - This has information about the party.. such has their inventory items, steps taken, gold collected, who is in the party, etc.
Game_Map - This one handles events on the map, the players location, any vehicles present, etc
Game_Player - This class mostly handles how the player appears on the map.
Any of the above would work, but your farming system will probably deal mostly with map objects (like the events). So if it were me, I'd probably add your code into the Game_Map section -
Yeah... I'm stuck with thinking how if loops work in C++ and in some other scripting languages I have messed around with in the past. C++ was with school and other languages I messed around with and tried to learn in my free time.
Anyways moving on...
I want to verify with you if this portion would work with the given variable and numbers. In my head, it makes sense... but I possible could shorten it.
:EDIT:
Didn't see your edit until a few minutes I had edited this post. I saw on a tut, that I had to have that there and currently, if I do finish this script, I would have no idea where to put it in the Game_Map section. I currently have it separate under the materials scripts section.
Also I would like to ask, where should I look if I wanted to update the specific event graphic, tut wise?
Code:#Call below to see if land can be tilled if it can return a value and till. def hoe_till #First get plot being checked. #check via event id. #ids = [9,12] varid = 24 until $game_variables[varid][3] == $game_variables[23] do #if ids don't match varid +1 to move to the next variable if ($game_variables[varid][3] != $game_variables[23] { varid += 1 } else #Once we find the right array #then we must till it by flipping this switch. if ($game_variables[varid][0] == false) $game_variables[varid][0] = true break end end end -
I've never really coded outside of rpg maker (unless you count html ages ago), so I can't really compare the two languages. I started to take a visual basic class in college many years ago but ended up moving very early into the semester so I didn't get a lot of it.
If I'm understanding your def, you want to check the event id and make sure it matches and then change the tilled value to true. The following should work:
def hoe_till(var_id, event_id) $game_variables[id][0] = true if $game_variables[id][3] == event_id endSo when you call the method, you'd have to set the var_id and even_id..so when it's called it'd look more like:
hoe_till(24, 9)So it'd set game_varaible[24] tilled status to true if game_variables[24]'s event id is 9
As for changing the event's picture, I'm sure there is probably a tutorial some where, but I couldn't say I've seen it. But this is the method that does it (found in Game_CharacterBase)
#-------------------------------------------------------------------------- # * Change Graphics # character_name : new character graphic filename # character_index : new character graphic index #-------------------------------------------------------------------------- def set_graphic(character_name, character_index) @tile_id = 0 @character_name = character_name @character_index = character_index @original_pattern = 1 endI plan on writing a farming script at some point in the future, however my plate is really full with scripts I'm working on right now. And I know TDS has one that is fuctional if not completed.
http://forums.rpgmakerweb.com/index.php?/topic/542-farm-engine/
It can be a little hard to follow as it's not a finished script. His demo does a lot of what you are wanting to do as well so you might be able to learn some from it -
Thanks, I'll take a look at that. And thank you for helping me out. :)
And yeah, what I'm trying to do is get the variable id, during the check at the event, with the event's id. -
I apologize for double posting at this point, but I felt it was better to do so like this with my next question here.
I've been re-writing my previous script to look a lot neater and be less reliant on the $game_variables at the moment. Also, after endlessly searching for a script call to get event Id, things became less messy for me, but in the end I'm still at the same problem as before.
How do I locate specific set of data from within array that's within an array? I think the for loop is my best method... but... here's how things look.
# $plot_name = [ event id, if tilled, crop id, days to maturity, watered?, matured? ]# Only CHANGE event id number to add/change what events it will refer to. #-------------------------------------------------------------------------------$farm_plots = [ farm_plot1 = [ 9, 0, 0, 0, 0, 0 ], farm_plot2 = [ 12, 0, 0, 0, 0, 0 ] ]#------------------------------------------------------------------------------- def nak_tool_check() #Data below stores all variable data for each farm plot. #Information needed. #Current event id being interacted with $farm_id = get_character(0).id #Check to see if there is a value in party leaders tool slot if $game_party.leader.equips[1] == nil farmtool = 0 $game_message.add("You do not have a tool or seeds equiped.") else farmtool = $game_party.leader.equips[1].id #Runs tilling method if farmtool == Nak_Tools::Tool_Hoe #Nak_Tools::Tool_Hoe defined in Crop Module nak_tills_plot() #elseif farmtool >= Nak_Tools::Tool_Seeds_Min && farmtool <= Nak_Tools::Tool_Seeds_Max #elseif farmtool == Nak_Tools::Tool_WaterCan else $game_message.add("You need to equip the hoe first!") end end end #Till land: Need map_id and Event_id stored maybe? def nak_tills_plot() endWhat I'm trying to figure out is how would I get the first value out of either farm_plot1 or farm_plot2 if they're a match to $farm_id.
So I'm asking, would a for loop be the best for this, or what other loop would I use? Again I'll say this, I'm having a hard time understanding the for loop as it is right now, since it is different in ruby than what I have learned, which I've forgotten. X_X
**EDIT**
Never mind, I finally figured out something that worked. I'm trying to learn a bit too fast and absorb to much, I forget sometimes to slow down and review some of the things I learned as well as re-review a part of that particular area that could help.
Currently, as it is, I figured that an until do loop would work wonderful for me. -
In Ruby, for loops are essentially training wheels for iterators -- they provide a more well-known idiom to most programmers, but they're actually rarely necessary in Ruby code. In most cases, what you're really looking for are iterator methods, many of which are provided by the Enumerable module (which is already included in collections such as arrays and hashes).
Essentially, these three pieces of code do the same thing -- the differences between them are mostly in performance and the lexical scoping used (for loops do not create a new local scope, iterator methods do):
# This is how you're likely to iterate over the contents of an array coming# from a C, C++, or Java background:for i in 0...$game_variables.length puts $game_variablesend# After learning a little more about Ruby `for` loops, you'd do it this way:for i in $game_variables puts iend# This, however, is the more idiomatic Ruby way of iterating:$game_variables.each { |i| puts i }Of course, these examples don't really show you the power of iterators -- those become more clear once you begin using methods such as reduce (also known as inject), map (or collect), and select. All of these methods -- and many more -- are provided by Enumerable for iterating over collections and performing operations upon them.As for your specific problem, the solution seems very simple to me (but I've been working with Ruby for a long time now):
def example_method(farm_id) var = $farm_plots.find { |array| array.first == farm_id } unless var.nil? # Do something, making use of `var`. In this case, `var` will be the entire # array which contains the value of `$farm_id` as its first element. Note # that the return value of this method will be `nil` if no applicable array # was found in `$farm_plots` due to the `unless` conditional. endendAlso, for the record, the usage of global variables is generally ill-advised. As such, I'd reevaluate their usage here if for no other reason than to reinforce good practices -- how you decide to do so is up to you, but I recommend using a namespaced module, personally.For more information about what iterators are available for collections, please consult the documentation for Ruby 1.9.2 (the version used by RPG Maker VX Ace). In particular, you'll likely want to look at the documentation for Array, Hash, and the Enumerable module. -
Alright, I'll keep that in mind about what you said about not using Global variables, as a few tuts I have been 'watching' have mentioned not to use them too often.
**Edit**
btw, thank you for the information on the iterator methods you mentioned, as well as the example. It really helped cleaned up some a messy loop I was using and had to escape with breaks. At first I didn't exactly understand how to use it, till I actually tried using it. So again Thank you Solistra.
**end of edit**
Also... forgive me for being an idiot here but... What do you mean by a name space module? X_X
Like I said... I'm being a bit slow, even after reading and watching the tuts at the moment and I normally don't understand unless I continuously ask questions and practice the coding. Blame my messed up years with computer science with this. Changed teachers each year. X_X and I don't remember the tuts I've watched so far, mentioning namespace modules. X_X -
Something like this:
module Nak module Farming class << self attr_accessor :plots, :event_id end @plots = [ [ 9, 0, 0, 0, 0, 0 ], # Farm plot 1. [ 12, 0, 0, 0, 0, 0 ] # Farm plot 2. ] endendUsing this, you can make use of the convenience of global variables while isolating your variables to your own "name space". In this instance, modules are used because they are essentially classes without the ability to be instantiated -- there are no instances of them, so they work quite nicely for separating code into logical areas. This is also the reason for the class << self portion used to define the accessors for the module; since it is not instanced, a regular accessor will not reference the correct instance variable. Defining the accessors on the singleton class of the module ensures that they refer to the proper objects.Now, here is how you would access your variables and write to them:
Nak::Farming.plots # => [ ... ]Nak::Farming.event_id = 15 # => 15Play around with it a little bit to understand how all of this works. For instance, why doesn't @event_id have to be defined within the Nak::Farming module? How would you assign the event identifiers you want to it? What is the value of Nak::Farming.event_id before anything is assigned to it, and how can you use that value to your advantage?Good luck. -
Alright and thank you with the information you've provided, Solista. I'll certainly give it a try playing around with it, as that seems the only way I actually learn. xD
**Edit **
Going back to your iterator methods from earlier.
I had a new question that popped up in my mind that demanded that I provide an answer in order to keep flexibility of the script.
so... what I would like to ask, what would be the proper way to check for both the event_id and for the map_id. I've already learned how to get both and pass them into the function that is using them.
I know already with the information you've provided which I've used to help me change the script to end up with this for the iterator method:
tilling_plot = Naks_Plots::Farming.plots.find { |array| array.first == farm_id}
So far I know that works like an if statement or some loops would and I could use && for the second condition but... I don't know what I would use after starting with the above. Would I just go ...
tilling_plot = Naks_Plots::Farming.plots.find { |array| array.first == farm_id && array[1] == map_id }
In order to get it? or... is there a more correct way? Sorry for these questions... but... as I mentioned... I'm a slow learner and a idiot. X_X