Searching through the script editor only goes so far, since you can't see a lot of the core classes like Bitmap or Sprite. A better resource is: Cidiomar's RGSS3 Reference Guide. That's usually my first place to look when I forget how to call a function, or when I'm wondering how to use some class I've never used before.
Ruby/RGSSx questions that don't deserve their own thread
RGSSx Script SupportThanks. Bookmarked it.
Be nice if it had syntax layout instead of just listing the attributes.
I'm sure that with time, I won't need to have my hand held so much but at the beginning it's hard to get fluent in the new language without context and so on.
The help file is pretty good too. I spend a lot of time looking through that at the various classes that aren't defined in the scripts.
pity they hide the method definitions...
Took me almost two days to figure out how to replicate the Input.trigger? method...
Ideally, we wouldn't need to know how they are defined anyways. I mean, none of the scripts I've written have ever needed to change how the stuff is defined, for example, cause they're just really basic building blocks.
Plus I would figure the definition is just a call to a C library or something ...
Plus I would figure the definition is just a call to a C library or something ...
yea i didnt want to change the way its defined, i just wanted to replicate it. ( for other key triggers )
i ended up getting it though and its so simple, i couldn't believe how simple it was when i finally figured it out 😛
code in spoiler
i ended up getting it though and its so simple, i couldn't believe how simple it was when i finally figured it out 😛
code in spoiler
Code:
#===============================================================================module Keys#=============================================================================== #--------------------------------------------------------------------------- # Win32API Information #--------------------------------------------------------------------------- def self.w ; Win32API ; end def self.u ; "user32" ; end def self.i ; "i" ; end def self.gk ; "GetKeyState" ; end def self.ga ; "GetAsyncKeyState" ; end #--------------------------------------------------------------------------- # Key Settings #--------------------------------------------------------------------------- Key={ # Func's :F1 => 0x70,:F2 => 0x71,:F3 => 0x72,:F4 => 0x73,:F5 => 0x74,:F6 => 0x75, :F7 => 0x76,:F8 => 0x77,:F9 => 0x78,:F10 => 0x7a,:F11 => 0x7b,:F12 => 0x7c, # Numbers :_0 => 0x30,:_1 => 0x31,:_2 => 0x32,:_3 => 0x33,:_4 => 0x34, :_5 => 0x35,:_6 => 0x36,:_7 => 0x37,:_8 => 0x38,:_9 => 0x39, # Letters :A => 0x41,:B => 0x42,:C => 0x43, => 0x44,:E => 0x45,:F => 0x46,:G => 0x47, :H => 0x48,:I => 0x49,:J => 0x4A,:K => 0x4B,:L => 0x4C,:M => 0x4D,:N => 0x4E, => 0x4F, => 0x50,:Q => 0x51,:R => 0x52,:S => 0x53,:T => 0x54,:U => 0x55, :V => 0x56,:W => 0x57,:X => 0x58,:Y => 0x59,:Z => 0x5A, # NumPad :N_0 => 0x60, :N_1 => 0x61, :N_2 => 0x62, :N_3 => 0x63, :N_4 => 0x64, :N_5 => 0x65, :N_6 => 0x66, :N_7 => 0x67, :N_8 => 0x68, :N_9 => 0x69, :ADD => 0x6b, :MUL => 0x6a, :SUB => 0x6d, :DIV => 0x6f, :DECI => 0x6e, # Arrows :LEFT => 0x25, :RIGHT => 0x27, :UP => 0x26, :DOWN => 0x28, # Symbols :S_Colon => 0xBA, :Equal => 0xBB, :Comma => 0xBC, :Minus => 0xBD, :Period => 0xBE, :F_Slash => 0xBF, :HASH => 0xDE, :L_Sqr_Brack => 0xDB, :B_Slash => 0xDC, :R_Sqr_Brack => 0xDD, :S_Quote => 0xC0, # Other :SHIFT => 0x10, :LSHIFT => 0xa0, :RSHIFT => 0xa1, :TAB => 0x09, :PAUSE => 0x13, :L_CTRL => 0xa2, :R_CTRL => 0xa3, :CTRL => 0x11, :BACK => 0x08, :ENTER => 0x0D, :CAPS => 0x14, :SPACE => 0x20, :ESC => 0x1B, :ALT => 0x12, } # << End Key {} #--------------------------------------------------------------------------- # Constants #--------------------------------------------------------------------------- KS = w.new(u,gk,[i],i) AK = w.new(u,ga,[i],i) #--------------------------------------------------------------------------- # Variables #--------------------------------------------------------------------------- @trigger = Array.new(256, false) @presses = Array.new(256, false) @release = Array.new(256, false) @repeats = Array.new(256, false) #--------------------------------------------------------------------------- # Update All Key's State's #--------------------------------------------------------------------------- def self.update @trigger = Array.new(256, false) @release = Array.new(256, false) Key.each do |key| akey = AK.call(key[1]) ktri = KS.call(key[1]) @trigger[ key[1] ] = true if ((!@presses[key[1]]) && (akey != 0)) @release[ key[1] ] = true if (( @presses[key[1]]) && (akey == 0)) @presses[ key[1] ] = (akey != 0) ? true : false @repeats[ key[1] ] = (ktri == 1) ? true : false end end #--------------------------------------------------------------------------- # Key Got Triggered? [ inital key press ] #--------------------------------------------------------------------------- def self.trigger?(key) return if key.nil? return @trigger[key] end #--------------------------------------------------------------------------- # Key Got Released? [ inital key depress ] #--------------------------------------------------------------------------- def self.release?(key) return if key.nil? return @release[key] end #--------------------------------------------------------------------------- # Key Being Pressed Currently ? #--------------------------------------------------------------------------- def self.press?(key) return if key.nil? return @presses[key] end #--------------------------------------------------------------------------- # Key Turned On? [Caps Lock, Num Lock, ect..] #--------------------------------------------------------------------------- def self.repeat?(key) return if key.nil? return @repeats[key] endend#===============================================================================module Input#=============================================================================== #--------------------------------------------------------------------------- # Alias List #--------------------------------------------------------------------------- class << self alias :update_sD13x_Keys :update end #--------------------------------------------------------------------------- # Update $D13x Keys #--------------------------------------------------------------------------- def self.update(*args) update_sD13x_Keys(*args) Keys.update end end
Not sure if this would be a big deal or not. I'm trying to make a conditional for when an event is in a certain range from another event. Say, EVENT01 is within 4 tiles of EVENT02.
if switch 1 is on
If EV01 is within 4 tiles of EV02
turn switch 2 on
if at all possible, a circular area surrounding EV01 would be preferrable to a square one.
if switch 1 is on
If EV01 is within 4 tiles of EV02
turn switch 2 on
if at all possible, a circular area surrounding EV01 would be preferrable to a square one.
Thanks Tsuki. It works for what it's intended, but I need one that detects when an event gets within range of another event. I assume it's not impossible to edit that script.
Another unfortunate thing is that there seems to be a compatibility issue with Victors Pixel Movement Script, which I'm already heavily invested in. Something about "undefined method 'times' for 4.0:Float"
Another unfortunate thing is that there seems to be a compatibility issue with Victors Pixel Movement Script, which I'm already heavily invested in. Something about "undefined method 'times' for 4.0:Float"
Hello, what is the difference between those two ways to add an instance (object) into an array:
Code:
$my_array = my_class.new# Two ways:$my_array += my_class.new$my_array[1] = my_class.new
I'll do this line by line using your literal examples.
$my_array = my_class.newYou've initialized a new object called $my_array, but is it really an array? Unless 'my_class' is a child class of Array or has operator methods that simulate an array, $my_array is not an array object.
$my_array += my_class.newLet's assume $my_array is really an Array object ( '$my_array = []' ). In this case, this statement will not work and you'll receive an error. The '+' operator for array objects only concatenates other array objects. Example link here.
$my_array[1] = my_class.newAgain, assume $my_array is an actual Array object ( '$my_array = []' ). In this case, you won't receive an error since you are instantiating an object into a specified element. But you've essentially created this array:
$my_array[1] = my_class.new # => [nil, my_class.new]You must remember that Ruby (and practically every other programming language) uses zero-based numbering: http://en.wikipedia.org/wiki/Zero-based_numbering
This means the first element of an array is at index 0, the second element is at index 1, etc.
So to answer your question, what's the difference between the two ways? One doesn't work, and the other does (but probably not in the way that you want).
Let's rewrite your examples in another way.
$my_array = []This statement initializes '$my_array' into an empty array. This eliminates any ambiguity in regards to what kind of object '$my_array' is.
'$my_array = Array.new' is an equivalent statement, by the way.
$my_array = [] # => []$my_array.push(1) # => [1]$my_array.push(2) # => [1, 2]$my_array.push(3) # => [1, 2, 3]$my_array[1] # => 2For learning purposes, use 'push' to add new objects into an array. 'push' will guarantee that the object you add will be placed after the last element in the array. '$my_array.push(obj)' is equivalent to '$my_array[$my_array.size] = obj'
Also, most any programmer understands the basic concept of a stack (first in, last out). So whenever anyone else looks at your code and sees 'push', they will know at a glance that $my_array is an Array object without even looking at what $my_array was initialized as.
The opposite of 'push' is 'pop'.
$my_array = [1, 2, 3]$my_array.pop # => 3$my_array # => [1, 2]
And using your 'my_class' object:
$my_array = []foo = My_class.new$my_array.push(foo)$my_array # => [foo]
Edit: I just remembered. The first letter of a class name in Ruby must be capitalized. (e.g. class My_class)
Thanks a looooootttt my friend!
just thought i would add a little onto Mr Bubbles explanation..
you can also do this to initialize arrays
# $my_array = [initial value for all elements] * (number of elements)# like this...$my_array = [false] * 5# this would make an array like this...# [ false, false, false ,false, false ]# you can test this by printing the $my_array variable
or you could also do this...
$my_array = Array.new(number of elements) { initial value for all elements } # Or this...$my_array = Array.new(number of elements, initial value)
Also, to add onto arrays you can use this method ( works the same as push )
$my_array << value to push
Also, you can use the += operator to add onto the array ( once its been initialized ) like this...
#$my_array += another_array# eg.$my_array += [ false, true, nil, nil false]
To show a complete example of this you can pop this code into your script editor and it will show you ...
$my_array = [false] * 5another_array = [true] * 5$my_array += another_array5.times do |the_following_code_until_end| $my_array << "some text"endp $my_array
anyway, thought that might add little information to mr bubbles initial help 🙂
you can also do this to initialize arrays
# $my_array = [initial value for all elements] * (number of elements)# like this...$my_array = [false] * 5# this would make an array like this...# [ false, false, false ,false, false ]# you can test this by printing the $my_array variable
or you could also do this...
$my_array = Array.new(number of elements) { initial value for all elements } # Or this...$my_array = Array.new(number of elements, initial value)
Also, to add onto arrays you can use this method ( works the same as push )
$my_array << value to push
Also, you can use the += operator to add onto the array ( once its been initialized ) like this...
#$my_array += another_array# eg.$my_array += [ false, true, nil, nil false]
To show a complete example of this you can pop this code into your script editor and it will show you ...
$my_array = [false] * 5another_array = [true] * 5$my_array += another_array5.times do |the_following_code_until_end| $my_array << "some text"endp $my_array
anyway, thought that might add little information to mr bubbles initial help 🙂
What is the difference between x/y, self.x/self.y and @x/@y in a class?
In Sprite_Character, the update_position method does this:
move_animation(@character.screen_x - x, @character.screen_y - y) self.x = @character.screen_x self.y = @character.screen_yI have, for some reason, added this to the initialize method:
@x = @character.screen_x @y = @character.screen_yand when I comment it out now, I get an error on the move_animation line saying x and y are nil. I cannot see ANY difference in either method between my modified script and the default script from the RTP, and I don't have any scripts that overwrite or alias either of those methods.Do all three mean the same thing? When I puts them to the console, they all have the same values (when I leave the @x and @y in the init method), but it seems I can't use them interchangeably. When is the appropriate time to use/avoid each one?
In Sprite_Character, the update_position method does this:
move_animation(@character.screen_x - x, @character.screen_y - y) self.x = @character.screen_x self.y = @character.screen_yI have, for some reason, added this to the initialize method:
@x = @character.screen_x @y = @character.screen_yand when I comment it out now, I get an error on the move_animation line saying x and y are nil. I cannot see ANY difference in either method between my modified script and the default script from the RTP, and I don't have any scripts that overwrite or alias either of those methods.Do all three mean the same thing? When I puts them to the console, they all have the same values (when I leave the @x and @y in the init method), but it seems I can't use them interchangeably. When is the appropriate time to use/avoid each one?
Short answer is that in the case of Sprite objects, the assignment operators for x= and y= store value in the metadata rather than in an instance variable. You can't access them with @x and @y unless other methods have been added to allow this. If there are no redefinitions for x and y, try looking for attr_reader methods, as these would also change the default functionality. Try looking in every superclass. The metadata for Sprite wont allow nil assignment for x or y, so something has to be changing the accessors for either to be read as nil on the move_animation line.self.x refers to the method 'x' for the object at the current scope. 'x' refers to the local variable x, or, if it does not exist, the method 'x' at the current scope. @x refers to the instance variable @x. In many cases, they may refer to the same thing, but they can at times be very different.
To go a bit more in depth, self.x does not necessarily refer to the instance variable @x. If it is a simple attribute reader or accessor method, it will return the value in @x, but otherwise it would work like any other method. As to the variable 'x', this is nearly identical to referencing 'self.x' in most situations, including the one you have posted. However, if you attempt to assign something to 'x', you'd be creating a new local variable and not using the assignment operator for the object. So 'x' does not necessarily refer to the same value 'self.x' or '@x'. 'self.x =' does not necessarily assign a value to @x (in the case of Sprite, it does not), and using a 'x =' will never assign the value to @x as it will assign to the local variable x (you need to explicitly state the object when using an assignment operator, which is why the 'self' on those lines).
In the case you have posted above, you should always use the accessor methods. Sprites use their metadata to determine where they should be displayed on screen, they wont recognize values that are just put into instance variables. That just leaves the matter of what redefined the reader method, and why.. and I can't think of anything off the top of my head here as to why that would be done.
In other situations, I still prefer to use the methods even if they are simple accessors. Sure, just referencing the instance variable directly saves a modicum of processor time, but it is a lot easier to deal with changing situations if you always use accessors, because you can alias or redefine the accessors. Having the methods encapsulated like this gives you much more control over the data in the event you need to change something. I don't feel that it is necessary to always explicitly access the variables using 'self' (except, of course, when performing assignment), but this is a personal preference. For example, if you ever use x as a local variable within the same class, you should probably denote when you are using the accesssor method by using self.x. Of course, if you are only using the variables locally but across many methods, a simple instance variable will do the trick without the need for any accessors
I am interested to know if there is a possible way to erase the number system on your item window and erase the column based style on it. I am halfway making my own window and the ones that bugs me is that I want to make a window that displays the Item Window or Key Item window only. As I place or add new items to it, the system should not indicate how many items of that kind is in my inventory. So even if I have a 67 Apples inside my inventory, it should only display Apple.
yes there are ways to do both of those. (not on my cpu right now to give the correct methods)
i assume you would be using some kind of draw_item method
and if its the default one ( inherited from Window_Base ) im pretty sure you would only have to modify that ( within your new window class, not the window base class )
it should look something along the lines of...
def draw_item(x,y,item) draw_text(x, y, 180, line_height, item.name) draw_text(x, y, 180, line_height, $game_party.item.number(item), 2)endthen again, when its a command window your doing it with, it works a little differently (cant remember exactly how as i always just add a new draw item method into the refresh method and delete all the normal command window text ( so it only shows the selector ) )
either way, i think a little more information would be helpful for others who try and help you resolve this issue, maybe even post a copy of your window code 🙂
Edit:
if your using a 'normal' item window it should inherit from
class Window_ItemList
so you will probably find the relevant method in there..
i assume you would be using some kind of draw_item method
and if its the default one ( inherited from Window_Base ) im pretty sure you would only have to modify that ( within your new window class, not the window base class )
it should look something along the lines of...
def draw_item(x,y,item) draw_text(x, y, 180, line_height, item.name) draw_text(x, y, 180, line_height, $game_party.item.number(item), 2)endthen again, when its a command window your doing it with, it works a little differently (cant remember exactly how as i always just add a new draw item method into the refresh method and delete all the normal command window text ( so it only shows the selector ) )
either way, i think a little more information would be helpful for others who try and help you resolve this issue, maybe even post a copy of your window code 🙂
Edit:
if your using a 'normal' item window it should inherit from
class Window_ItemList
so you will probably find the relevant method in there..
I'll check on that, thanks.
I'll stay here to read some of the possible solutions as well...
this is a big help.
I'll stay here to read some of the possible solutions as well...
this is a big help.
Is there a script conditional that checks if an picture exists? If an picture with the ID of 1 has been shown.
My current problem is, I have a retry-chapter mechanic that erases all of the shown pictures. However, the game will crash if a picture has not yet been shown or initialized when I erase it.
My current problem is, I have a retry-chapter mechanic that erases all of the shown pictures. However, the game will crash if a picture has not yet been shown or initialized when I erase it.
You can use $game_map.screen.clear_pictures that will erase all existing pictures without having to loop through them.
Original thread: https://forums.rpgmakerweb.com/threads/ruby-rgssx-questions-that-dont-deserve-their-own-thread.150/