Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

I need help with my switches and objects script.

Hey everyone.
ok .. I'm going to cover this topic in detail.

First of all, the intended function of the script, without throwing code at you.

I want to have a global table, that stores the following information :

Switch number
Switch's X position on the map
Switchy's Y position on the map
Ball number
Ball's X position on the map
Ball's Y position on the map
The variable number of the switch's X position
The variable number of the switchy's Y position
The variable number of the ball's X position
The variable number of the ball's Y position

The ball is an object.
The switch is an object.

Each instance of the switch object will have a flag.
The flag will be which switch ($game_switches[switchnumber] ) to turn on if a collision is detected.

Seperate events will be dependent on said switches.


This may seem like alot of data, but using other programs I've handled alot more data.

Now for the actual issue itself.
First, My Shandler class.

Code:
class Shandler

attr_accessor :switchnum

attr_accessor :ballnum

attr_accessor :ballx

attr_accessor :bally

attr_accessor :switchx

attr_accessor :switchy

attr_accessor :switchstate

 

def initialize

 

$bx=Table.new(100)      #tables

$by=Table.new(100)      #tables

$bxvar=Table.new(100)    #tables

$byvar=Table.new(100)    #tables

$swx=Table.new(100)    #tables

$swy=Table.new(100)    #tables

$swxvar=Table.new(100)  #tables

$swyvar=Table.new(100)  #tables

$swstate=Table.new(100)   #tables

 

 

end

 

def collide(targeta, targetb)

  if $swx[targeta]==$bx[targetb]

  if $swy[targeta]==$by[targetb]

      $game_switches[targeta]="on"

    end

    end

  

end

 

def newswitch(switchnum, switchx, switchy)

$swx[switchnum]=switchx

$swy[switchnum]=switchy

end

 

def newball(ballnum, ballx, bally)

$bx[ballnum]=ballx

$by[ballnum]=bally

end

 

def ballx(num, var)

  $bxvar[num]=var

end

 

def ballx(num, var)

  $bxvar[num]=var

end

 

def switchx(num, var)

  $swxvar[num]=var

end

 

def switchy(num, var)

  $swyvar[num]=var

end

 

  def bally(num, var)

  $byvar[num]=var

end

  

def getballx(num)

  tempnum=$bxvar[num]

$bx[num]=$game_variables[tempnum]

end

 

def getbally(num)

  tempnum=$byvar[num]

  $by[num]=$game_variables[tempnum]

end

 

def getswitchx(num)

  tempnum=$swxvar[num]

  $swx[num]=$game_variables[tempnum]

end

 

def getswitchy(num)

  tempnum=$swyvar[num]

  $swy[num]=$game_variables[tempnum]

end

 

def pcoord(num)

  print "X coordinate of ball"

  print $bx[num]

  print "Y coordinate of ball"

  print $by[num]

  print "X coordinate of switch"

  print $swx[num]

  print "Y coordinate of switch"

  print $swy[num]

  end

  

end

Ok now let me explain each chunk of this class and how it's SUPPOSED to work.

Code:
$bx=Table.new(100)      #tables

$by=Table.new(100)      #tables

$bxvar=Table.new(100)    #tables

$byvar=Table.new(100)    #tables

$swx=Table.new(100)    #tables

$swy=Table.new(100)    #tables

$swxvar=Table.new(100)  #tables

$swyvar=Table.new(100)  #tables

$swstate=Table.new(100)   #tables

These are the table definitions, before the initialize method for this class.

Basically, each of these HAS to be a global variable, because I need each instance of the object, to be able to access this table.

I want to have up to 100 switches, and 100 balls.
The X and Y coordinate of these switches and balls has to be stored in a table, or array. Tables seem to work better.

Now...

I ran into a slight problem.
I can't seem to grab the X and Y coordinate of "this event" through a script and store it into a table.
Or anything really.
It would save me alot of trouble if I could grab the X and Y coordinate of an event or current instance class without having to use the event commands:

Control Variables [001-01] -This event's MapX
Control variables [002-02] - This event's Mapy

I don't see why the people who made the rgss didn't include a way to grab the X and Y coordinates of "this event" through a script command. If there IS a way, I'd like to know. It would be very helpful.

Moving on. The reason that the $swxvar $swyvar and the $bxvar $byvar tables exist is so that I can do the following:

$swxvar is the variable number that holds the switch's X coordinate.
$swyvar is the variable number that holds the switch's Y coordinate.
$bxvar is the variable number that holds the ball's X coordinate.
$byvar is the variable number that holds the ball's Y coordinate.
$bx holds the ball's X coordinate
$by holds the ball's Y coordinate
$swx holds the switch's X coordinate
$swy holds the switch's Y coordinate

I make a call to my class through an event like this:


For the ball:

Event Page 1:
Condition: Paralell Process
Script:
Code:
A=Shandler.new

A.newball(1,4,4)
Control Self Switch A ON


