[XP] My first script [Resolved]

Started by mroedesigns, January 06, 2011, 06:50:28 am

Previous topic - Next topic

mroedesigns

January 06, 2011, 06:50:28 am Last Edit: January 08, 2011, 08:20:30 am by mroedesigns
So I'm working on a script for my online game that will allow players to have items that will teleport them to certain locations. It's my first script, so I expected to run in to problems. However, when I ran the game and tried it out I got no errors. It did nothing, but I got no errors. And in a way that's worse than getting an error.. at least then I know whats wrong  :^_^':

If somebody could help me out with this it'd be awesome. I based it off of the [XP] Town Scrolls script that was posted on here.

This is the script
Spoiler: ShowHide
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# Teleportation Items by MRoeDesigns
# Version: 0.1
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#
# Instructions ::
#
# This script is used to create items that players can use to
# be teleported back to certain areas.
#
#
# Script Calls ::
#
# TItem.SetLocation(ID)
#  - Sets the location ID for the item
#
# TItem.Location?
#  - Returns the players location ID
#
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=


if !defined?(RMXOS) || RMXOS::VERSION < 1.09
 raise 'ERROR: The "Teleport Item" script requires RMX-OS 1.09 or higher.'
end

class  TItem
 
 def initialize
   @LocationID = 0
 end
 
 def self.SetLocation(id)
   @LocationID = id
 end
 
 def self.Location?
   return @LocationID
 end
 
 def self.Teleport
   
#   Change the Item Number here to reflect your teleport item
   if $game_party.item_number(34) == 1
     
     case TItem.Location?
   
     when 0
#      These values reflect where the player will be teleported to.
      $game_temp.player_transferring = true
      $game_temp.player_new_map_id = 28
      $game_temp.player_new_x = 8
      $game_temp.player_new_y = 8
      $game_temp.player_new_direction = 0
      $scene = Scene_Map.new
      $game_map.autoplay
      return true
     
     when 1
      $game_temp.player_transferring = true
      $game_temp.player_new_map_id = 28
      $game_temp.player_new_x = 5
      $game_temp.player_new_y = 5
      $game_temp.player_new_direction = 0
      $scene = Scene_Map.new
      $game_map.autoplay
      return true
     
     end

   end
 end
end


and the item runs a common event that runs this script "TItem.Teleport"

Sorry if I'm missing something obvious, this is the first time I've ever scripted in ruby   :shy:

mroedesigns

Bump?  :ninja:

Anybody have any ideas? As I said this is my first script, so I'm not 100% familiar with the syntax of method calls and that sort of thing.

Another thing to note; using the item displays a 'Miss' every time it's cast [using blizz-abs of course] even though it has a hit rate of 100%.
No other parameters are set in the database entry for the item, and I didn't modify anything in the blizz-abs config. Im guessing it has something to do with blizz-abs somehow recognizing that the script is wrong, so instead of crashing it just forces a miss? Not sure.

gerrtunk

January 08, 2011, 06:37:26 am #2 Last Edit: January 08, 2011, 06:45:18 am by gerrtunk
-¿Is your code actually working? If you dont instantiate that class it wont do nothing. You
have to use static one that can be called without being instantanted, try with ruby modules.


-Is better to add that methods to game_party, for example.

-Have you modified the item_effect or the method that uses the blizz to check for items effects when used? If not it dont have a chance, no?

-Also dont use the method setLocation, or the if, or the class for titems. You dont have a item class to be your parent or nothing. Create a static array with all the ids of the items and make the if like this:

if array.includes? item_id

I suggest using modules and hashes:

module Mro
Tel_items = {
1 => 1,
35 => 4
}
end

if Mro::Tel_items[item_id] != nil
check for use



-Dont copy all the code in every case. Just set a pre list of default variables and set the ones you need
in each when:

      $game_temp.player_transferring = true
      $game_temp.player_new_map_id = 28
      $game_temp.player_new_x = 5
      $game_temp.player_new_y = 5
      $game_temp.player_new_direction = 0
      $scene = Scene_Map.new
      $game_map.autoplay
     case TItem.Location?
   
     when 0
#      These values reflect where the player will be teleported to.
      $game_temp.player_new_x = 8
      $game_temp.player_new_y = 8
      return true
when 1
return true

mroedesigns

