• Just a reminder that providing specifics on, sharing links to, or naming websites where ROMs can be accessed is against the rules. If your post has any of this information it will be removed.
  • Ever thought it'd be cool to have your art, writing, or challenge runs featured on PokéCommunity? Click here for info - we'd love to spotlight your work!
  • Our weekly protagonist poll is now up! Vote for your favorite Trading Card Game 2 protagonist in the poll by clicking here.
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

[Question] Scripting question about the use of "for loops" to simplify codes

  • 22
    Posts
    8
    Years
    • Seen May 15, 2023
    _Hi, I am trying to use this code:

    Spoiler:


    to simplify this code:

    Spoiler:


    But I always got Errors, what I am doing wrong?
    Maybe is a noob question but
    I cant understand well the use of "for loops"
     
    It's likely due to @mat and @cost not being an array (yet). So when you're trying to do @cost[1] it's not going to @cost1, it's trying to go to @cost[1] and the game is like "@cost[1] no exist, error, error"

    I'd suggest you do @mat = [] and @cost = [] before the code. and then it should work.
    Code:
    @mat, @cost = [], []

    There's also an 'error log' which greatly helps points to 'what the error is'. usually, if you post the error log as well, it narrows down all scenarios of 'what is wrong'.
     
    Last edited:
    It's likely due to @mat and @cost not being an array (yet). So when you're trying to do @cost[1] it's not going to @cost1, it's trying to go to @cost[1] and the game is like "@cost[1] no exist, error, error"

    I'd suggest you do @mat = [] and @cost = [] before the code. and then it should work.
    Code:
    @mat, @cost = [], []

    There's also an 'error log' which greatly helps points to 'what the error is'. usually, if you post the error log as well, it narrows down all scenarios of 'what is wrong'.

    Thank you, JulyArt, for that ultra fast help!
    I applied that lines that you suggested me and the previous error seem has gone.
    But now a new error is happening

    Spoiler:


    The script I am modifying is the "Itemcrafter1"
    And this is the line 480 mentioned in the Error Log:

    Spoiler:


    Here the complete script that i am trying to simplify (WIP)(mainly for learn how to use "for loops", and improve my coding skill):

    Spoiler:


    Thank you again!
     
    It's the same error actually.

    You see, because we're now using array for @mat[1] and @cost[1] but the script is looking for @mat1 and @cost2

    so you have two options. Either CTRL + Replace all the @mat1 to @mat[1] and so on. and same with @cost.

    or. you can alter your

    for i in 1..6
    a=i+1
    @mat=RECIPES[@item][a]? RECIPES[@item][a][0] : -1
    @cost=RECIPES[@item][a]? RECIPES[@item][a][1] : 0
    end

    to not set up @mat and @cost as arrays.

    If it was me, I'd likely do the latter, because this will keep the script 'intact' and be easier for you to upkeep the script with others. i.e. if the script updates by dev in the future, yeah. It's up to you though. Cause I can see advantages in modifying it as well.



    I was gonna write some code to get @mat to work with @mat1 but. Apparently, that's 'bad' coding. so just avoid that.
    you'd do it through 'eval' though. is the only way I was able to. It doesn't appear there's a simpler answer. Although if there is, that'd be quite useful.

    edit#1 : here's to bad coding. haha. (this don't work btw, it requires for the variable to be predeterminely set, which defeats the whole purpose)
    Code:
    eval "a#{1} = 9"
    for i in 1..6
    a=i+1
    eval'@mat#{i}=RECIPES[@item][a]? RECIPES[@item][a][0] : -1'
    eval'@cost#{i}=RECIPES[@item][a]? RECIPES[@item][a][1] : 0'
    end
     
    Last edited:
    I decided try to replace all the @mat1, @mat2, etc. for @mat[1]
    But I have to be doing things wrong:
    Now i am getting this error after i tried to apply this: ".to_i" to the "@mat"; (in some cases seems to be working)

    Spoiler:


    Another doubt i have is with this section i have improvised:

    Spoiler:


    I dont know if i did well defining this:
    r=[1,2,3]
    f=[1,3,5]
    g=[2,4,6]
    to achieve the effect i want

    Here is the full code, the commented lines are from the original script:

    Spoiler:


    Thank you!
     
    "Item_{i}" -> "Item_#{i}"

    I don't think you need the .to_i because the original script doesn't need to convert @mat1 to integer. unsure what's happening there regarding 'why'.

    clean up the parentheses though for easier readability for yourself.

    conFile(getID(PBItems,@mat1)) : "") -> conFile(getID(PBItems,@mat[1].to_i)) : "")
     
    Last edited:
    Back
    Top