Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Volklaw on Sun 13/05/2018 08:19:23

Title: Using an inventory item to add an interactable object to the environment
Post by: Volklaw on Sun 13/05/2018 08:19:23
Hello there

This is my first question on this forum. I have no doubt it has been answered before. It is probable that to some degree this is explained in the manual, though I think it is the combination of relevant commands required for my solution that is causing me confusion.
I have never scripted anything before, though I am dedicated to learning. I love these types of games and was raised playing KQ, SQ and Monkey Island when I was very young.

1.) My first question is, how do I use an item in my inventory on a hotspot so that the item I used is then placed somewhere within the environment?
2.) My second question is; how do I ensure that by interacting with the item that is now in the environment, that the character undergoes an animation with the item so that they can then undertake an interaction with a hotspot?

My example is as follows:
1) cEgo has picked up a small ladder and it has been added to his inventory (iLadder)
2) cEgo now wants to use the ladder from his inventory on a bookcase so that he can move a book on the top of the bookcase
3) The ladder needs to appear in the room in front of the bookcase
4) The ladder that is now in front of the bookcase needs to be interactive, so that an interaction begins an animation where cEgo climbs the ladder
5) Once at the top of the ladder cEgo needs to be able to interact with the book on the top shelf
6) This interaction moves the book to one side and reveals a locked box
7) cEgo then needs to use a key (iKey) in his inventory on the locked box. This will open the box and retrieve the items inside.

I know that all in all that is probably a reasonable amount of code. I'm not asking for a script for this whole problem. I have managed step number 1. I think if I can understand how to do 2, 3, 4, and 5, that I should be able to do the remaining steps myself.

A big thank you to anyone who can help me with the functions commands that I am missing to get this going.
Title: Re: Using an inventory item to add an interactable object to the environment
Post by: ManicMatt on Sun 13/05/2018 09:24:26
Hey, welcome to the forums!

I hope I don't sound rude, but most, if not all of this, is covered in the AGS tutorial, "Getting Started". If you click on Help at the top of the ags window and click on contents, you'll find a folder called Tutorial with "getting started" inside. I'm pretty sure all your queries are covered in this section. If you follow that and are still not sure, then let us know what troubles you!

Its just the character getting up the ladder that might prove trickier for you, see how you get on with the other bits first.

Edit: Oh, as for not being able to reach the book, a global variable can be created by going to the global variable window in explore. right click in the space, and name your variable, for example gBookread. Set the number to 0, leave the other settings as is.

Now when you go to room and whether its a hotspot or an object, go to its properties window and the tab that's a lightning symbol iirc, click ... on interact, and the script will generate an empty function for you. now you want this script inside the {}

if (gBookread==0)cEgo.Think("I can't reach it.");
if (gBookread==1)cEgo.Think("I read the book.");

those should be on two lines, my phone puts rest of thinking line on next line when it shouldn't be.

Now to change that variable, when the action of the character is going up ladder (maybe someone else can explain how to do that), set the variable to change, so for instance when the interact with ladder has been given a function, much like the book, use this line

gBookread=1;

When he goes down ladder, that variable will need to be changed back to 0, incase player doesn't take book and goes back down.

Notice its two = for checking a variable, and one = for changing it.
Title: Re: Using an inventory item to add an interactable object to the environment
Post by: Volklaw on Sun 13/05/2018 10:41:52
Hi Matt

Thanks for your reply. That stuff regarding the global variables is very helpful. I'll definitely be using that once I've got the item to interact with the hotspot and a result where this imposes the picture/object of the item in the room.
Unfortunately I'm still lost with regards to having cEgo place an item from his inventory into a place on the floor (environment). I read all the getting started tutorial and even the advanced stuff about character scaling etc. I couldn't find anything about using an item on a hotspot. There is stuff about "inventory item hotspots" but that seems to be for changing the part of the item that interacts with a hotspot etc... like, when it's in it's cursor form.