January 08, 2011, 07:46:02 am #3 Last Edit: January 08, 2011, 08:20:11 am by mroedesigns
First Post ::
Spoiler: ShowHide
Quote from: gerrtunk on January 08, 2011, 06:37:26 am
-¿Is your code actually working? If you dont instantiate that class it wont do nothing. You
have to use static one that can be called without being instantanted, try with ruby modules.

I'm pretty sure it's not working, that's why I made the topic. Where do I need to initialize the class? I have a def_initialize, but I wasn't sure if I need to call it before using the class or what. I'm new to RGSS scripting so I'm unfamiliar with exactly how the structure is set up.


Quote from: gerrtunk on January 08, 2011, 06:37:26 am
-Have you modified the item_effect or the method that uses the blizz to check for items effects when used? If not it dont have a chance, no?

I did realize that I had the scope set incorrectly, thank you! Now the item doesn't 'miss' like it was before, but it still does nothing. No errors, no changes.


Quote from: gerrtunk on January 08, 2011, 06:37:26 am
-Also dont use the method setLocation, or the if, or the class for titems. You dont have a item class to be your parent or nothing.

So I should just change everything to a method? Hmm.


Quote from: gerrtunk on January 08, 2011, 06:37:26 am
Create a static array with all the ids of the items and make the if like this:

if array.includes? item_id

I suggest using modules and hashes:

...


But there's only one Item that's used for teleportation.. so why use an array?


Quote from: gerrtunk on January 08, 2011, 06:37:26 am
-Dont copy all the code in every case. Just set a pre list of default variables and set the ones you need
in each when:
...


I do need those other variables though, because depending on the location id, it will teleport players to different maps. The only reason they were the same map in the script I posted was because I'm testing it, so I just tried to teleport to a different location on the map.


Edit ::
I put my old post in a big spoiler, because I've got the script working! Thanks Gerrtunk for your help. I'm going to go post this in the scripts section in case anyone else can use it.

gerrtunk

January 08, 2011, 08:29:08 am #4 Last Edit: January 08, 2011, 08:40:20 am by gerrtunk
¿What language you know? Is to think in what you know about programing.

Normally its better do not instantiate nothing. Thats because if you do that you have to modifiy the
scene_title to load that class, for example, and create ir globally. That sucks. If you want to use permament data for example, if you do your won class you have to modifiy the save system also.

Is better to use a module or modify a default script. Ruby modules are totally static classes. That mean that you can access its
constants variables or methods without instantiating them. If you dont need the array i suggest avoid that, because you cant modifiy its constants.

Then, dont use that class. I suggest you extending one of that global objects that already exist. Game party is the one is more interesting for you.



class Game_Party
 
  attr_accessor    :telep_item_location
  alias wep_gp_mroTel_init initialize
  def initialize
    wep_gp_mroTel_init
    @telep_item_location = 0
  end
 
 def apply_tel_item
   
  if $game_party.item_number(34) == 1
     
     case @telep_item_location
   
     when 0
#      These values reflect where the player will be teleported to.
      $game_temp.player_transferring = true
      $game_temp.player_new_map_id = 28
      $game_temp.player_new_x = 8
      $game_temp.player_new_y = 8
      $game_temp.player_new_direction = 0
      $scene = Scene_Map.new
      $game_map.autoplay
      return true
     
     when 1
      $game_temp.player_transferring = true
      $game_temp.player_new_map_id = 28
      $game_temp.player_new_x = 5
      $game_temp.player_new_y = 5
      $game_temp.player_new_direction = 0
      $scene = Scene_Map.new
      $game_map.autoplay
      return true
     
     end

   end
 end
 
end



In ruby you dont have to use set or get methods if you dont need something special. It make it automatically for you calling attr_reader   :variable (for reading only), attr_writter   :variable (for writing only) and attr_accessor   :variable (both)

So now you can access your location variable with $game_party.telep_item_location and its still protected and encapsulated.

Also i dont know if you know what aliasing is. Its technique that overrides a method and set the original with a alias. Is useful for compatibilty and quick development. In this way you can set your variable and then call the aliased initialize method so it will add all you need without a problem. Its also better because any number of scripts can aliase consecutively this method without a problem of compatibility.

You set it with script calls. Then, you need to call that apply method in the item effect one, or in the place where its set the apply items for blizz.

