My first script - little help?

Kryterion

Villager
Member
Joined
Sep 26, 2012
Messages
32
Reaction score
4
Primarily Uses
OK, so this is my first attempt at a script. The purpose of it is to display a picture from the item menu. This would allow you to pick up items like books, carry it around with you and view the contents whenever you want.

But I can't get it to work! I've been fiddling around with this for hours and I can't see what I'm doing wrong so I'm throwing it over to the community.

This is a rough "first draft" so forgive me if it's not well-written. Once I've got it working I'll make it nice and elegant (and probably add more functionality too).

class RPG::BaseItem def check_picture_tag? #method to check if there is a picture to be displayed. /picture/i =~ @note end def get_pictures_to_display # Method to get the filename of the picture to display. arr = [] n = /<(?:pICTURE|picture):[ ]*"(\S+)">/i @note.split(/[\r\n]+/).each { |line| case line when n p "match found OK" #get contents of note that comes between "..." $1.scan(/"(\W+)"/).each { |filename| p filename.to_s arr.push(filename.to_s) } #return as an array of strings so each can be displayed in order end p arr } return arr end end# define a new version of scene_item which calls the above when an item is usedclass Scene_Item < Scene_ItemBase def on_item_ok if item.check_picture_tag? pictures_to_display = item.get_pictures_to_display display_picture(pictures_to_display) else $game_party.last_item.object = item determine_item end end def display_picture (pictures_to_display) p pictures_to_display pictures_to_display.each { |pic| @pic = Sprite.new @pic.bitmap = Cache.picture(pic) #add in some way of pausing til user presses enter (refresh method) } end endTo get it to work, you're supposed to create an item from the manager and add the notetag <picture: "filename"> where filename is the name of an image in the game's picture folder. Once you possess the item you should be able to activate it from the item menu and it displays the picture. However, nothing is returned. As far as I can tell, the method get_pictures_to_display just returns an empty array.

If anyone can point me in the right direction I'd be very grateful!
 

Warpmind

Twisted Genius
Veteran
Joined
Mar 13, 2012
Messages
936
Reaction score
578
First Language
Norwegian
Primarily Uses
I'm not sure, but shouldn't there be some coordinates for where you want the picture displayed? I can't see anything in the script...
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
What do you mean, "carry it around with you"? Is there supposed to be a picture on the screen that follows you wherever you walk, showing that you have the item?


If not, why don't you just do it through events? When you "pick up" the item, turn on a self switch and add a new page conditioned by the switch with no sprite, so it no longer appears on the map. Add the item to your inventory. Then attach a common event to the item so when you use it from the menu it does a show picture, do a Show Text with no text to display and a blank background (to wait for the player to press enter), then erase picture.


If that's not what you want, you need to explain in more detail how it works. Giving us the script won't tell us what you want it to do, as it's obviously not doing what you want it to do. Tell us what you're trying to achieve (in detail) and we'll be able to show you what you've missed.
 

Kryterion

Villager
Member
Joined
Sep 26, 2012
Messages
32
Reaction score
4
Primarily Uses
do you mean like

    @pic = Sprite.new(1,1) ?

I did have it in there originally but I removed it. I made another script that didn't use it and still worked so I wasn't sure it was necessary..?
 

Kryterion

Villager
Member
Joined
Sep 26, 2012
Messages
32
Reaction score
4
Primarily Uses
Shaz

What do you mean, "carry it around with you"? Is there supposed to be a picture on the screen that follows you wherever you walk, showing that you have the item?

If not, why don't you just do it through events? When you "pick up" the item, turn on a self switch and add a new page conditioned by the switch with no sprite, so it no longer appears on the map. Add the item to your inventory. Then attach a common event to the item so when you use it from the menu it does a show picture, do a Show Text with no text to display and a blank background (to wait for the player to press enter), then erase picture.

If that's not what you want, you need to explain in more detail how it works. Giving us the script won't tell us what you want it to do, as it's obviously not doing what you want it to do. Tell us what you're trying to achieve (in detail) and we'll be able to show you what you've missed.
I guess I could do that but part of this is learning to code/script so it's not so much the end result as it is the learning process. Although to answer your question more fully, I'm using this as a starting point as I'm creating a magic system for my game that would use this feature.

It sounds like you understand what I'm trying to achieve anyway.
 

Evgenij

Veteran
Veteran
Joined
Aug 28, 2013
Messages
349
Reaction score
100
First Language
German
Primarily Uses
N/A
Maybe this can help you:

Code:
class RPG::BaseItem    def check_picture_tag?    # if the array is empty there are no pictures to display    !get_pictures_to_display.empty?  end    def get_pictures_to_display    # scan returns an array of all matches to that regex    # so there is no need for the multiline thing you    # wrote.     # Because the matches are in their own array, the    # main array needs to be flatten.    #    # You can remove .flatten and print out the array to    # see what I mean.    #    # You also don't need to do the PICTURE|picture things,    # cause /i ignores case.    @note.scan(/<picture:\s*"(\S+)"\s*>/i).flatten  end
 
Last edited by a moderator:

Kryterion

Villager
Member
Joined
Sep 26, 2012
Messages
32
Reaction score
4
Primarily Uses
Hi Evgenij, thanks for your reply. I'm a little embarrassed - you were able to do in a single line something I tried to do with a whole block! Thanks though.

I now have this working.... more or less. The problem I'm having is that if the array contains more than 1 picture, my code is causing the array to be overwritten with whatever the last image is. Here's the offending block:

def display_picture (pictures_to_display) pictures_to_display.each { |pic| $current_picture = pic SceneManager.call(Scene_ShowPic) } endNow, I've worked out what's happening here. When it hits the .each loop, it executes the whole block of code but stops when it reaches the SceneManager.call (which in this case is only a single line), then it returns to the top, and repeats that for every element in the array. Then *after* that's happened, it then calls SceneManager once for each item in the array, but only shows the last picture each time. So,if for example the array contains 3 images, it does this:

- make picture 1 the current picture

- make picture 2 the current picture

- make picture 3 the current picture

- show current picture

- show current picture

- show current picture

When I want it to do this:

- make picture 1 the current picture

- show current picture

- make picture 2 the current picture (etc...)

I was wondering if anyone can tell me (a) why it's doing this and (b ) how to get around this behaviour!

Thanks again....
 
Last edited by a moderator:

Evgenij

Veteran
Veteran
Joined
Aug 28, 2013
Messages
349
Reaction score
100
First Language
German
Primarily Uses
N/A
So just a scene where all pictures in the array are displayed after each other. Should the next picture be displayed when the player hits a key?
 

Kryterion

Villager
Member
Joined
Sep 26, 2012
Messages
32
Reaction score
4
Primarily Uses
yes, the picture displays and then returns when the player hits a key.

here's the scene it calls.

Code:
class Scene_ShowPic < Scene_Base    def start    super    make_picture  end    def update    super    if Input.trigger?(:C)      return_scene    end  end    def make_picture    image = $current_picture.to_s    @pic = Sprite.new    @pic.bitmap = Cache.picture("#{image}")  end  end
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,162
Reaction score
822
First Language
Hungarian
Primarily Uses
RMVXA
Make $current_picture an array, place every pic in that iteration into that array.


Call the scene after that, make the update method of that scene, so the first pic shows until the player press a button, than dispose the first pic, show the next, and so on.


There is no reason to call the same scene 3 times with close to same data, it is better to organize your data and call your scene after that with all of your data at once, than make a method to do something with that data.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,865
Messages
1,017,059
Members
137,574
Latest member
nikisknight
Top