Function parameters

Hi there!

I'm making a plugin, and I don't want to put a lot of if conditions... so I'm trying to learn function parameters.
After a hard time trying to figure that out, I came here asking for help... ^^'''

here is the code:

Code:
function elipic (id, property) {
    var casa = property;
    return SceneManager._scene._spriteset._pictureContainer.children[id - 1].casa;
};

The idea is to make in a way that I only have to replace the two parameters to get what I want.

Then I call the function. But this is what I get...
YiubD1E.png


I manage it to work like that:
Code:
var casa;
function elipic (id) {
    if(casa == "width") {
        return SceneManager._scene._spriteset._pictureContainer.children[id - 1].width;
    } else if (casa == "height") {
        return SceneManager._scene._spriteset._pictureContainer.children[id - 1].height;
    }
};

NfngskY.png
Ah, try the following :

Code:
function elipic (id, property) {
   var casa = property;
   return SceneManager._scene._spriteset._pictureContainer.children[id - 1][casa];
};

// Call the Function ...
elipic(1, 'width');
Ah, try the following :

Code:
function elipic (id, property) {
   var casa = property;
   return SceneManager._scene._spriteset._pictureContainer.children[id - 1][casa];
};

// Call the Function ...
elipic(1, 'width');
Thank you! That worked!

But, why? =O
It may be possible to make the code even shorter than Ossra's version. Try this:

Code:
function elipic (id, property) {
  return SceneManager._scene._spriteset._pictureContainer.children[id - 1][property];
};

// Call the Function ...
elipic(1, 'width');

'var casa = property' can be removed and the casa in the return statement can be replaced by property, saving a step. This is possible since the property is defined when the 'elipic' function is called.
@Eliaquim There are different ways to access variables in an object :

Code:
var object = { a: 'blarg' };

// 'dot' Notation
object.a

// 'bracket' Notation
object['a']

// 'bracket' Notation with Variable
var test = 'a';
object[test]

If you want to access a property of a variable dynamically, you should use the 'bracket' notation.

You could also have each Sprite_Picture callback to the picture data stored in $gameScreen and then nab the values from there :

Code:
var Temp_Alias_Sprite_Picture_updateOther = Sprite_Picture.prototype.updateOther;

Sprite_Picture.prototype.updateOther = function() {
    Temp_Alias_Sprite_Picture_updateOther.call(this);

    var picture = this.picture();
    picture.__spriteData.width = this.width;
    picture.__spriteData.height = this.height;
};

var Temp_Alias_Game_Picture_initialize = Game_Picture.prototype.initialize;

Game_Picture.prototype.initialize = function() {
    Temp_Alias_Game_Picture_initialize.call(this);

    this.initSpriteData();
};

Game_Picture.prototype.initSpriteData = function() {
    this.__spriteData = { };

    this.__spriteData.width = 0;
    this.__spriteData.height = 0;
};

Game_Picture.prototype.setSpriteData = function(property, value) {
    this.__spriteData[property] = value;
};

Game_Picture.prototype.getSpriteData = function(property) {
    if (property) {
      return this.__spriteData[property];
    } else {
      return this.__spriteData;
    }
};

Code:
$gameScreen.picture(1).getSpriteData('width');

// Or ...
$gameScreen.picture(1).__spriteData.width;
Thank you! That worked!

But, why? =O
Simply put, computers are deterministic machines. And so they do what they are told to do, not what you think they should do 😛

The difference between a.x and a[x] is, a.x means the property x of object a. So .casa meant the engine tried to search for a property named casa, but could not find one.
With [] instead of . you tell Javascript to search for a property, whose name is equal to the value of the variable. So if casa was equal to "width", a[casa] makes it search for a.width.

Why is that so? It allows for dynamic programming.
Hmmmmm!!!! Understand that now!!
This is a mindblowing discovery for me!! =O

Thanks a lot, people!!
😍

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/function-parameters.113152/