Event Page2:
Condition: Player Touch
Set move route: This event: Move away from player
Control Variables [001-Switchx] This event's Mapx
Control Variables [002-Switchhy] This event's Mapy
Script:
Code:
A.ballx(1,1)

A.bally(1,2)

A.getballx(1)

A.getbally(1)

Ok, the first event page basically sets up the ball. And call's to the script Shandler.new (creates a new ball )
The function Newball takes three values. Ball number, Ballx, and bally.
The ballx and bally get stored (They're supposed to get stored) in the global variables:
$bx[ballnumber] $by[ball number]
So far I hope this makes sense.
The second event page for the ball event, calls the ballx and bally functions of Shandler.
The first value that gets passed with both of these, is for WHICH ball NUMBER, these variables are for.
A.ballx(1,1) (ball number1, store it's x coordinate in $game_variables[1] )
A.bally(1,2) (ball number1, store it's y coordinate in $game_variables[2] )
These last two lines are the most important.

A.getballx(1)
A.getbally(1)
These call the methods getballx and get bally which basically... take the corresponding values of which variable is the ball's x in and which variable is the ball's y in.. and puts them in $bx[ball number] $by[ball number]

Code:
def ballx(num, var)

  $bxvar[num]=var

end

As you can see, this is the ballx function.
Basically, the ball's numer is stored, Var is the variable number for the coordinate.
So then $bxvar[ball number]=variable number.
This mean's that if I say... A.ballx[20, 1]
The script stores the value 1 in $bxvar[20] (ball 20's variable reference number)
It's the same thing for the Y coordinate. Look at the bally method.

Now..
This is where I begin to REALLY run into issues...
Ok here's the next event script:
A.getballx(1)
A.getbally(1)

Code:
def getballx(num)

  tempnum=$bxvar[num]

$bx[num]=$game_variables[tempnum]

end

What this is supposed to do, is use $bxvar[ball number]
It takes the value of $bxvar[ballnumber]
So say we want to use variable 1 for ball number 1
That means that $bxvar[1] would have to equal 1
Tempnum is supposed to become equal to this value.
So the next step is that tempnum has to equal 1 in this case.
So now tempnum=1
Good.
now. we have to make $bx[1] (for ball 1's x] equal to what $game_variables[1] is since, we're using $game_variables[1] for the ball's X and we can't directly pull the ball's X coordinate in script and put it into a table... stupid work around. Anyhow.
So if I pass this script in.

Code:
A=Shandler.new

A.newball(1, 2, 2)

A.ballx(1,1)

A.getballx(1)

The following should result:
The global table $bx[1] should be equal to whatever $game_variables[1] is since I'm basically telling the script that ball 1's x is to be stored in $game_variables[1] the only step is that I have to have this in the event in a loop: Control variables[001]- This event's MapX

now that's all fine and dandy... until we get into my collide routine.
I wont' cover the switches as they basically do the same thing as what I just explained, only using different tables...

So the event for the switch does the same thing
B=Shandler.new
B.newswitch(1,1,1) (the switch number is 1, the switch's x is 1, the switch's y is 1)
B.switchx(1,3) (Switch 1's X is stored in $game_variables[3]
B.switchy(1,4) (Switch 1's Y is stored in $game_variables[4]
B.getswitchx(1) (takes whatever is in $game_variables[3] and puts it into $swx[1]
B.getswitchy(1) (takes whatever is in $game_variables[4] and puts it into $swy[1]

NOW the final part.

B.collide(1,1)

Code:
def collide(targeta, targetb)

  if $swx[targeta]==$bx[targetb]

  if $swy[targeta]==$by[targetb]

      $game_switches[targeta]="on"

    end

    end

  

end

Ok so we covered most of this. At this point...
$swx[1] should be holding the x coordinate of switch 1
$swy[1] should be holding the y coordinate of switch 1
$bx[1] should be holding the x coordinate of ball 1
$by[1] should be holding the y coordinate of ball 1
Right.
In this method Targeta is used for two things.
Basically you pass the switch number, and the ball number to Shandler, with the .collide method call.
And it checks to see, if Switch Number is ontop of ball number.
if it is, it turns switch number on
So say we have switch 1 and ball1

basically, I want it so that if:
$bx[1] equals $swx[1]
and
$by[1] equals $swy[1]
Turn $game_switches[1] "on"
else,
don't do anything.

THE ISSUE I BRING TO YOU NOW
Is this:
I tested the values, manually. It all stores the values in the table correctly..
I can't seem to figure out what's wrong.
When I compare the collision manually through an event directly implementing the table variables it works.
But not through my collide method. I dont get why.

can ANYONE help me fix this issue?
Thank you.
~Tosharu
 
Have you searched the default scripts for the actual methods that determine when the player really "touched" an event? I guess those methods should give you a clue of what to do next... (Maybe to use some sort of alias_method...)

I don't know if this might help you...

$game_map.events[id].x or $game_map.events[id].y

In such case, what you should actually get is the respective event id...
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top