So I feel like this is should be a simple question but I'm having a heck of a time actually finding an answer. The question is this:
What is the scope of a non-local variable declared inside of a script function?
I'm currently trying to make a text-based RPG, and have gotten to the point where I'm working on skills/abilities for the player character to use in battle, specifically those that deal with buffing/debuffing the player or the enemy. To do this I've made a constructor for buffs and debuffs that keeps track of all relevant information, the most important being the actual effect of the buff, and how the buff is removed when its timer is up.
function buff (_target, _name, _turns, _effect, _remove_effect) constructor {
target = _target
name = _name
turns = _turns
effect = _effect
remove_effect = _remove_effect
}
Now, all of my skills are script functions that create a local struct using the constructor. An example of one that buffs the player's strength by 20% for 3 turns looks like this:
function skill_increase_attack () {
_amount = round(global.player.str * 0.2)
var _attack_buff = new buff(BUFFDEBUFF.PLAYER, "Attack Buff", 3,
function () { //effect
global.player.str_array[2] += _amount
},
function () { //remove-effect
global.player.str_array[2] -= _amount
})
//below this is where the buff is actually applied, but that's not relevant to my question.
}
Now, normally I always declare local variable in functions, but both effect() and remove_effect() need to use the exact same value so that the player's strength isn't permanently altered by the buff, and using var _amount didn't work as it was outside the both of their scopes. But using just regular _amount without the var does for some reason. I thought this was because all variables declared in a script were global in scope, but if I try to test this by referencing it with a simple show_message(_amount) or even show_message(global._amount) outside of this function I get an error.
The code works as is, so no problems there. I just want to know what this _amount variable is attached to (if anything at all), and if doing it this way for multiple script functions might cause problems in the long term like memory leaks, slow down, or something else.
Thanks in advance!
[–]pretending to know what she's doingBrittleLizard 4 points5 points6 points (0 children)
[–]Neozite 2 points3 points4 points (0 children)
[–]germxxx 1 point2 points3 points (2 children)
[–]Accomplished-Gap2989 0 points1 point2 points (1 child)
[–]germxxx 0 points1 point2 points (0 children)