Ruby/RGSSx questions that don't deserve their own thread

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
Also, instead of making an init method and using .new then .init, just make an initialize method which takes the parameters for the init method, and do class.new(parameters)

and yeah, there must be something else interfering with that array.push call
 

FeaR616

Veteran
Veteran
Joined
Nov 22, 2014
Messages
277
Reaction score
52
First Language
german
Primarily Uses
yeah, it is better this way with initialize and new. I change this part, I made it because earlier I thought I need it seperate, but now this is not the case.

hm... I don't think I overwrite the push method or something. my class only have the initialize method and some getters.

this is the error I get:

https://www.dropbox.com/s/mpuob7pzfft9cy2/Unbenannt.png?dl=0

but it can't be nil, immediately before I push my object into the array, I declare it to MyClass.new(params)... I don't get it...
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
Since it says undefined method push for nil class, that means the variable named array is the one that is nil (coz you're calling array.push after all, so it looks for the push method inside the array object)...
 
Last edited by a moderator:

FeaR616

Veteran
Veteran
Joined
Nov 22, 2014
Messages
277
Reaction score
52
First Language
german
Primarily Uses
huh? did I miss something? the array is nil? how? it worked everytime to made up an array with array=[] and then use it later... 
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,517
Reaction score
3,221
First Language
Binary
Primarily Uses
RMMZ
@ AnotherFen. Again, thanks for highlighting the splat operator being used n unison with a range.

I just updated my code generator script (that uses large arrays of alphanumeric characters) so that its using [*"a".."z"] rather than ("a".."z").to_a

https://github.com/Dekita/Dekyde-Ace/blob/master/Serial%20Code%20Generator.rb

^ as seen there..

so yea, thanks for that. :)

@ Fear. It sounds as though your local variable is no longer defined when you are trying to push onto it...

This kinda issue could arise in this kinda situation...

def method_one array = []enddef method_two array.push(data)endIn method one the array is created, in method two it is trying to be used but is not available and thus, returns the nil class error.
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
Probably show us the entire script that ur making so we could see where the problem lies... The error that you posted clearly shows that the array is nil...
 

FeaR616

Veteran
Veteran
Joined
Nov 22, 2014
Messages
277
Reaction score
52
First Language
german
Primarily Uses
@Dekita: yea, I know. this shouldn't work at all.

no no, I made this:

class MyClass @myArray = [] def method1 ... end def method2 ... end def method3 do something lot object = MyClass.new(param1, param2, ..., paramN) @myArray.push(object) endend # of MyClassand I didn't see any mistake there...

oh man... C++ is way more easy to me >.>
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,517
Reaction score
3,221
First Language
Binary
Primarily Uses
RMMZ
@myarray is never being initialized. Do this instead.

(@myArray||=[]).push(object)That will make @myarray equal an array if it does not already, from there you can push onto it. :)

Edit: and remove the @myarray = [] from the top of your code.

Edit 2:

To be clear, I mean put that new line in place of the line you have that pushes onto the array in method 3.
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
Uhm, try to do it this way

class MyClassattr_accessor: myArray def initialize  @myArray = []enddef method1...enddef method2...enddef method3do something lotobject = MyClass.new(param1, param2, ..., paramN)@myArray.push(object)endend # of MyClassI do think instance variables (the @name variables) are only initialized (and can only be used) once the object/class is created, so in your case, it actually never initializes to [] since the @myArray=[] line is only run when the game inits, in which case the instance of the class is not yet existent.
 
Last edited by a moderator:

FeaR616

Veteran
Veteran
Joined
Nov 22, 2014
Messages
277
Reaction score
52
First Language
german
Primarily Uses
oh crap ... why is ruby torturing me so much? XD

and why is it working in events scripts but not in the scripts itself?

anyway, thanks again for help!
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
though, I'm just curious, why make an array of MyClass inside MyClass?

and why is it working in events scripts but not in the scripts itself?
you mean something like this in events?

@myarray=[]blahblah@myarray.push(1)Read my post above... In this case, the array is clearly initialized before it was used. That's why it works on this case. Those lines are run from the script evaluating method of Game_Interpreter, so it runs inside the same method, while on your code above, it's as I explained. Any setting inside a class that isn't part of a method will be run during game init (at least from experience, that's how it looks like). And since the @variables are instance variables, they only exist once an instance of the class is actually made, which is why even if the variable looks initialized during game init, it's not actually initialized when an instance of the class is made...

that event script looks like this

def eval_script  @myarray=[] blahblah @myarray.push(1)endwhile your original code was

class MyClass  @myarray=[]endSee the difference?
 
Last edited by a moderator:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,517
Reaction score
3,221
First Language
Binary
Primarily Uses
RMMZ
@Fear - just an fyi...

Code:
class MyKewelKlass  @mykewelvari  = [] # NOT initialized when class is created.   @@mykewelvari = [] # Initialized when class is first created. (shared by all instances of this class, so best to use sparingly.end
 

