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.

Odd unidentified method error

I'm trying to make a time reversing system similar to the one in Prince of Persia, or Braid.

basically, every frame it writes the players X and Y into a text file, and then when you press the "L" button (Q by default) it move the player to the X and Y position designated by the file, starting from the bottom.

It writes the coordinates into the file correctly, and seems to be reading them correctly as well, but when it tries to move the player it gets the error message:

"Script 'Game_Player' line 44: NoMethodError occured

undefined method `-' for #String:xxxxxxx>"

the x's at the end are random system numbers that I didn't feel like typing. The line of code it says errored is one dealing with centering the map. I didn't edit that at all.

here's the script:

#===============================================================================
#-------------------------------------------------------------------------------
# * Class: Time_Record
#-------------------------------------------------------------------------------
#===============================================================================

class Time_System
  def initialize
    @file = "time.txt"
    $time_file = File.open(@file, "r")
    $time_file.close
    end
  def update
    if Input.trigger?(Input::L)
      read_time
    else
      write_time
    end
  end
  def write_time 
    unless Input.trigger?(Input::L)
    $time_file = File.open(@file, "a")
    @player_x = $game_player.x
    @player_y = $game_player.y
    @player_x.to_s
    @player_y.to_s
    @input = @player_x
    $time_file.write(@input)
    $time_file.write("\n")   
    @input = @player_y
    $time_file.write(@input)
    $time_file.write("\n")
    $time_file.close
    end
  end
  def read_time
    $time_file = File.open(@file, "r")
    if $time_file.eof? == true
    $time_file.pos=0
    end
    @time_array = []
    @time_array = $time_file.readlines("\n")
    @temp = @time_array.size
    while @temp > 0
    @time_x = @time_array[@temp - 1]
    @time_y = @time_array[@temp - 2]
    @temp = @temp - 2
    reverse
    end
  end
  def reverse
    $game_temp.player_transferring = true
    $game_temp.player_new_map_id = $game_map.map_id
    $game_temp.player_new_x = @time_x
    $game_temp.player_new_y = @time_y
  end
  def file_clear
  $time_file = File.open(@file, "w")
  $time_file.close
  end
end

then in Scene_Title, under the "command_new_game" method I put "$time_system = Time_System.new"
and in Game_Map, under the "update" method I put "$time_system.update" , underneath "$game_screen.update"

I'm not sure what the error is, any help would be greatly appreciated!
 
You need to change this
Code:
    $game_temp.player_new_x = @time_x
    $game_temp.player_new_y = @time_y
to this:
Code:
    $game_temp.player_new_x = @time_x.to_i
    $game_temp.player_new_y = @time_y.to_i

Though for me it always teleports me to (7,9), so something's wrong there. Oh, and you need to do something about the file getting too big, like only keep a certain amount of entries. Also, it should probably update every time the player moves to remove lag, it doesn't need to add 5,5 a million times just because you're standing there. Also(lol), if it teleports you to your last position, with the way you have it now it will most likely teleport you to where you are, or just one tile over, I think. But only when you get that (7,9) problem fixed. ;)
Other than that, good job so far! Pretty innovative idea, hope to see it running smoothly sometime!

EDIT: Oh and here are some things...
It looks to be taking the x,y from the beginning of the file, not the end, so there's one problem. And change these lines too from
Code:
     @time_x = @time_array[@temp - 1]
     @time_y = @time_array[@temp - 2]
to this:
Code:
     @time_x = @time_array[@temp - 2]
     @time_y = @time_array[@temp - 1]
Otherwise it reads it wrong. So now you can teleport to the first set of coordinates in the file. Now you just need to get it to read the last ones instead, and it'll work right!(Or, at least sort of.)
TBH I think this might work better in a hash or something rather than an external file, since an external file, especially a simple text file, is easy to change, and a hash or array is easier to manage. But those are just my opinions, you can ignore me if you want. In any case, good luck with your game embooglement!
 
thanks for the help Tensuke! its weird, cause if you move the player starting position thats where it teleports you to, instead of (7,9).

I did notice how it fills up the text file fast, and I was going to fix that as soon as i got it to work properly. and i decided to use an external file because i have no idea how to use a hash. that, and a friend and i are trying to figure out how to do the same thing on Game Maker. if I can get it to work like this in RMXP, i know how to do it in Game Maker too.

But anyways, yeah, thanks for the help! at least it kind of works now.
 

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