ID's

Started by Pax Animo, Sun 04/09/2022 02:11:29

Previous topic - Next topic

Pax Animo

Hey, so I think I've lost my way and need some advice on how ID's work,

After reading recent questions regarding ID's including my own, I'm just a bit confused by it.

This is a function i use to switch players:

Code: ags
function member_select(int get_char_id, int set_portrait_gfx)
{
  //gPlayer.BackgroundGraphic = set_portrait_gfx;
  character[get_char_id].SetAsPlayer();
  //targetCharacter = player;
}


This is an example of how i use this function:

Code: ags
member_select(cScientist3.ID, 2144);


Have i got this all wrong?

Cheers in advance.
Misunderstood

Crimson Wizard

#1
Above script works, but is overcomplicated. You seem to already know that "cScientist3" exists, but extract its ID. Instead you may use these pointers to address the object for all the purposes.

The "character[]" array, in fact, contains exactly same pointers, including "cScientist3".

So, in your case, you may do it as:
Code: ags

function member_select(Character *c, int set_portrait_gfx)
{
    //gPlayer.BackgroundGraphic = set_portrait_gfx;
    c.SetAsPlayer();
    //targetCharacter = player;
}


And call this function like:
Code: ags

member_select(cScientist3, 2144);


The only reason to use numeric IDs and take objects from array is when you don't have a pointer ready, or don't know which pointer to use, but have a number stored somewhere, and want to get an object using that number. For example, you could have saved a character's ID into a file, and read it back. Then of course you will have to get object as "Character *c = character[some_id];". But when you already know which object to use, there's little reason to not pass it as a pointer and use directly.

Pax Animo

#2
Thank you so much Crimson, this clears up my question perfectly, invaluable info as always.
Misunderstood

SMF spam blocked by CleanTalk