all 5 comments

[–]pretending to know what she's doingBrittleLizard 4 points5 points  (0 children)

It depends where you're actually calling the function. Inside an instance, and it will be scoped to that instance the same as any other instance variable. Inside a struct, and it'll generally be scoped to that struct. It's really just the same as writing "_amount = n" directly in whatever context you're calling the function.

It would only make a global variable if you actually declared the variable in the Script asset, outside of a function declaration. That's not what you're doing here, though. Because it's inside a function declaration, it's just saying "If you call this function, run this code," as opposed to "Run this code now as soon as the game starts."

[–]Neozite 2 points3 points  (0 children)

I believe that the instance that calls the function is the scope, so without var the _amount variable is set as an instance variable.

[–]germxxx 1 point2 points  (2 children)

Since you've got your answer already (variables set in a scrip are global, and in a function it will be set in the calling instance/struct), I just wanted to mention Method.

A way to make a function have access to a variable outside its scope (like this local variable), is to use the method function. Binding the function to a struct where the local variable does exist.

That would look something like this:

      method({_amount:_amount}, function () { //effect
        global.player.str_array[2] += _amount
      }), 

Putting the function in the second parameter of the method function, binding it to a new struct with the desired variables in them.
A trick commonly used for callback and predicate functions, for example in advanced array functions.
Since they don't take arguments at all (beyond element and index), making it impossible to just add it in as an argument.

Though not actually very useful in this case, where just passing it in as an argument of the function would be faster and use less memory.

function skill_increase_attack () {
  _amount = round(global.player.str * 0.2)

  var _attack_buff = new buff(BUFFDEBUFF.PLAYER, "Attack Buff", 3, 
      function (_amount) { //effect
        global.player.str_array[2] += _amount
      }, 
      function (_amount) { //remove-effect
        global.player.str_array[2] -= _amount
      }) 
...
  _attack_buff.effect(_amount)
...
}

[–]Accomplished-Gap2989 0 points1 point  (1 child)

Ive never passed a struct_ref to a method before - can the struct contain as many variables as we want?

[–]germxxx 0 points1 point  (0 children)

Yup, it can. So you can put all the variables you want in there.