I need some help with an event position check script snippet i made

Hello,

 

 

At first, I want to know if I could have written the code better so I dont need to use the Class Variables?

And the second thing is I have a problem which I dont know how to fix. I have uploaded a demo. If you put one rock on the switch it triggers. but if i you then move another rock the trigger cancel. I know where the problem is, but I have no Idea in how to fix this

 

I have solved the second problem and the script now looks like this: 

Spoiler
class Game_Interpreter  @@t_x   = []  @@t_y   = []  @@t_x2  = []  @@t_y2  = []   @@e_id  = []  @@e_id2 = []  def check_event_pos(id, connect, static)    unless !static      @@t_x2[connect] = $game_map.events[id].x      @@t_y2[connect] = $game_map.events[id].y      if @@t_y[connect] == @@t_y2[connect] && @@t_x[connect] == @@t_x2[connect]       @@e_id2[connect] = @@e_id[connect]       return true      else       @@e_id2[connect] = -1       return false      end      p(connect)    else      if @@e_id2[connect] != @@e_id[connect] || @@e_id[connect] == id      @@t_x[connect] = $game_map.events[id].x      @@t_y[connect] = $game_map.events[id].y      end      if @@t_y[connect] ==  @@t_y2[connect] && @@t_x[connect] == @@t_x2[connect]        if @@e_id2[connect] != @@e_id[connect]          @@e_id[connect] = id        end      end    end    return false  endend


But I think, that it can be done more easily and without using Class Variables.

I have attached the new demo. Pls look into it. I really want to improve it.

Thanks for help

Project3.rar
Explain what you're trying to do, not how you're trying to do it.


If it's what I'm thinking, you might be able to do the whole thing without scripts at all.
I agree with Shaz.

It seems that the player needs to push the red orb on top of the red button and the green orb on top of the green button. Is that all that you want to achieve? Because just that can be coded a WHOLE lot simpler and shorter and probably without those variables.

The code that you could place in a red orb to check if it is on top of the red button:

# call like this: check2(event_id) # no need to replace "event_id". # returns true if this orb is on top of the red button. Returns false otherwise. def check2(ev_id) $game_map.events[ev_id].x == 5 && $game_map.events[ev_id].y == 4 endNow you can create a simple conditional branch and voila it works. Unless you were trying to do something else 😛 . But in that case we need more information.

Oh and use different coordinates for the green button of course. But this is only 1 script line and you wouldn't even need a whole method for it depending on how many buttons you will be using in your project.
You don't even need scripting to do that. It can all be done with events. Which is why I asked what the goal was - there are several tutorials and examples that show you how to do a simple "push this object to that spot" puzzle with events.
Yes but then you have to 'mess around' with so many event-lines and perhaps some autorun/parallel events that consume unnecessary resources and such which can be a pain on big maps. Scripts are faster and easier for me personally.

But again, I do agree with you. It can be done with just events and regions and such. There is no need for scripting. Unless of course I misunderstood the OP's goals for that puzzle.
You obviously don't know how much I detest parallel process events 😉 I would not recommend doing it via events if wasn't as good as, or as easy as, doing it via scripts. And you don't need really complex events to do it.
With enough effort quite everything can be done with events. I already know it. Im just learning to script, so I do basic things.

I want to get feedback on the code, things I havent done good and how I could improve the code and so on.

Im just thinking that i could wrote this better, and for example without using the class variables.

Its difficult for me to explain what Iam doing in english but I try:

check_event_pos(id, connect, static)

"id" = the EventID(@event_id,the whole time)

"static" can be true or false.

If its true it means that the function will check the events position it is called in and the position of other connected events and return true or false.

(its mean to be used on the switches which run with a parallel process)

If static is false it means, than it will just save the x and y pos of the event it is called in.

(it is mean to be run on the "stone"-events so if the stone get moved the x and y pos will change)

And "connect" will connect the stone with the switch. Connect should be a number.E.g. a switch will only proc. if the stone on it have the same number in connect like the switch. So like you see on the demo you can create 2 switches easily, which run separated from another.

I hope you understand what i mean. Just look at the demo and Ì think you will understand.
You obviously don't know how much I detest parallel process events 😉 I would not recommend doing it via events if wasn't as good as, or as easy as, doing it via scripts. And you don't need really complex events to do it.
Ah okay clearly I indeed did not know that 😉 .

@eugene222

I'm sorry I still do not understand what you want to accomplish. But here is some quick undocumented code for a ball-game with a sample project:

