RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
[RGSS2] Help making a script

0 Members and 1 Guest are viewing this topic.

**
Rep: +0/-0Level 69
RMRK Junior
I never made a script before and I'm not very good at it either, but I'd like to try (what I think is) a simple one which I also need and found nowhere else.

So the script is about having the character opacity set to X when it goes under a tile with priority 3 (above character).

So here's the script I tried to make:

Code: [Select]
class Game_Event < Game_Character
  if @priority == 3
  $game_character.opacity = 90
else
end
end

But as I expected, once I got in game and went under a tile set above character, it just didn't work =S

-EDIT-
I also tried:

Code: [Select]
class Game_Event < Game_Character
  if @flag == 0x10
  $game_character.opacity = 90
  else
end
end

but with no success, I have no idea what I'm doing wrong, well I guess the script is way too short to work after all xD I hope someone can point me to the right direction

« Last Edit: February 25, 2012, 06:57:30 PM by Darkxyn »

*
Rep:
Level 82
Because this code:

Code: [Select]
if @flag == 0x10
  $game_character.opacity = 90
  else
end

is part of the class itself and not a defined method, it gets executed immediately at compile time. Compile time starts when you hit the 'Test Play' button or run the game executable before anything is actually executed and the game starts.

At this point, the instance variables '@flag' and '@priority' are both nil. They have no value in them. So when the compiler reads the line:

Code: [Select]
if @priority == 3

It is really saying

Code: [Select]
if nil == 3

Which is false, because nil does not equate to 3. So the code within the if statement is never executed, and the program continues on.

Having code run at compile time can be a very important thing to make use of, but in this case it basically does nothing at all.

This code might be good* as part of the 'initialize' method after everything else has been initialized. This would be achieved by aliasing initialize:

*The point here is to demonstrate aliasing, rather than what you actually want to do. Going from what you are trying to achieve, this isn't actually a good place at all.

Code: [Select]
class Game_Event < Game_Character

  alias :alias_initialize_method :initialize
  def initialize
    #call the original initialize method by its alias
    alias_initialize_method
    #do my extra things here
    if @priority == 3
      $game_character.opacity = 90
    end
  end

end

Now, there's also a couple of other things that should be noted.

Firstly, $game_character doesn't actually exist in the first place, so you will get a NoMethodError as soon as the code hits "$game_character.opacity". If you want this opacity change to occur on the event itself, you can just use "@opacity = 90" instead. If you want, for whatever reason, the player character to have it's opacity changed you want to use "$game_player". You really don't want to do that, though; every time an event is created (which normally happens at the time of reading the map data and setting the current map up), the opacity of the player will be set to whatever event triggers that bit of code last. It would be unpredictable and, therefore, a difficult thing to manage.

Having the opacity of the player change when you go 'under' the event is easily achieve without a script in the first place. There's options in the 'Set Move Route...' command where you can change opacity of the player. Just set the event to trigger on "Player Touch".

Secondly, you don't need a floating 'else' if there's nothing 'else' to do. When the if fails, the code in else is executed. But when there's no code in else, it does nothing. So it might as well not actually be there.

If you really did want to use a script, as means of learning how to script, I'd actually say don't bother anyway with regards to this idea. There's far more simpler and easier things to learn scripting with than by basically repeating what the system can already do for you.

The way that I started (and I'm certain many other scripters did this too) is to take a script that already exists, copy it to a slot in materials and make changes to the script paying close attention to what you did and how it affected the game. An example can be changing the Window_Item class to have a different layout to the default, or changing Window_Menu to a new layout. By familiarising yourself with existing code, you can start to understand how it comes together to give the effect it does when you run the game. Then you can try to do some new and interesting things.

I don't want to sound like an ass, but I believe the right direction is what I've given you. You know why your code isn't working the way you was hoping it did, but I really can't justify to myself why it would be worth taking the time to demonstrate using a script to do what essentially can be done in less than 30 seconds already. It'll take a good 30 minutes for me to get started; it really just isn't worth the hassle.

I hope that's understandable, and I hope you still got something useful out of this post. I apologise for not being that little more helpful.
(Why do I always feel like it's the end of the world and I'm the last man standing?)

***
Rep:
Level 84
Yes, hoh my gawd!
You'll want to place your code in Game_Player class or alias the "update" method for Game_Player. And to accomplish what you want, you'll also have to overwrite the screen_z method in Game_Player to make the player "always on top". This way it gets drawn over everything.
Code: [Select]
class Game_Player < Game_Character
    alias update_player_old update
    def update
        update_player_old
        if @priority >= 3
            @opacity = 90
        end
    end
    def screen_z
        return 1000
    end
end

Now I don't know how RMVX handles priorities so I can't help you with that so this code won't work 100%. I don't mean to spoon feed anyone, more or less trying to give my 2 cents on what you have to do exactly.

**
Rep: +0/-0Level 69
RMRK Junior
Oh sorry xD maybe I wasn't so much clear, I actually needed it both for events and tiles, for events maybe not so much because as you said, that can be evented.

Anyway you were REALLY helpful, I really appreciated the fact you wrote so much just to help me and point me to the right direction and also explaining me why it didn't work! =)

About the way you started, it's true, eventually by reading tons of tutorials, some of them suggested what you did and it's infact what I'm doing and it really helps =).

And I put a floating else because I thought if the "IF" code was wrong it would be skipped so not to crash the whole game xD

Again, thanks for spending time just to help me, I'm actually going to save the message so next time I can read it again and take some of the suggestions you gave me (I'm pretty forgetful xD).

Gameguy you were really useful too *copies what you said*, later on I'll try both suggestions and see what I can do, probably won't get to do what I want but I surely learnt a lot by reading you guys' replies and I hope to learn more by practise!
Thanks =)

***
Rep:
Level 70
RMRK Junior
New to scripting myself, but I dont like that code because it doesnt account for the methods already being defined.  IE hitting F12 a couple times (on some scripts) can cause a Stack Too Deep Error.  Hitting F12 makes an alias of the alias which calls the alias insteand of the original def.

Im trying to build good habits and I believe this is a god one to have.  Check if the Alias is already defined or not.

Code: [Select]
class Game_Player < Game_Character

  # Test if the Alias already exists (it will if you hit F12 or Reset)
  unless self.method_defined?('update_player_old')
    alias update_player_old update
  end

  def update
    update_player_old
    if @priority >= 3
      @opacity = 90
    end
  end

  # Since this is not aliasing, I dont think it needs to be checked
  def screen_z
    return 1000
  end

end


Now, that is if I understood my lessons correctly...
Heretic's Vehicles XP (Boat and Magic Carpet)

Heretic's Collection XP Ver 2.3 - Updated to include Dynamic Lighting, Moving Platforms, Vehicles, and much much more!

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Most Mature Member2012 Favorite Staff Member2012 Best RPG Maker User (Scripting)2012 Best MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
The error you are concerned about will only occur if you are aliasing an inherited method or a method in a hidden class. See http://rmrk.net/index.php/topic,43457.msg494683.html#msg494683