Anyway, if you want to release it well, it suggest adding the teleport item id in a constant in a module. Its better than having the user move throught the script.


ForeverZer0

Quote from: gerrtunk on January 08, 2011, 06:37:26 am
I suggest using modules and hashes:

module Mro
Tel_items = {
1 => 1,
35 => 4
}
end

if Mro::Tel_items[item_id] != nil
check for use



Do not use this technique. That is not good coding practice at all. There are far better ways to do the same thing that are easier to script and easier for anyone who uses your script to configure.  Doing that would be a very n00b thing to do. Try something more like this:


module Mro
  def self.tele_items(item_id)
    return case item_id
    when 1 then 3
    when 2 then 69
    when 435 then -4
    else
      0
    end
  end
end


This not only looks better, but is better performance-wise as well.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

mroedesigns

Quote from: ForeverZer0 on January 08, 2011, 02:00:36 pm
Quote from: gerrtunk on January 08, 2011, 06:37:26 am
I suggest using modules and hashes:

module Mro
Tel_items = {
1 => 1,
35 => 4
}
end

if Mro::Tel_items[item_id] != nil
check for use



Do not use this technique. That is not good coding practice at all. There are far better ways to do the same thing that are easier to script and easier for anyone who uses your script to configure.  Doing that would be a very n00b thing to do. Try something more like this:


module Mro
  def self.tele_items(item_id)
    return case item_id
    when 1 then 3
    when 2 then 69
    when 435 then -4
    else
      0
    end
  end
end


This not only looks better, but is better performance-wise as well.


I don't need to use either of those. For the second time, there is only ONE item that uses teleportation, so there is absolutely no need to check multiple items.

ForeverZer0

For the first time, its a good idea to know that.

It is a good idea if you ever want to release a script, someone else may want to use it slightly different than you.
It doesn't hurt to have the ability there from the start, seeing that it doesn't hurt anything.

Just a suggestion.  ;)
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

gerrtunk

January 08, 2011, 08:53:29 pm #9 Last Edit: January 08, 2011, 08:55:42 pm by gerrtunk
Quote from: ForeverZer0 on January 08, 2011, 02:00:36 pm
Quote from: gerrtunk on January 08, 2011, 06:37:26 am
I suggest using modules and hashes:

module Mro
Tel_items = {
1 => 1,
35 => 4
}
end

if Mro::Tel_items[item_id] != nil
check for use



Do not use this technique. That is not good coding practice at all. There are far better ways to do the same thing that are easier to script and easier for anyone who uses your script to configure.  Doing that would be a very n00b thing to do. Try something more like this:


module Mro
 def self.tele_items(item_id)
   return case item_id
   when 1 then 3
   when 2 then 69
   when 435 then -4
   else
     0
   end
 end
end



This not only looks better, but is better performance-wise as well.


I dont think the case when is better. It sucks when compacting large scripts, ¿how many methods i have to create then? Also i dont think they are more readable for scripters, and writing that entire code may be slowest.

Also they dont work when you have to create complex data to return.

It can be better performance but thats not important. Im not programing in ruby or usign rpgmaker for perfomance, and it wont make the difference.

Sorry but i dont think its a noob practice. Also NOOB is a despective word that i hate. A newbie practice will be to use global variables or nothing at all and create it in the same line in the code.

ForeverZer0

QuoteI dont think the case when is better.

Luckily your opinion doesn't change the facts. The sample I provided is better. Maybe you should research how data, specifically hashes, memory is stored and referenced, let alon it looks like a n00b wrote it.

QuoteIt sucks when compacting large scripts, ¿how many methods i have to create then?

You would create the exact same number of methods as you would hashes.

QuoteAlso i dont think they are more readable for scripters,...

I can only make an opinion for myself, but a blind person can see the difference.

Quote...and writing that entire code may be slowest.

Wouldn't want you to hurt youself typing a few extra words to make a better script.

QuoteAlso they dont work when you have to create complex data to return.

You just drop the return from the beginning.

case @n00b
when gerrtunk
  call_some_methods
  make_some_calculations
  argue_over_common_sense
else
  do_something_else
end


QuoteIt can be better performance but thats not important. Im not programing in ruby or usign rpgmaker for perfomance, and it wont make the difference.