I'm surprised myself that this isn't covered in the getting started tutorial... unless I've been looking at script too much for my novice eyes to handle and i'm just not seeing it somehow :/
I have searched extensively though... I don't think my problems are addressed in the getting started.
Title: Re: Using an inventory item to add an interactable object to the environment
Post by: Cassiebsg on Sun 13/05/2018 11:22:43
The first thing to understand is that you don't literally place an inventory item into the environment.
To place the inventory item into the BG you will need to:
1st: Check which inventory item the player has picked up/is holding and has clicked on a hotspot (in this case let's say hBookcase).
2nd: Make the player lose the item (iLadder)
3rd: Make visible an object that was invisible (let's say oLadder)
4rd: Interact with the oLadder so you can then play your animation of climbing the ladder
5th: Interact with the book (oBook?) and show him reading the book and/or pick it up to inventory (iBook... and and turning oBook to invisible), play climbing down of ladder animation (you can just play the same animation backwards).

Also ManicMatt, don't use gBookread as a variable, it's an acceptable code rule that anything that starts with a g is referring to a GUI. And if you have a variable that can only be 2 things (like 0 or 1) then use a bool and not an int, and set it to true or false accordingly. So a bool variable should probably be named isBookReadable = true/false... much easier than remembering later on what gBookread was and what you agreed with your self what 0 or 1 should be. ;)
Title: Re: Using an inventory item to add an interactable object to the environment
Post by: Volklaw on Sun 13/05/2018 11:36:21
Thank you Cassie!

Your logic there makes perfect sense.
Furthermore I confess I have found a resource to help me through these sorts of problems. Although I couldn't find an answer in the start up guide I highly recommend this youtube video, and others by Densming for problems like mine.

https://www.youtube.com/watch?v=jdOTF_ErXG0&list=PL21DB402CB4DAEAEF&index=27&t=0s
Title: Re: Using an inventory item to add an interactable object to the environment
Post by: ManicMatt on Sun 13/05/2018 14:29:05
Looks like I learned something too Volklaw!

Cassiebsg: Thanks. I might rename some of my variables just incase. They seem to be functioning okay but if it might cause me problems later on.. (and of course rename where said variable is used)

Why use a bool over an int, sorry? Sometimes I have initially intended for a variable to be just 0 or 1, but then found I need it to change to 2 as part of a puzzle. So I understand bool is 0 or 1, as in 0 is false 1 is true, or vice versa, right?

But let's say for arguments sake he later decides he wants his character to stop halfway on the ladder, and change the interaction with the book from "i can't reach it" to "i still can't reach it". He can't use his bool anymore, he'll have to change it to an int.

So honest question, what is the advantage of using a bool? Edit again: ah going on ags engine i see a bool actually says true or false. Is that the only difference? If so I'll stick to ints in most cases, but it's useful to know! :)
Title: Re: Using an inventory item to add an interactable object to the environment
Post by: Cassiebsg on Sun 13/05/2018 15:29:30
First of, think what you want to do. Keeping redoing stuff will result into a game that chances are won't be released... ;)
Personally I rarely use int as variable, only if I really must. If you use an int you need to remember what each value means (when I do have to use an int, I've learn to always comment right in front of the line - every single time I use that variable - what each value means... or you can make an enum to replace the value.) Because when you are debugging, looking at a variable that is 3 will make you scratch your head wondering "What was 3 again?"... Most adventure game puzzles are true/false problems, like you either see it or not, you either can pick/reach or not, etc. If all you need and think you will need is a simple true/false then use it. Can always change it to an int if later you decide you need a third option.

But I'm not a "coder" or experienced in anyway and I hate numbers, so I may be bias...  (laugh) If you happy with using ints and number values and you don't get lost then be my guess. You've made games before. :) I'm talking about my limited experience that I have gained by the few games I've done.

Also using bool you can just use if (isBookinCase) do stuff or  if (!isBookinCase) do stuff. Instead of  if (isBookinCase==3) do stuff... oh wait, what was 3 again? :p
Title: Re: Using an inventory item to add an interactable object to the environment
Post by: ManicMatt on Sun 13/05/2018 15:44:17
Fair point! Oh ive changed my global variables just now, its early days and was a five minute job to find all usages and then remove the g's.

looking at my variables, several of them require more than just false or true, so it's not really an option for me.

For instance, I have a sleeping character. On 0, the main character has no reason to wake him. when he finds out he needs to speak to him, it changes to 1, and he struggles to wake him. you use an inventory item to wake him up, it now becomes 3, and a dialog is now started upon interacting with him.

you probably could have it check multiple bools to achieve this, but that sounds even more complicated to me! :P