AGS: Some Questions Regarding Multiple Characters

Started by metalmario991, Wed 23/01/2019 06:28:56

Previous topic - Next topic

metalmario991

Hello, I am working on a game where you have multiple characters that you switch between.
My main questions are:
1) Best way to check what character you're using, for character-specific events/dialogue.
2) How to Give an item to another character and add it to their inventory.
3) Best way to switch between characters. I'm currently trying the DOTT way, with the character icons in the GUI.

As the last one implies, I'm using the Verb-style.

Snarky

#1
1) if(player == cBob)
2) You mean transfer it from one playable character to another? Basically:

cBob.AddInventory(iFlute);
cAlice.LoseInventory(iFlute);


If you're going to do this a lot, you can make a function out of it, like this:

Code: ags
bool GiveInventory(this Character*, Character* recipient, InventoryItem* item)
{
  // This checks that you're not trying to give "nothing", or to "nobody", or give something you don't have
  if(!recipient || !item || !this.HasInventory(item))
    return false;
  recipient.AddInventory(item);
  this.LoseInventory(item);
  return true;
}


Now you can call it like cBob.GiveInventory(cAlice, iFlute); (Or more likely, rather than hardcoding it with values like this, you'll do something like player.GiveInventory(characterThatWasClicked, player.ActiveInventory);)

3) Up to you.

Khris

Maybe latest versions support this, but afaik !recipient needs to be recipient != null.

As for 3, the basic idea is to call cBob.SetAsPlayer(); when the respective button is clicked. This will automatically change the room, if necessary. If both characters are in the same room, the camera will abruptly jump there to try and center the other character. You could implement a check for the other character being in the same room and if so, scroll there instead.

Edit: fixed weird phrasing

Slasher

You mean 'Jump There'.... if only there was a Scroll speed ;)

metalmario991

Quote from: Snarky on Wed 23/01/2019 07:19:42
1) if(player == cBob)
2) You mean transfer it from one playable character to another? Basically:

cBob.AddInventory(iFlute);
cAlice.LoseInventory(iFlute);


If you're going to do this a lot, you can make a function out of it, like this:

Code: ags
bool GiveInventory(this Character*, Character* recipient, InventoryItem* item)
{
  // This checks that you're not trying to give "nothing", or to "nobody", or give something you don't have
  if(!recipient || !item || !this.HasInventory(item))
    return false;
  recipient.AddInventory(item);
  this.LoseInventory(item);
  return true;
}


Now you can call it like cBob.GiveInventory(cAlice, iFlute); (Or more likely, rather than hardcoding it with values like this, you'll do something like player.GiveInventory(characterThatWasClicked, player.ActiveInventory);)

3) Up to you.

I'm a little confused in regards to the second point. I'm not too familar with coding. So where does it go in the script, do I need to apply a check like "This Character is selected", just to make sure I can actually GIVE stuff without adding it into an NPC's inventory etc.

metalmario991

I guess another thing as well would be how to make them walk over to the character to give them the item.

Snarky

Quote from: Khris on Wed 23/01/2019 11:06:35
Maybe latest versions support this, but afaik !recipient needs to be recipient != null.

The test would be recipient == null (and same for item) in this case, but I see a lot of code that uses if(cChar) instead of if(cChar != null), and I assumed a negation would work.

Quote from: metalmario991 on Wed 23/01/2019 19:16:09
I'm a little confused in regards to the second point. I'm not too familar with coding. So where does it go in the script, do I need to apply a check like "This Character is selected", just to make sure I can actually GIVE stuff without adding it into an NPC's inventory etc.

AGS works on the basis of events: there's a "trigger" (the event), and something that happens as a result (the event handler, written as a function). Usually to respond to player interaction, the trigger is "the player clicked a mouse button", or "the player clicked on X".

If you're using the LucasArts/Tumbleweed template, it defines how these interactions work. I'm not too familiar with it, but I know it comes with a lot of documentation, which you should refer to. In this case what we're interested in is "the player clicked on a character while the Use/Give verb was active and an inventory item selected", right?

So you find the function that handles this event (or write a new one if necessary, making sure to actually hook it up to the event), and insert the code there. How to best handle it depends on a little on whether you want this to be a general mechanic (so that you can transfer items between multiple playable characters pretty freely), or just a few special cases (for solving given puzzles, a certain item can be given to a certain character).

Quote from: metalmario991 on Wed 23/01/2019 19:32:30
I guess another thing as well would be how to make them walk over to the character to give them the item.

In the most basic case, you just put a blocking Walk command in the code. Depending on your game, you may need to check that you can actually reach them, or do some logic to figure out precisely where you need to walk. (It's simpler if the character you're walking to just stands in a predefined fixed position.) If you want the LucasArts style where you can change your mind and interrupt the action while walking, things get a bit more complicated, but I believe the LucasArts/Tumbleweed template does a lot of the work.

Khris

Quote from: Snarky on Thu 24/01/2019 07:46:04The test would be recipient == null (and same for item) in this case, but I see a lot of code that uses if(cChar) instead of if(cChar != null), and I assumed a negation would work.
Right, it should be recipient == null. And putting truthy / falsey stuff in if () does work now indeed! I completely missed that change. Good to know.

SMF spam blocked by CleanTalk