How can you use an "Inventory Item" on a character?

Started by MufasaRKG, Sun 22/03/2015 01:17:40

Previous topic - Next topic

MufasaRKG

I need help finding the basic script function where instead of using the Items on a object, where you can use it on another character and that allows to further access to another room?

-m
BUCKET FRESH STUDIOS
Underground Art, Music, & Culture

Ghost

That's really simple- go to the "Characters" tree of your game and find the character you want to give the item to. Click the event button (that small thunderbolt icon) to open his events pane.
There's one called "use inventory on character", and that is the event handler you want to create. Click the "..." button to create the handler and then:

Code: ags

if (player.activeInventory == iTicket)
{
  player.Say("Here is my ticket!");
  cGuy.Say("Okay, move on!");
  player.ChangeRoom(12);
}
else
{
  cGuy.Say("No, I don't want that.");
}


(Note: This code is functional but you will probably want to enter something else here :D ) This also does not remove the item from the player's inventory, so you can use it over and over again.

MufasaRKG

Thank you for your help! I'm gonna pratice and try that out in a few. Once again thank you.

-m
BUCKET FRESH STUDIOS
Underground Art, Music, & Culture

berika

its the same as an object but with a character pretty much

MufasaRKG

Well here's the thing. I did take heed when he said the Item will still remain in my inventory, but what if I wanted to clear that item out for good? Also, to command the script to stop actions such as, an pop up talk quote from another character every time he re enters the room, so that "event fade before room function" stops and is only used once? 

Im pretty close to putting an demo out with around 16 rooms available.

-m
BUCKET FRESH STUDIOS
Underground Art, Music, & Culture

berika

to take away an item from your characters inventory do something like
cwhatever.loseinventory(item you want to take away);
just add that to the code

Ghost

Quote from: MufasaRKG on Sun 22/03/2015 21:38:18
so that "event fade before room function" stops and is only used once? 

If you want some code to be executed just once, there are two ways to do it:

1) Use the room's "first time enters room" event handler. Whatever code you put there is only executes the first time the room is entered, ever. In your case this would be the most logical solution since your event (a comment by someone) is directly tied to the room. It's usually wise to keep your code clean.

2) Create your own code and ensure it's executed just once. AGS has a build-in way to do just that, the DoOnceOnly command. It works like this:
Code: ags

if (Game.DoOnceOnly("enter this room")) 
{
  player.Say("Ah, finally! Awesome scenery here!");
}


DoOnceOnly takes a string as parameter; it's up to you what you type but IIRC it must be unique.

A good place to put this code would be the room's after_fadein event handler. That way, all your code is run but the comment/one-time action is really only executed the first time this room is entered.
Hope that helps :)


MufasaRKG

Thank you. This is more of an learning curve then I thought! It is well worth it and worth the days, hours, and time I take out for this. Hopefully I will be developing something enjoyable for the whole forum community to try out.

Also thanks you Berika for that inventory help. That was a strong part of the script I needed.

-m
BUCKET FRESH STUDIOS
Underground Art, Music, & Culture

monkey0506

Quote from: Ghost on Mon 23/03/2015 03:09:52DoOnceOnly takes a string as parameter; it's up to you what you type but IIRC it must be unique.

The parameter to Game.DoOnceOnly has to be unique to the condition. It's perfectly legal to have a usage like:

Code: ags
void SomeFunction()
{
  if (Game.DoOnceOnly("didthething")) {}
}

void SomeOtherFunction()
{
  if (Game.DoOnceOnly("didthething")) {}
}


The point is that you are creating a condition that will only run once per game, but there are valid cases where you might need to check that same condition in more than one place.

Also note that since the condition is actually a string, you can name it pretty much anything from "Has the player done that thing with the inventory item yet?" to "123", so you don't have to restrict yourself to valid variable names.

Ghost

Quote from: monkey_05_06 on Mon 23/03/2015 19:03:52
The parameter to Game.DoOnceOnly has to be unique to the condition.

