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.

[RESOLVED] Quick Coding Question

Status
Not open for further replies.
Just before I ask my question, id just like to say, I'm not a great scripter, yet, but through looking at user made scripts i'm starting to learn how to script. Right now, i'm modifying scripts for myself, to suit my needs. (Such as modifying the layout of a menu, or the formula for taking damage... stuff like that. I'm really proud of myself.)

As for my question..:
Is it possible to add more variables onto actor? (i'm not positive this is a correct example, but ie. actor.str)
I want to make another variable such as "pir" (pierce) for each actor. Of course ill know how to put it into my menu if i want it, but im just curious how.

Details of what i'm doing:
I completely redid how damage is calculated, and how all the database data applies to the damage formula. As an example I changed pdef into minimum damage, atk into maximum damage, and mdef in defense. But here's the thing; what kind of weapon increases defence? So i just want to change a weapon's mdef into piercing power (cutting through armor.).

Thanks for reading and hopefully you didn't TL;DR.
 
It's the perfectly right section, and I don't think the RPG Maker forums have been changed in a while... just shifted around...

However, take a look into Game_Actor class. You should see a number of variables in the initialize method, which is where you can add your variable(s). Then, you need to atribute them (as shown in this post. From that point on, it depends on how you want to handle it.
 
While pretending mdef is 'pir' or whatever works,
it'll end up confusing you.
Here's a code to add pierce and min/max damage to weapons & pierce to actors.
You won't be able to set the new states from the Database-
so you set them inside the script:
PIR = {1=>100}
means weapon/actor number 001 has 100 pir.
You don't have to do this for every actor/weapon. There is a default value (set it to whatever you like).
Code:
class RPG::Weapon

  PIR = {1=>75, 2=>90}

  MIN_DAMAGE = {1=>5, 2=>10}

  MAX_DAMAGE = {1=>200, 2=>400}

  

  def pir_plus

    default = 50  # default value

    return (PIR[@id].nil? ? default : PIR[@id] )

  end

  

  def minDamage

    default = 50

    return ( MIN_DAMAGE[@id].nil? ? default : MIN_DAMAGE[@id] )

  end

  

  def maxDamage

    default = 50

    return ( MAX_DAMAGE[@id].nil? ? default : MAX_DAMAGE[@id] )

  end

  

end

 

class Game_Actor < Game_Battler

  

  PIR = {1=>100, 3=>55, 6=>145}

  

  def pir

    default = 50

    # base_pir

    n = PIR[self.id]

    n = default if n.nil?

    # A formula to make 'pir' grow by 5 every level.

    n += @level*5

    # gain 'pir' boost from the weapon.

    weapon = $data_weapons[@weapon_id]

    n += weapon != nil ? weapon.pir_plus : 0

    return [[n, 1].max, 999].min

  end

  

end

I hope this example will teach you how to add new states. You sound like one who does things his own way :p
 
@BlueScope: Okay since i am still a scripting nub, when it says def (like def base_str) does that mean its defining what the value is? So if i put in def base_pir and did a little magic and put in something like this:
Code:
def pir

weapon = $data_weapons[@weapon_id]

n = weapon != nil ? weapon.mdef : 0

return [[n, 1].max, 999].min

end
...there would a variable for PIR? If this is true, you just improved my scripting level by a huge chunk.



@silver wind: Hehe, for me, I put everything into a notepad document, so I can easily keep track. I have two main reasons I like to modify the database; First, its very easy and right at hand to use, you dont have to go into a script and start changing things, and secondly, I had no idea how to make a script, i can just edit them. Although, I agree with you, its simpler just to make a script for it.

By the way, that nice piece of script you made right there is very helpful, thanks!

Edit: Oh yea, and i love doing things my own way! :P

2Edit: HOLYSHIZTZTI! I got it to work, happy day for me! Thanks to both of you! With this kind of info, i can almost start making scripts. The only things i need to learn is class, and making windows. Thanks again.
 
'def' only defined methods, so your script there would allow the usage of $game_actors[0].pir for example. It will return, in your case, the mdef value of the equipped weapon (or 0, if no weapon is equipped).
It will not, however, create a variable called @pir or whatever (you could just as well name the method 'yaythisnamemakesnosenseatall', and it wouldn't make a difference - it's that method's unique name).
 
Heh, not quite... attr_reader only defines symbols (:variable would be a symbol; note that this is not a necessity to do if you don't want to access that variable externally) to access variables from different classes. You define/set variables like this:
Code:
@variable = 'yay'
Sorry for confusing you in the process of explaining everything at once ^^"
 
Okay, so thats basically a global variable, correct? So if i wanted to make a variable, that would be accesed somewhere else, in a different class, id put dont something like:
Code:
@importantnumber = 501924
and then access it later on using [special = @importantnumber * 2], correct?
Thanks so much for your time, and it wasn't THAT confusing :P.
 
Global variables always begin with a $ symbol... @variables are instance variables and in such way they can only be accessed by defined methods of the current class. To make them accessible from anywhere else, first you would either define symbols like :new_variable with attr_reader or attr_accessor (attr_reader :new_variable) or define a method like in this example...

[rgss]class Something
  def new_variable
    @new_variable
  end
 
  def new_variable=(some_value)
    @new_variable = some_value
  end
end
[/rgss]

Then and only then any other variable would be able to get access to @new_variable.

[rgss]class Scene_Item
  alias scripter_scene_item_init initialize
  alias scripter_scene_item_up update
  def initialize
    scripter_scene_item_init
    @something = Something.new
    @something.new_variable = 0
    @scripter = Person.new
  end
 
  def update
    @something.new_variable += 1 if @scripter.is_right?
    scripter_scene_item_up
  end
end
[/rgss]
 
@kyonides: Sweet thanks, i always thought $ was global, just got confused. @ variables still seem to be confusing to me...


@silver wind: I've got a small problem that developed with the use of your code snippet. I just wanted to try it out, so i added two new attributes, SwiftA and SwiftL (basically Swift Arm and Swift Leg), except im getting a small error.
Heres the new code i generated from yours:
Code:
class RPG::WEAPON

  SWIFTA = {1=>0.1,2=>0.6,3=>5.5}

  SWIFTL = {}

 

  def swifta

    default = 0.1  # default value

    return (SWIFTA[@id].nil? ? default : SWIFTA[@id] )

  end

 

  def swiftl

    default = 0.0

    return (SWIFTL[@id].nil? ? default : SWIFTL[@id] )

  end

 

end

 

class RPG::ARMOR

  SWIFTA = {}

  SWIFTL = {}

 

  def swifta

    default = 0.0  # default value

    return (SWIFTA[@id].nil? ? default : SWIFTA[@id])

  end

 

  def swiftl

    default = 0.0

    return (SWIFTL[@id].nil? ? default : SWIFTL[@id])

  end

 

end

 

class Game_Actor < Game_Battler

 

  SWIFTA = {}

  SWIFTL = {}

 

  def SWIFTA

    default = 1.0

    # base_pir

    n = SWIFTA[self.id]

    n = default if n.nil?

    # A formula to make 'pir' grow by 5 every level.

    n += (@level/2)*0.1

    # gain 'pir' boost from the weapon.

    weapon = $data_weapons[@weapon_id]

    armor1 = $data_armors[@armor1_id]

    armor2 = $data_armors[@armor2_id]

    armor3 = $data_armors[@armor3_id]

    armor4 = $data_armors[@armor4_id]

    n += weapon != nil ? weapon.swifta : 0

    n += armor1 != nil ? armor1.swifta : 0

    n += armor2 != nil ? armor2.swifta : 0

    n += armor3 != nil ? armor3.swifta : 0

    n += armor4 != nil ? armor4.swifta : 0

    return [[n, 1.0].max, 999.0].min

  end

  

  def SWIFTL

    default = 1.0

    # base_pir

    n = SWIFTL[self.id]

    n = default if n.nil?

    # A formula to make 'pir' grow by 5 every level.

    n += (@level/2)*0.1

    # gain 'pir' boost from the weapon.

    weapon = $data_weapons[@weapon_id]

    armor1 = $data_armors[@armor1_id]

    armor2 = $data_armors[@armor2_id]

    armor3 = $data_armors[@armor3_id]

    armor4 = $data_armors[@armor4_id]

    n += weapon != nil ? weapon.swiftl : 0

    n += armor1 != nil ? armor1.swiftl : 0

    n += armor2 != nil ? armor2.swiftl : 0

    n += armor3 != nil ? armor3.swiftl : 0

    n += armor4 != nil ? armor4.swiftl : 0

    return [[n, 1.0].max, 999.0].min

  end

 

 

end
and the error im getting is:

Script 'Swiftness' line 51: NoMethodError Occurred
undefined method 'swifta' for #<RPG::Weapon:0x37888e8>


I assume this error is because i am mis-defining the proper value. (Is it possible i can't use decimal numbers?)
If anyone can help me on this it'd be great.
 
I converted everything to lowercase (except for weapon and armor) and now im getting stack level is too deep.
Code:
class RPG::Weapon

  swifta = {}

  swiftl = {}

 

  def swifta

    default = 1.0  # default value

    return ( swifta[@id].nil? ? default : swifta[@id] )

  end

 

  def swiftl

    default = 1.0

    return ( swiftl[@id].nil? ? default : swiftl[@id] )

  end

 

end

 

class RPG::Armor

  swifta = {}

  swiftl = {}

 

  def swifta

    default = 1.0  # default value

    return (swifta[@id].nil? ? default : swifta[@id])

  end

 

  def swiftl

    default = 1.0

    return (swiftl[@id].nil? ? default : swiftl[@id])

  end

 

end

 

class Game_Actor < Game_Battler

 

  def swifta

    # base_pir

    n = 1.0

    n += (@level/2)*0.1

    weapon = $data_weapons[@weapon_id]

    armor1 = $data_armors[@armor1_id]

    armor2 = $data_armors[@armor2_id]

    armor3 = $data_armors[@armor3_id]

    armor4 = $data_armors[@armor4_id]

    n += weapon != nil ? weapon.swifta : 0

    n += armor1 != nil ? armor1.swifta : 0

    n += armor2 != nil ? armor2.swifta : 0

    n += armor3 != nil ? armor3.swifta : 0

    n += armor4 != nil ? armor4.swifta : 0

    return [[n, 1.0].max, 999.0].min

  end

  

  def swiftl

    # base_pir

    n = 1.0

    n += (@level/2)*0.1

    weapon = $data_weapons[@weapon_id]

    armor1 = $data_armors[@armor1_id]

    armor2 = $data_armors[@armor2_id]

    armor3 = $data_armors[@armor3_id]

    armor4 = $data_armors[@armor4_id]

    n += weapon != nil ? weapon.swiftl : 0

    n += armor1 != nil ? armor1.swiftl : 0

    n += armor2 != nil ? armor2.swiftl : 0

    n += armor3 != nil ? armor3.swiftl : 0

    n += armor4 != nil ? armor4.swiftl : 0

    return [[n, 1.0].max, 999.0].min

  end

  

end

Revised version ^

Error:
Script 'Swiftness' line 7: SystemsStackError Occurred
stack level too deep
 
I tried to be specific, but didn't do well enough... The hashes (SWIFTA = {}, SWIFTL = {}) are constants and should be capitalized. Since you made everything lowercase the methods are calling themselves.

[rgss]class RPG::Weapon
  SWIFTA = {}
  SWIFTL = {}
 
  def swifta
    default = 1.0  # default value
    return ( SWIFTA[@id].nil? ? default : SWIFTA[@id] )
  end
 
  def swiftl
    default = 1.0
    return ( SWIFTL[@id].nil? ? default : SWIFTL[@id] )
  end
end
 
class RPG::Armor
  SWIFTA = {}
  SWIFTL = {}
 
  def swifta
    default = 1.0  # default value
    return (SWIFTA[@id].nil? ? default : SWIFTA[@id])
  end
 
  def swiftl
    default = 1.0
    return (SWIFTL[@id].nil? ? default : SWIFTL[@id])
  end
end
 
class Game_Actor < Game_Battler
  def swifta
    # base_pir
    n = 1.0
    n += (@level/2)*0.1
    weapon = $data_weapons[@weapon_id]
    armor1 = $data_armors[@armor1_id]
    armor2 = $data_armors[@armor2_id]
    armor3 = $data_armors[@armor3_id]
    armor4 = $data_armors[@armor4_id]
    n += weapon != nil ? weapon.swifta : 0
    n += armor1 != nil ? armor1.swifta : 0
    n += armor2 != nil ? armor2.swifta : 0
    n += armor3 != nil ? armor3.swifta : 0
    n += armor4 != nil ? armor4.swifta : 0
    return [[n, 1.0].max, 999.0].min
  end
 
  def swiftl
    # base_pir
    n = 1.0
    n += (@level/2)*0.1
    weapon = $data_weapons[@weapon_id]
    armor1 = $data_armors[@armor1_id]
    armor2 = $data_armors[@armor2_id]
    armor3 = $data_armors[@armor3_id]
    armor4 = $data_armors[@armor4_id]
    n += weapon != nil ? weapon.swiftl : 0
    n += armor1 != nil ? armor1.swiftl : 0
    n += armor2 != nil ? armor2.swiftl : 0
    n += armor3 != nil ? armor3.swiftl : 0
    n += armor4 != nil ? armor4.swiftl : 0
    return [[n, 1.0].max, 999.0].min
  end
end
[/rgss]
 
Status
Not open for further replies.

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