Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

I want to regain SP while walking

Somebody, please help. I'm not good enough with RGSS. I tried to solve this myself but I don't know how to do it. I know I must edit the game_map script... (if somebody can help, can you also tell me how to make the game give exp to the actors while walking?)

Thanks in advance
 

khmp

Sponsor

No you can leave Game_Map alone. Someone else with more experience might do it differently but I added a method to Game_Actor and aliased a method in Game_Party.

Code:
class Game_Actor < Game_Battler
  def increment_attr(attr, n = 1)
    case attr
      when 'sp'
        if @sp < base_maxsp
          @sp += n
        end
      when 'exp'
        exp=(@exp+=n)
      else
        return
    end
  end
end

class Game_Party
  alias_method :game_party_increase_steps, :increase_steps
  def increase_steps
    game_party_increase_steps
    for i in 0..@actors.size - 1
      @actors[i].increment_attr('sp')
    end
  end
end

Insert a line above Main but below Scene_Debug and paste in it the code above.

Good luck with it grecias! :thumb:
 
It works perfectly. If you don't mind, I made some corrections:

Code:
class Game_Actor < Game_Battler
  def increment_attr(attr, n = 1)
    case attr
      when 'sp'
        if @sp < maxsp #Using base_maxsp keeped it from recovering extra sp
          sp= (@sp+=n)
        end
      when 'exp'
        exp=(@exp+=n)
      else
        return
    end
  end
end

class Game_Party
  alias_method :game_party_increase_steps, :increase_steps
  def increase_steps
    game_party_increase_steps
    for i in 0...@actors.size
      @actors[i].increment_attr('sp')
      @actors[i].increment_attr('exp') #I think it didn't get exp without that
    end
  end
end


Thanks again! Now I owe you twice. I will definitely have to give you credit if I ever finish my project...
 

khmp

Sponsor

I'm flattered but don't give credit to me reserve that for the real scripters. I'm just glad to help.

As for the additions you made don't make the sp increment look like the exp increment for the sake of symmetry. You are just assigning that work to a local variable and than once you left the scope of the case statement it doesn't exist anymore. Just extra work and memory. The reason the exp incrementation looks the way it does is because its actually calling a method. It checks to see if you level up among other things. So you can leave the sp the way it was.

Second; You took away the '- 1' from the for loop in Game_Party. The reason that was done was so that the 'i' never went to an invalid index. Like say we had an array that was 3 elements long:

Code:
ar = [1, 2, 3]

If we make a for loop to access the array elements using an indexer(i), that went ar.size times.

Code:
for i in 0..ar.size
  ar[i] += (i % 2) 
end

The indexes it would use would be 0, 1, 2, 3. At index 3 which doesn't exist in the array you will get an error for accessing an invalid index. I'm surprised you haven't run into an error with that code like that.

I do like the addition of comments I sometimes forget to do those at first and then do them later when I forget why I did something one way and not another. I didn't know you wanted both exp and sp incrementation to work sorry :)

And as always good luck with your game! :thumb:
 

khmp

Sponsor

Aright so lets go back to the Array. Arrays act as a collection of items. These arrays are just blocks of memory linked together. In order to gain access to a particular part of an array we use an index. This index also acts as an offset into the array based off the start.

So if we had an Array and inside it contained 4 integers:
Code:
aexample = [4, 5, 6, 7]
To get access to the third element we use the following code:
Code:
aexample[2]
The semi-technical answer to this is that the computer finds the address in memory where aexample is located. Then it sees the index as the offset. In this case it's 2 so to get the proper element the computer adds to aexample's address, 2 * sizeof(element). Its hard to explain maybe I'll make some pictures later.

So now a 'for loop':
Code:
for i in 0..aexample.size
  do_work(aexample[i])
end
That will loop 5 times. The results for i will be 0, 1, 2, 3, 4. All is fine and dandy until it goes to access aexample[4]. The valid indexes for accessing our four element array are 0,1,2,3. There is no such thing as an element at index 4. It's out of bounds for the array.

That's why the proper way to create a for loop that indexes an array would be this:
Code:
for i in 0..aexample.size - 1
  do_work(aexample[i])
end

It might have been overkill to describe all this but I think it will help you better understand I hope.
 
So mind explaining the difference between,
Code:
for i in 0...aexample.size - 1
  do_work(aexample[i])
end
and
Code:
for i in 0..aexample.size - 1
  do_work(aexample[i])
end

For the record I do know how arrays work, but thanks for pointing it out.
Edit: I Know how to program in other languages, and belive me, I am just a bit rusty in terms of Ruby.
 

khmp

Sponsor

Well ok I notice you have three dots in the range of that upper loop:

Code:
for i in 0...aexample.size - 1

See for loops utilize a range of elements that define how many times the loop should run. In our case we are creating a Range type going from 0 to (aexample.size - 1). Range type variables are created by placing two periods or three periods between two numbers. If you are using two periods the Range will run from the start and include the last element. Three periods however will accomplish start with the first and go to all but the last number. The -1 after the second number in a 3 dot range would be redundant. And most likely would not perform the way we want it to.

Code:
rexample1 = 0..3
rexample2 = 0...3

p rexample1 # -> [0,1,2,3]
p rexample2 # -> [0,1,2]

So back to:

Code:
aexample = [4, 5, 6, 7]
for i in 0...aexampe.size - 1
  p aexample[i]
end

The output will be 4, 5, 6. 7 will not print. 'i' who acts as our indexer will be 0, 1, 2.

I made a mistake for correcting grecias. I didn't even notice the 3 dots. The below code is indeed correct and I apologize for questioning it.':|

Code:
for i in 0...@actors.size
  @actors[i].increment_attr('sp')
  @actors[i].increment_attr('exp') #I think it didn't get exp without that
end
 
Thats what I was saying, but thanks for pointing it out man, I though it was like, .. it wouldn't pick the last index, that was my mistake.

Good Job on explaining me :D
 
Wait, I need some explaining too :)

I just got a bit troubled after what you said eariler...

I tried your corrections about the sp like you said and I run into an error. Using the script as I posted it was the only way for it to work properly. I also noted that the script doesn't check for level up like you said. Could it be that the rules do not apply because I have put a lot of other scripts in the game as well? (I also have modified some thing over the scene_debug in the past...)

P.S. 1.
I must admit I too needed to learn about the "for i in 0...@actors.size" thing.

P.S. 2.
About the credit, believe me, I feel much more obliged to mention you than the "real scripters"(if anything, everybody who cares will know it's their script even if I don't mention it - e.g. like dubealex and that awesome advanced message code)
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top