Thanks for clearing that up! The manual's line is a bit more vague IMO, maybe worth a change?

Quote from: MufasaRKG on Mon 23/03/2015 13:57:59
This is more of an learning curve then I thought!

The learning curve is mostly to grab the concepts of AGS's inner workings. Once you get a clear idea about the different things you build your game from, you will soon find that the commands for them are almost always the same, and then it's easy to familiarize yourself with what's most often used. There's never any harm in asking too; people will always help you out- I wish you a good start!

MufasaRKG



Quote from: MufasaRKG on Mon 23/03/2015 13:57:59
This is more of an learning curve then I thought!

The learning curve is mostly to grab the concepts of AGS's inner workings. Once you get a clear idea about the different things you build your game from, you will soon find that the commands for them are almost always the same, and then it's easy to familiarize yourself with what's most often used. There's never any harm in asking too; people will always help you out- I wish you a good start!
[/quote]


Well you have been a big help so far. Just a couple things that still have me stuck which is, when the event occurs where the item is used on the character and say he switches rooms into another area. When you enter that room that he / she is now in, how can you get the character to been seen through animated view? Otherwise, getting him to move all around on his own in room while you can set certain interactions with him. Are you able to use new inventory items on him and continue to switch his rooms as it follows?


-m
BUCKET FRESH STUDIOS
Underground Art, Music, & Culture

Ghost

Quote from: MufasaRKG on Wed 25/03/2015 04:43:20
When you enter that room that he / she is now in, how can you get the character to been seen through animated view? Otherwise, getting him to move all around on his own in room while you can set certain interactions with him. Are you able to use new inventory items on him and continue to switch his rooms as it follows?

I am not quite sure what you mean with "through animated view"; if you explain what you want to achieve I am sure we can find a solution, though (nod)

As for the rest, all of this is possible. As far as AGS is concerned, the player character and any NPC you create are just "characters", and they all have access to the full suite of commands.

1) In order to switch rooms, you use the ChangeRoom command. It takes at least one parameter, the target room number, so:
CHARACTER.ChangeRoom(ROOMMUMBER);
This can be used for every character and allows you to have the cast of your game switch rooms as you desire. You can also call it with coordinates so that a player is instantly placed where you want them to be.

2) In order to have someone being able to "react" to several inventory items, you just need to expand on the code I posted back up there, by checking for multiple items:

Code: ags

if (player.activeInventory == iTicket)
{
  player.Say("Here is my ticket!");
  cGuy.Say("Okay, move on!");
  player.ChangeRoom(12);
}
else if (player.activeInventory == iBanana)
{
cGuy.Say("I am not hungry.");
}
else
{
  cGuy.Say("No, I don't want that.");
}


The idea is that the script will check all the if clauses to see if it has a response for the item that was used on the character, and only if no match is found, the last "else" is run.

===
These are all very basic commands and you can easily read them up in the manual (press F1 in the AGS editor)- there you will find full examples on how to use them, and what parameters you can set. This is "bread and butter" stuff and I suggest you make sure you understand them; once you get some basics and the control structures down, you'll quickly be able to write very complex scripts without ever having to learn much "harder stuff".

MufasaRKG

#12
Quote from: Ghost on Wed 25/03/2015 05:11:40
Quote from: MufasaRKG on Wed 25/03/2015 04:43:20
When you enter that room that he / she is now in, how can you get the character to been seen through animated view? Otherwise, getting him to move all around on his own in room while you can set certain interactions with him. Are you able to use new inventory items on him and continue to switch his rooms as it follows?

I am not quite sure what you mean with "through animated view"; if you explain what you want to achieve I am sure we can find a solution, though (nod)

As for the rest, all of this is possible. As far as AGS is concerned, the player character and any NPC you create are just "characters", and they all have access to the full suite of commands.

1) In order to switch rooms, you use the ChangeRoom command. It takes at least one parameter, the target room number, so:
CHARACTER.ChangeRoom(ROOMMUMBER);
This can be used for every character and allows you to have the cast of your game switch rooms as you desire. You can also call it with coordinates so that a player is instantly placed where you want them to be.

