Jump to content
Sign in to follow this  
Saru

Variable in Script

Recommended Posts

Hello,

 

Right now I try to make a condition branch for variable in a script, but I can't get it to work. 

 

The picture show what I have right now. When I start the game, I get

 

"NoMethodError occured.

 undefined method `[]' for nil:NilClass"

 

Can somebody help?

 

Thanks in advance

 

post-68602-0-65708400-1497627748_thumb.jpg

post-68602-0-55903400-1497655297_thumb.jpg

post-68602-0-88354000-1497697448_thumb.jpg

post-68602-0-41564200-1497708491_thumb.jpg

Edited by Saru

Share this post


Link to post
Share on other sites

Simple.

 

Consider sample code.

# Case 1
def setup
  $array = Array.new(1000, nil)
end
 
$array[50] == 5
 
setup
 
#=> error since the array doesn't exist until we call setup
# Case 2
 
def setup
  $array = Array.new(1000, nil)
end
 
def variable
  $array[50] == 5
end
 
setup
 
#=> If we choose to call the method later on
# there will be no problem since the variable
# has been initialized by setup.
Nothing really exists until the main process runs. Just put your variable inside a method and it will work fine when you call it inside an event or whatever.  ;)

 

Share this post


Link to post
Share on other sites

First of all, thank you for the fast answers. I tried to do as said, but now I get a "NameError. Undefined local variable or method "var" "

 

Version 1:

  
def var
    $game_variables[981] 
    $game_variables[982]
end
 
var
 
Version 2:
 
def var
    a = $game_variables[981]
    b = $game_variables[982] 
end
 
var
 
 
 
that's what I wrote before the script-part you can see in the image of the main-post. I don't know that much about scripting, I'm still a beginner.
 
Edit: Maybe I should tell that this script is inside of an already existing script I found on the internet and wanted to use. It's inside  the "module", before the first class or anything else (I would add a picture, but somehow I can't upload it to this post, so I will add it to the main post)
Edited by Saru

Share this post


Link to post
Share on other sites

I'm just starting to learn coding myself, but this is what I think it should be.

 

class this script

def varcheck1

varcheck1 = $game_variables[981]

end

 

def  varcheck2

varcheck2 = $game_variables[982]

end

 

case varcheck1

 when 0

  case varcheck2

    when 1  

       Picture_name = "this"

    when 2

 ...

    end

 else

  case varcheck2

    when 1  

       Picture_name = "other"

    when 2

 ...

    end

 end

 

end

Share this post


Link to post
Share on other sites

I'm just starting to learn coding myself, but this is what I think it should be.

 

class this script

def varcheck1

varcheck1 = $game_variables[981]

end

 

def  varcheck2

varcheck2 = $game_variables[982]

end

 

case varcheck1

 when 0

  case varcheck2

    when 1  

       Picture_name = "this"

    when 2

 ...

    end

 else

  case varcheck2

    when 1  

       Picture_name = "other"

    when 2

 ...

    end

 end

 

end

 

Thanks for the answer, but sadly it didn't work. Probably because it's in the module-part of the script (but you couldnt know, didn't share that information before you answered)

Share this post


Link to post
Share on other sites

 

First of all, thank you for the fast answers. I tried to do as said, but now I get a "NameError. Undefined local variable or method "var" "

 

Version 1:

  
def var
    $game_variables[981] 
    $game_variables[982]
end
 
var

Sorry but I think you missed the point

 

the module should look something like this:

 

module Zvart_menu

 

  def var

     case $game_variables[981]

     when 0

       case $game_variables[982]

       when 1

         picture_name = 'actorm1'

       when 2

         picture_name = 'actorm2'

       when 3

         picture_name = 'actorm3'

       end

    else

      case $game_variables[982]

      when 1

         picture_name = 'actorw1'

      when 2

        picture_name = 'actorw2'

      when 3

        picture_name = 'actorw3'

      end

    end

  end

end

 

 

If you go to the bottom of the script list, you will see a tab called 'Main', please read the header. Bellow that there is a command

rgss_main { SceneManager.run }

 

Nothing exist before this command is run. If you where to call the module immediately after defining it, the game would crash anyways because it reference an object that doesn't exist.

Share this post


Link to post
Share on other sites

Alright. I now Changed my script as said, but It gives me a "dynamic constant assignment"-Error.

(The Variable-Value is defined at the start of a new game, if this helps)

 

Also (so that I don't have to ask later): Where exactly do I call the metthod?

Share this post


Link to post
Share on other sites

If you look at my code it 'picture_name' is not capitalized. In ruby if you capitalize then it is defined as a constant. When your module tries to change that constant we have a problem since you aren't allowed to change the value of a constant.

 

Where to call it depends on what you will use it for.

Share this post


Link to post
Share on other sites

The script is for a new menu (a single actor menu to be precise). The code we are talking about is checking, which character-model is chosen in the character-selection and puts a matching picture of the actor on the menu screen (when you press esc, the menu is called. On the left are the submenues (items, equipment.,...), on the right is a picture of the actor, a hp-bar,...

(reference-picture in the main post) 

 

Changing from capitalised to uncapitalised did fix the other error, thank you for that.

 

But now I somehow have to get it back to a constant, because later on "PICTURE_NAME" is used in a method, and changing it to "picture_name" doesn't work.

Edited by Saru

Share this post


Link to post
Share on other sites

There problem is I don't know how (I'm still a beginner, as I said).

 

I will give another picture (this time from the lines where the actor-picture is drawn). Maybe you can tell me what to do.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×