What!? You should strive for best performance in a script or any other language, no matter what the application. There is rarely any ONE thing that kills performance. It is a combination of milliseconds throughout all your code that makes the difference of lag and not. By keeping to a minimum as you go, you improve performance, though I don't understand how you don't care about that, and try to code anything.

QuoteSorry but i dont think its a noob practice.

(see responce to first quote)

QuoteAlso NOOB is a despective word that i hate. A newbie practice will be to use global variables or nothing at all and create it in the same line in the code.

Yes, that would be one example. Yours is another.


I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

mroedesigns

Woah guys O.o

It doesn't matter anyway, the script works, and for now that's good enough for me hah

gerrtunk

January 08, 2011, 09:26:18 pm #12 Last Edit: January 08, 2011, 09:28:36 pm by gerrtunk
Quote from: ForeverZer0 on January 08, 2011, 09:17:52 pm
QuoteI dont think the case when is better.

Luckily your opinion doesn't change the facts. The sample I provided is better. Maybe you should research how data, specifically hashes, memory is stored and referenced, let alon it looks like a n00b wrote it.

QuoteIt sucks when compacting large scripts, ¿how many methods i have to create then?

You would create the exact same number of methods as you would hashes.

QuoteAlso i dont think they are more readable for scripters,...

I can only make an opinion for myself, but a blind person can see the difference.

Quote...and writing that entire code may be slowest.

Wouldn't want you to hurt youself typing a few extra words to make a better script.

QuoteAlso they dont work when you have to create complex data to return.

You just drop the return from the beginning.

case @n00b
when gerrtunk
 call_some_methods
 make_some_calculations
 argue_over_common_sense
else
 do_something_else
end


QuoteIt can be better performance but thats not important. Im not programing in ruby or usign rpgmaker for perfomance, and it wont make the difference.

What!? You should strive for best performance in a script or any other language, no matter what the application. There is rarely any ONE thing that kills performance. It is a combination of milliseconds throughout all your code that makes the difference of lag and not. By keeping to a minimum as you go, you improve performance, though I don't understand how you don't care about that, and try to code anything.

QuoteSorry but i dont think its a noob practice.

(see responce to first quote)

QuoteAlso NOOB is a despective word that i hate. A newbie practice will be to use global variables or nothing at all and create it in the same line in the code.

Yes, that would be one example. Yours is another.





Im not going to respond. I doubt in person you call me that im a  'noob'' again ;).

Quote
What!? You should strive for best performance in a script or any other language, no matter what the application.

Oh yeah! Totally true. Now im understand. Im going to rewrite all my ruby scripts in C and call them using dlls. No matter the application, man...

Im a lazy bastard. Fuck me.

ForeverZer0

I'm not gonna argue. We are off topic here. I don't understand why you are being purposefully ignorant, but whatever.
Oh, yeah, it must be this.
Spoiler: ShowHide
 :n00b:

I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

gerrtunk

January 08, 2011, 10:02:27 pm #14 Last Edit: January 08, 2011, 10:04:17 pm by gerrtunk
¿Purposely ignorant? ¿? I have say a thing that isnt true? I have say that a hash is more quick than a case or something?

The problem is yours.

You come here insulting me,i ask you for a explaniation and you attack me more and more. Im not want to argue with a person that attack me only for contradict you.

In the streets it will be a other history. Is tipical that people encourage and attack using internet security and anonymity.

ForeverZer0

I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

A hash will be by about 20% faster than a method like that. But that is irrelevant because 100000 accesses take for a method 0.36 seconds while they take 0.28 seconds for a hash. That means that one access per frame is close to nothingness.

The difference is not in the code but in practical appliance. People that are unfamiliar with scripts and configurations understand using a case-when-return configuration method significantly easier than a hash. Not everybody is a mathematician that X => Y makes sense to them.

gerrtunk, I hope that one day you will let of your lack of inability to learn or lack of willingness to learn. One day you may understand that fastest is not always the best and that not only usability but pretty much everything (resuability, system structure, userfriendlieness, etc.) is opposed to speed. I gladly sacrifice 0.1 ms in order to prevent people asking me over and over how to configure my script because they simply don't understand it.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

mroedesigns

QuoteWe should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil
- C. A. R. Hoare


:V

gerrtunk