2) In order to have someone being able to "react" to several inventory items, you just need to expand on the code I posted back up there, by checking for multiple items:

Code: ags

if (player.activeInventory == iTicket)
{
  player.Say("Here is my ticket!");
  cGuy.Say("Okay, move on!");
  player.ChangeRoom(12);
}
else if (player.activeInventory == iBanana)
{
cGuy.Say("I am not hungry.");
}
else
{
  cGuy.Say("No, I don't want that.");
}


The idea is that the script will check all the if clauses to see if it has a response for the item that was used on the character, and only if no match is found, the last "else" is run.

===
These are all very basic commands and you can easily read them up in the manual (press F1 in the AGS editor)- there you will find full examples on how to use them, and what parameters you can set. This is "bread and butter" stuff and I suggest you make sure you understand them; once you get some basics and the control structures down, you'll quickly be able to write very complex scripts without ever having to learn much "harder stuff".

Ok I understand what you mean to reaction to certain items but, in the script you just showed me, when the character changes room, in the new room when you enter it, can that character been seen moving around freely or doing some type of action? Instead of seeing the character just stand there.

-m


EXAMPLE:
cEgo.LockView(5);
cEgo.Animate(3, 1, 0, eBlock, eBackwards);
cEgo.UnlockView();


"This type of action once you get the character to move into a new room."
BUCKET FRESH STUDIOS
Underground Art, Music, & Culture

Ghost

Ah, I get it now- sorry ;-D

To make a character move around without blocking the game, use the Walk command with a "noBlock" paramater. For example:
cGuy.Walk(12,60,eNoBlock);
will make the cGuy character walk to screen position 12,60 and then instantly returning control to the player- otherwise the game would force you to watch him walk all the way while blocking your cursor.

This can be used to have characters walk around a room- you would, in the room's repeatedly_execute function, write code to occasionally set a target position for a character and have him "prowl the room". If this interest you I can provide example code.

Animations and dialog are always possible. You will always have to provide the code in the characters event handlers and make sure they check if certain criteria are met. For example, a "talk" command could check in which room the player is, and depending on the room the guard could provide different comments. This is why if-clauses are your new best friend :D

If you just want a one-time scene playing out, you could even use a cutscene:
Code: ags

	StartCutscene(eSkipESCOrRightButton);
	player.Say("Well, now we are here.");
	cGuy.Say("Yes. That was interesting.");
	EndCutscene();

The benefit here is that you give the player a chance to abort a (lengthy) scene, but keep full control of when you return control to the player.

MufasaRKG


This can be used to have characters walk around a room- you would, in the room's repeatedly_execute function, write code to occasionally set a target position for a character and have him "prowl the room". If this interest you I can provide example code.


"YES PLEASE!!

For example, a "talk" command could check in which room the player is, and depending on the room the guard could provide different comments. This is why if-clauses are your new best friend :D



Can you provide an example script code of how the function will take place place in the character event handler? I'm trying to place test it in this area but doesn not seem to be working.

-m
BUCKET FRESH STUDIOS
Underground Art, Music, & Culture

Ghost

Quick-and-dirty random walk code, Variant A
First create the repeatedly_execute event handler for the room where you want the cGuy character to move around. Then:
Code: ags

function room_RepExec()
{
	// first pick some room coordinates- make sure to keep them within the room's actual size!)
	int xPos = Random(0,320);
	int yPos = Random(180, 210);
	
	// with a chance of 3% we order the character to walk to this position:
	int i = Random(100);
	if (i < 3)
	{
		cGuy.Walk(xPos, yPos, eNoBlock, eWalkableAreas);		
	}

}

This is VERY basic: We never check if the character is already moving before setting a new location, but it's a start. Alternatively you could use a timer which allows you to set a manual delay. But maybe this simple snippet
already is enough.