FeaR616

Veteran
Veteran
Joined
Nov 22, 2014
Messages
277
Reaction score
52
First Language
german
Primarily Uses
uhm... this was for showing purpose, the array is in a different class XD

and now it works with your help! =)

did I see this right that attr_accessor :varName is to declare class variables? and later I have to initialize them! oh lord...

@Dekita: ah thanks! but I think it is not very useful in my case if it is shared by all instances.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
@Fear 

the

attr_accessor :variable

simply automates the creation of the instance variable's writer and reader methods.

Also I do think it's (@variables) called Instance variables since it's per instance of the class, while class variables are the @@variables, which is 1 variable for ALL instances of the class.
 
Last edited by a moderator:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,517
Reaction score
3,221
First Language
Binary
Primarily Uses
RMMZ
  attr_accessor :varname   # ^ the purpose of this is to aovid having to write the following...  def varname    @varname  end  def varname=(varname)    @varname = varname  endNotice, neither of these tehcniques actually initializes the variable, it just informs the script of the variables existance, so if for example if was called from outwith the class... ie, Object.varname is called on the object,it will return the value nil (which btw, nil is actually an object), rather than raise an error. hope that helps clarify a little :)
 
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
yeah, you won't need to use it unless you're planning to access that variable outside of that class.
 

FeaR616

Veteran
Veteran
Joined
Nov 22, 2014
Messages
277
Reaction score
52
First Language
german
Primarily Uses
okay, I think this helps in understanding =)

well... now I have to look how I fill my windows with information ^^

I come back here if I have questions XD
 

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
565
Reaction score
276
First Language
German
Primarily Uses
@FeaR616: In ruby, classes are just instances of the Class class and thus equivalent to other objects. By writing @myArray = [] outside of an instance method you accessed the instance variable of self. In the given scope, you changed the value of an instance variable of the class object MyClass itself.

You only need attr_accessor if the variable should be accessed from outside the object (getter and setter methods in other languages). The equasions have been already pointed out. :)

If you want to initialize the variable with a value other than nil, you can do that explicitely within the initialize method (EDIT: or do as Dekita demonstrates 10 posts above).
 

@Dekita:

I prefer this method expecially when combining multiple sets:

[*'0'..'9', *'A'..'Z', *'a'..'z', '_', *special_characters]
(as an alternative to using flatten(1) or +).

However, I don't even use a consistent style at the moment...
 
Last edited by a moderator:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,517
Reaction score
3,221
First Language
Binary
Primarily Uses
RMMZ
Yea, I'm fairly familiar with 'splatting things for kicks', such as this..

@mvarg ||= ['i',['p',*['i']*4,'p'],'MoveWindow',USER] @mvwin ||= Win32API.new(*@mvarg.reverse)But had never even thought of using a range with it.

Personally, I feel my only consistency is being inconsistent. Since I started coding my 'coding style' has changed dramatically. (as I'm sure it does with most people as they find things they like and generate preferences) but like, reading code I wrote not even a few months ago and now I would have written it completely differently. IMO - theres no harm in trying out something new, providing its not excessive.
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,162
Reaction score
823
First Language
Hungarian
Primarily Uses
RMVXA
A Performance question:

I heard that eval can slow down the game if used unwise...

So, I have this little dilemma...

I got two ways to set up my method. One uses an eval method and it takes only 2 lines to do.

The other one takes 18 lines with a case statement but without any eval method.

The method will be called every single time the player attacks or does any battle actions and the battle system in question is an ABS, so the player will be mashing buttons left and right for sure to attack the enemies...

What should be better to use?

Another question on an entirely different subject:

So, I have this note-tag grabber method:

def cooldown_bonus ary = Array.new if @note =~ /Cooldown Bonus = <(\d+[ ].*(?:\s*,\s*\d+[ ].*)*)>/i i = 0 $1.split(", ").map.each do |info| ary = Array.new info =~ /(\d+) (.*)/i ary.push($1.to_i) # value ary.push($2.to_s.downcase) # stat i += 1 end end return ary endWhich works fine.The actual note-tag would look like this, for example:

Cooldown Bonus = <12 AGI, 5 ATK, 2 MAT, ... >So, it is an "infinite" note-tag.After the method, I get an array like this:

ary = [[12,"agi"],[5,"atk"],[2,"mat"], ... ]And this is exactly what I need.I am pretty new in making these infinite long note-tags with mixed data (meaning number and string needed from it), so I was wondering if there is an easier/shorter method of getting the data from this kind of note-tag... So... Is it? :D
 

Users Who Are Viewing This Thread (Users: 0, Guests: 3)

Latest Threads

Latest Posts

Latest Profile Posts

Day 9 of giveaways! 8 prizes today :D
He mad, but he cute :kaopride:

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.

Forum statistics

Threads
106,040
Messages
1,018,472
Members
137,822
Latest member
madelbylz
Top