Spoiler
#===============================================================================class Game_Map alias nap_ball_setup_events setup_events def setup_events nap_ball_setup_events $active_ball_game.reset $game_map.events.values.each { |ev| next if ev.list == nil ev.list.each{ |command| $active_ball_game.process_comment_tags(ev, command) } } endend#===============================================================================module Nap#=============================================================================== class Button attr_reader :x attr_reader :y attr_reader :color attr_accessor :current_ball # the ball that is currently on top of this button or -1 if none. attr_reader :switch_idx # game-switch index to use for this button def initialize(x,y,color, switch_idx) @x = x @y = y @color = color @switch_idx = switch_idx.to_i @current_ball = -1 end end # class Button #============================================================================= class Ball_Game attr_accessor :buttons def initialize @buttons = {} end def collision_handling(ev_id, color) if @buttons[color].current_ball == -1 || @buttons[color].current_ball == ev_id if $game_map.events[ev_id].x == @buttons[color].x && $game_map.events[ev_id].y == @buttons[color].y $game_switches[@buttons[color].switch_idx] = true @buttons[color].current_ball = ev_id else $game_switches[@buttons[color].switch_idx] = false @buttons[color].current_ball = -1 end p "Puzzle Completion: #{get_completion}%" end end def get_completion (@buttons.values.count{|b| b.current_ball != -1} / @buttons.length.to_f * 100).to_i end EV_COMMENT_TAG = 'button' # <button: red 1> def process_comment_tags(ev, command) if command.code == 108 if command.parameters[0] =~ /<#{Regexp.escape(EV_COMMENT_TAG)}: ([^>]*)>/ix options = $1.downcase.split(' ') buttons[options[0].to_sym] = Button.new(ev.x, ev.y, options[0].to_sym, options[1]) end end end def reset; $active_ball_game = Ball_Game.new; end end # class Ball_Game #============================================================================= $active_ball_game = Ball_Game.newend # module Nap#===============================================================================
switch-demo.zip
Haha you have made the same thing like I but thousand times better^^.

There are 2 things I dont understand in your code:

This because I dont yet now that kind of thing and how to get access to the notes and so on.

EV_COMMENT_TAG = 'button' # <button: red 1> def process_comment_tags(ev, command) if command.code == 108 if command.parameters[0] =~ /<#{Regexp.escape(EV_COMMENT_TAG)}: ([^>]*)>/ix options = $1.downcase.split(' ') buttons[options[0].to_sym] = Button.new(ev.x, ev.y, options[0].to_sym, options[1]) end end endAnd that:

What does it do exactly? I dont understand because you have called it above but so : "$active_ball_game.reset"  Isnt it "Ball_Game.reset" ?

Code:
        def reset; $active_ball_game = Ball_Game.new; end  end # class Ball_Game  #=============================================================================  $active_ball_game = Ball_Game.newend # module Nap#===============================================================================
To understand the first part you have to learn how regular expressions work. Just google for "regular expressions" (preferably for ruby). It is used to search in strings and to perform string-based actions like in this case: "<button: red 1>" is put in an array as [:red, 1] but ONLY if it starts with "button:" and not counting the "<". It's hard to explain for me. Better just look it up there are guides that can explain it much better.

def reset; $active_ball_game = Ball_Game.new; endequals:

def reset $active_ball_game = Ball_Game.newendAnd the code below assigns a new Ball_Game class to the global variable (Yeah I started using global variables instead of singletons because RPG Maker stripped out the singleton library   > 🙁     and creating one manually looks so messy imo). Screw it 😛 .

$active_ball_game = Ball_Game.new"Ball_Game.reset" would crash or at best do nothing at all. "Ball_Game" is the class and NOT a variable. The variable is "$active_ball_game" and you want to reset a variable and not a 'class definition'. See the example below:

class Foo def hello; p 'hello'; endend@a = [email hidden] # <<<<< works and prints 'hello'# You do NOT write:Foo.hello # <<<< crashes probablyThe only exception is when you have a 'static' method like "def self.hello()".

The advantage for using this code is that creating new ball games is very easy (just add the comment tags and the move-away-from-player and voila). No more fiddling around with event-lines in every new ball or button. It also uses no auto-run and no parallel processes and is only executed when moving a ball and when a game_map is loaded so it hardly consumes any resources unless you have like 200.000 events on 1 single map...
Thanks I now understand it fully, i just had a mind blockade on that variable thing. 

And yes I will look into regular expressions. Iam watching the video tutorials from diamondandplatinum and I have seen that he have made a video that covers regular expressions.
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/i-need-some-help-with-an-event-position-check-script-snippet-i-made.17512/