Help getting method to work

http://pastebin.com/e4LXPWGf


The above is a pastebin link to a method I wrote to:


a) Check if the player is within range of the event


b)Show a window for x seconds


c)Close the window and flip self switch A


The problem is I have no idea how to get it to allow me to use it. If I just paste it in it tells me x is an undefined method, or pop_up is an undefined method. Any help is appreciated. Rewriting the whole thing to get it to work is also something I would take XD Though I would rather understand what exactly I need to do for future reference. Thanks ^_^


Edit: Also always looking to improve my programming abilities. If there is something completely derpy that you see please tell me 🙂
Ok well... That was probably one of the longest methods i've seen. You should try posting a screenshot of the error. 
xD Yeah... It could probably be done better. I literally just took what I did in what RPG Maker gives me and rewrote it in Ruby. The error it's giving me is:


"Script 'Game_Interpreter' line 1411: NoMethodError occurred.


undefined method 'x' for 7:Fixnum"


Which I took as it can't find the .x method from where mine is at (Currently plopped down above main under materials because I have no clue where to put it). I have 0 scripting experience and almost as much actual programming experience. So the main problem is I just have no clue where to place the method so that it has access to everything it needs.
The script you linked is incomplete as it doesn't say for which class the method is defined. And that is most probably the reason for the error itself: you try to reference an x from the class it belongs to, but we can't say if that class has an x defined because we don't know which class that method belongs to.


And the error basically says "this class has no x defined"


Please check existing scripts on how you need to assign a method to a class before it can work
I understand that. That's the problem. I don't know which class has the x method. I have no clue where this method needs to go. I have already tried looking at other scripts, but I'm not well versed enough in scripting to understand exactly what to do. Which is why I'm here. Hoping someone will direct me in the correct direction, rather than tell me "Everything is wrong with this. Figure it out on your own." The method needs to be able to use .x .screen_x. I haven't yet been able to find what class those are in.
I think you should go work through some basic programming tutorials as they're linked in the "learning" sections.


More specifically, you need to learn how Object-Oriented Programming works, because you're trying to follow the principles of Functional Programming, not of OOP. And unfortunately that will not work in an engine that goes beyond basic functional programming.


Sorry - that difference is not something that is easy to explain in a few sentences (or at least not if you intend to learn something from the explanation), and I simply don't have the time to explain all that. And yes, depending on how much you already know of programming (independent of language), that difference might take you several weeks to learn.
Ok.
Whichever class you put that in, it will almost surely break other things if called.


You see, it creates instance variables (a lot of them which is not necessary at all) which are already defined in many classes (like @x, @y, etc). 


More importantly, you don't even need to create those instance variables, they can be simple local variables for what they are used in the method.


It tries to find the 'x' property/method of a class, but that class got no 'x' method, because that class is an integer (number 7).


Knowing this, you most probably used 7 for the 'event' argument, which will trigger the error you got because of the '@event.x' and '@event.y' parts.


You will need to change those into '$game_map.events[@event].x' and '$game_map.events[@event].y'.


But again, no need for instance variables, you can use local ones.


And seeing that you use many arguments which actually define (or can be used to define) all things you need in that method, you don't really need to put it into any class.


I usually put these methods (which operate solely with data obtained from arguments) into my own modules, so I can access them from any class/module.

Original thread: https://forums.rpgmakerweb.com/threads/help-getting-method-to-work.55942/