Quick-and-dirty random walk code, Variant B
Use the FollowCharacter command. This makes one character automatically follow around another, and you can set how far away they stay. Once you call this command the following character will also automatically switch rooms with the player, so if you do not want this, you will have to disable the "follow mode" yourself.
To start "follow mode" just call this code- in your case, just after the character accepts that one inventory item from your player and before the room chance:
Code: ags

// GIVE ITEM
cGuy.FollowCharacter(player, 120);
//ROOM CHANGE COMMAND


And this is how you make cGuy stop following the player around- you would put this into the room's room_leave event handler.
Code: ags

function room_Leave()
{
	cGuy.FollowCharacter(null, 120);
}


Dialog depending on room code
This is a simple template; make sure you actually CREATE the cGuy event handler- the rest you can copy/paste:
Code: ags

function cGuy_Talk()
{
	player.Say("Hey, do you know where we are?");
	cGuy.Say("Hmm...");
	// these answers are picked depending on the room:
	if (player.Room == 10) // the streets
	{
		cGuy.Say("We're at Crosslake Road. Nice place. My favourite bar is here, just to the left.");		
	}
	else if (player.Room == 11) // the factory
	{
		cGuy.Say("This is the Bacon Processing Plant, where all the bacon is made.");
		player.Say("Oh, THAT is the smell!");
	}
	else if (player.Room == 12) // secret room!
	{
		cGuy.Say("We're in the boss lair. Hear that stomping?");
		player.Say("Yes...?");
		cGuy.Say("Well that's the CYBERDEMON we borrowed from DOOM.");
		player.Say("Ohhhhh....");
	}
	else // all other rooms trigger this:
	{
		cGuy.Say("Actually, I have no idea where we are. I have never been here before.");
	}
}


Hope this gets you started. ;)

MufasaRKG

Thank you Ghost. As Im just getting to reading thsi now, I will be trying out the code in a few mins and playing around in areas to test myself. I will post on any stuck areas I fall into. 


-m
BUCKET FRESH STUDIOS
Underground Art, Music, & Culture

MufasaRKG

I can't get the scripts to work, bummer. I believe I'm placing them in the wrong areas for them not to read because I ethier get error line messages or the script being ignored.

-m
BUCKET FRESH STUDIOS
Underground Art, Music, & Culture

Ghost

You need to create all the handlers, you can't just copy them anywhere. AGS always puts them in the right place too, so there's probably some error because you copied code without creating the actual functions within AGS.

Code for the characters goes into the GlobalScript file. Open the Character tab in the editor, open the event handler pane (click the little flash button), find the handler and click the "..." button. This creates the function headers for the event handler, and you can then type in (or actually copy in) any code you want.

For rooms it's the same, only you need to create the function headers from the room pane, and each room has its own script file- room code never goes into the GlobalScript.

If you can't get it to work, feel free to PM me; I can check your code and maybe fix it.
You're not alone here, these are common "beginner's problems" (no offense) and I bet it's something really simple that's going wrong.

MufasaRKG

Quote from: Ghost on Wed 25/03/2015 21:20:26
You need to create all the handlers, you can't just copy them anywhere. AGS always puts them in the right place too, so there's probably some error because you copied code without creating the actual functions within AGS.

Code for the characters goes into the GlobalScript file. Open the Character tab in the editor, open the event handler pane (click the little flash button), find the handler and click the "..." button. This creates the function headers for the event handler, and you can then type in (or actually copy in) any code you want.

For rooms it's the same, only you need to create the function headers from the room pane, and each room has its own script file- room code never goes into the GlobalScript.

If you can't get it to work, feel free to PM me; I can check your code and maybe fix it.
You're not alone here, these are common "beginner's problems" (no offense) and I bet it's something really simple that's going wrong.


I take no offense, It was expected and I'm here to learn as well. Thank you for your time. I will send u a PM in a few.

-m
BUCKET FRESH STUDIOS
Underground Art, Music, & Culture

SMF spam blocked by CleanTalk