[RGSS3] attr_accessor

What is attr_accessor and what does it do?


I'm sorry if it's obvious but I'm not sure what it does, I have used it in a script I was following from a tutorial however he does not go into it, he mearly skims it.


The most I can assume is that it is kind of like setting and label and using goto label.


So, for example, I have a very very incomplete script and I am looking to go into


Class Update_NPCNeeds


and have it update every 60 frames, would attr_accessor help with that?
 


Warning: Longish code


class NPC_NeedsInitialize < Scene_Base # Create a scene so Game_Interpreter Works
def initialize # Initialize content
@mapid = [] #Map ID Array (to check for existing maps in D😎
@npcid = [] #NPC ID Array (to check for existing NPCs in D😎
@npcinitialized = [] # NPC Initialized (Probably unneeded)
@npcmoney = [] #NPC Money it sets up an NPCs money that it will drop on death (maybe)
@npcs = 0 #The amount of NPCs in the array
@maps = 0 #The amount of maps in the array
#Captain hindsite said I should have used array.length
@npchunger = []
@npcthirst = []
@npcsleep = []
end

def prepare(npcid, mapid, npcmoney) # Get info from event script (user entered)
for i in 1...@maps #Check all maps, if one doesn't exist, add it.
if @mapid == mapid
@currentmapused = i
end
if mapid != @mapid && i == @maps
@mapid.insert(i, mapid)
@currentmapused = i
end
end
a = @currentmapused*1000
for i in 1...@npcs #Check all NPCs, if one doesn't exist, add it.
b = a+i
if @npcid == npcid && i == @npcs
@currentnpcused = i
end
if npcid != @npcid && i == @npcs
@npcid.insert(b, npcid)
@currentnpcused = b
end
end #By the end of this, all npc id's should be like this:
#(MapID * 1000) + NPCID So there's room for 999 NPCs
#Example: MapID = 1 NPCID = 20 So his/her ID would be 1020
end
end

class Update_NPCNeeds
def npcupdate
#Placeholder values
@currentnpcused = a
@currentmapused = b
c = b*1000
d = a+c

#NPC Hunger
if @npchunger[d] == nil
@npchunger.insert(d, 100)
end else
if @npchunger[d] >= 0
e = @npchunger[d] - 1
@npchunger.insert(d, e)
end

#NPC Thirst
if @npcthirst[d] == nil
@npcthirst.insert(d, 100)
end else
if @npcthirst >= 0
e = @npchunger[d] - 1
@npchunger.insert(d, e)
end

#NPC Sleep
if @npcsleep[d] == nil
@npcsleep.insert(d, 100)
end else
if @npcsleep >= 0
e = @npcsleep[d] - 1
@npcsleep.insert(d, e)
end
end
end

class Game_Interpreter
def npcneeds(npcid, mapid, npcmoney)
SceneManager.call(Scene_DataHolder)
SceneManager.scene.prepare(npcid, mapid, npcmoney)
end
end


And no, I don't want someone to make anything for me.


I just want to know how to use attr_accessor and if you are a very nice person (which you are, that's why I luv you 🙂) share how I would run this every 60 frames on every NPC tagged <human> or <orc>


I can probably figure out the tagging thing, I think @Yanfly has a tutorial on that or something.


Anyway thanks! It's another late late night so I need to sleeeeeeepppppp.
attr_accessor basically lets you use the @ variables from the class outside of the class. I don't know the details but ithere is a tut here:http://www.rubyist.net/~slagell/ruby/accessors.html


Essentialy you use it like this. Say you have a Game_Example class, which has a variable called @example.


attr_reader :example


Lets me read the variable, for example myvar = $game_example.example


attr_writer :example


Lets me write to the var, for example $game_example.example = myvar


attr_accessor :example


Lets me do both.


I hope that makes sense. So when you want another class to be able to write to a variable in your class you use attr_writer, or attr_reader, or attr_accessor, depending on how much protection you want the variable to have.


The colon before the variable name makes it a symbol.


http://ruby-doc.org/core-2.2.0/Symbol.html
attr_accessor basically lets you use the @ variables from the class outside of the class. I don't know the details but ithere is a tut here:http://www.rubyist.net/~slagell/ruby/accessors.html


Essentialy you use it like this. Say you have a Game_Example class, which has a variable called @example.


attr_reader :example


Lets me read the variable, for example myvar = $game_example.example


attr_writer :example


Lets me write to the var, for example $game_example.example = myvar


attr_accessor :example


Lets me do both.


I hope that makes sense. So when you want another class to be able to write to a variable in your class you use attr_writer, or attr_reader, or attr_accessor, depending on how much protection you want the variable to have.


The colon before the variable name makes it a symbol.


http://ruby-doc.org/core-2.2.0/Symbol.html

This..... Actually makes so much sense.


Thank you a ton!

Original thread: https://forums.rpgmakerweb.com/threads/rgss3-attr_accessor.63198/