Quote from: Blizzard on January 09, 2011, 05:21:13 am
A hash will be by about 20% faster than a method like that. But that is irrelevant because 100000 accesses take for a method 0.36 seconds while they take 0.28 seconds for a hash. That means that one access per frame is close to nothingness.

The difference is not in the code but in practical appliance. People that are unfamiliar with scripts and configurations understand using a case-when-return configuration method significantly easier than a hash. Not everybody is a mathematician that X => Y makes sense to them.

gerrtunk, I hope that one day you will let of your lack of inability to learn or lack of willingness to learn. One day you may understand that fastest is not always the best and that not only usability but pretty much everything (resuability, system structure, userfriendlieness, etc.) is opposed to speed. I gladly sacrifice 0.1 ms in order to prevent people asking me over and over how to configure my script because they simply don't understand it.


But if i am the one that was saying that speed its not the important thing. Read it again. If we are using ruby or rgss from the start its also because we dont care about a complete efficience.

Well, also hashes uses more memory, no?

Anyway im not closed to learn, im open to argue. The problem is with zero calling me noob just for having a diffferent opinion. And calling me that again after warning he. Im not going to argue with peopple like he.

I have noted the case idea. That was the first thing i used and well... i just get tired in the eternal configurations because that methods take a long space and are worse to configure.

Using hashes sintax is pretty straightforward. I doubt that really make the difference. variable name => variable value. The symbol is not hard and in a few seconds a person can get it. The case when structure also needs explanations so its the same just taking more time to script, space, etc...



Blizzard

January 09, 2011, 08:20:29 am #19 Last Edit: January 09, 2011, 08:22:28 am by Blizzard
Quote from: gerrtunk on January 09, 2011, 07:21:58 am
Using hashes sintax is pretty straightforward.


That is not correct. It is straightforward for me and you, for programmers. It even may be straightforward for a mathematician. For the average person it is not straightforward.

Quote from: gerrtunk on January 09, 2011, 07:21:58 am
The symbol is not hard and in a few seconds a person can get it.


Yes, the symbol is not hard. The problem is not in the symbol, the problem is in the high level of abstraction. Words makes a lot more sense to the non-programmer than symbols because words are "symbols" they already know the meaning of.

Quote from: gerrtunk on January 09, 2011, 07:21:58 am
The case when structure also needs explanations so its the same just taking more time to script, space, etc...


Yes, both need an explanation. But "when X then return Y" or "when X then Y" makes a lot more sense for the average person than "X => Y" because people are used to language, they have been using it for years.

Quote from: gerrtunk on January 09, 2011, 07:21:58 am
i just get tired in the eternal configurations because that methods take a long space and are worse to configure.


That is also not entirely correct. Yes, methods do take longer to write up initially, but they are significantly easier to configure. 5 years of experience have shown me that, including many testimonials of the most various people. People liked to configure everything using words than symbols and they have praised it many times. Phrases like "Wow, this when-then thing is so less confusing than Seph's stupid hash arrow things or whatever they are. And they make so much more sense as well." are very common when I first started using this method of configuration.

Have you tried using a functional language like Haskell? Try it out. It will be very confusing for you because you are not familiar with this kind of thinking. Every mathematician I know swears on functional languages and they all claim that they are much easier to understand than programming languages. I disagree. They are easier to understand for them because they are used to that way of thinking, the thinking of formulas and statements rather than commands.

As I said, a hash makes more sense for a programmer, but not for the average person. Failing to understand that the average person does not think like you at all will hinder you in your progress as a successful programmer and as a person in general. You are the programmer here. It is not your job just to write the script, it is also your job to make your script more accessible to other programmers who want to understand and learn from your work as well as making your script as user-friendly as possible because otherwise people will not use your script or will choose a more user-friendly script over yours. Sometimes sacrifices have to be made.

Honestly, if I have to spend one more minute to type down a method instead of a hash that will save somebody else minutes, even hours, I'd gladly do it. Especially if it will save me time as well later because I won't have to waste time on explaining again how to configure a hash. And it is not helpful to just say "But it is so simple, so straightforward, they just have to understand it, they will understand it!" No, they won't. You can't force understanding on them, you can't force your world on them. If that would work in our world, computers would still have no graphical user interface, all games would still be text games and there would be no cars with automatic gear shifting. It is your job to make their life easier.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.