Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Blondbraid on Mon 07/11/2016 16:39:29

Title: Collecting enough points to proceed
Post by: Blondbraid on Mon 07/11/2016 16:39:29
Hi!
I want to try and make make a game where the player must first click on a number of items in order to proceed.
Basically, they have to click on a high number of things, and then interact with a character, but if they haven't interacted with enough items, interacting with the character will only lead to a message that they haven't collected enough things.

Is there any form of Global variable I can use to add a number to a meter (invisible to the player) or similar whenever the player interacts with a crucial item,
and the script for character only takes effect once the number is high enough?
Title: Re: Collecting enough points to proceed
Post by: Cassiebsg on Mon 07/11/2016 16:43:55
Uhm... sure, just make an int and default it to zero ... :-D Then add +1 to every thing the player needs to do, and check if int == 10 (or whatever value you want.
Title: Re: Collecting enough points to proceed
Post by: Blondbraid on Mon 07/11/2016 17:00:39
Quote from: Cassiebsg on Mon 07/11/2016 16:43:55
Uhm... sure, just make an int and default it to zero ... :-D Then add +1 to every thing the player needs to do, and check if int == 10 (or whatever value you want.
Ok, How do I add +1 in the script? I keep getting errors and I don't know how it's supposed to be written...
Title: Re: Collecting enough points to proceed
Post by: Cassiebsg on Mon 07/11/2016 17:08:07
Code (ags) Select

int something=0; //just to illustrate, as you want this to be a global variable
something++;  // like this?
Title: Re: Collecting enough points to proceed
Post by: Blondbraid on Mon 07/11/2016 17:20:07
Quote from: Cassiebsg on Mon 07/11/2016 17:08:07
Code (ags) Select

int something=0; //just to illustrate, as you want this to be a global variable
something++;  // like this?

I tried writing something++; but just keep getting error messages.
Title: Re: Collecting enough points to proceed
Post by: Cassiebsg on Mon 07/11/2016 17:30:06
Maybe copy/pasting the error msg would be a good idea? (nod)
Title: Re: Collecting enough points to proceed
Post by: Blondbraid on Mon 07/11/2016 17:35:11
Quote from: Cassiebsg on Mon 07/11/2016 17:30:06
Maybe copy/pasting the error msg would be a good idea? (nod)
Code (ags) Select
function hHotspot1_Interact()
{
something++;
}

Failed to save room room1.crm; details below
room1.asc(5): Error (line 5): Expected ';'

Here is the error, I'm not sure what's wrong since there is a ; there.
Title: Re: Collecting enough points to proceed
Post by: Crimson Wizard on Mon 07/11/2016 17:43:20
How and where did you declare "something" variable?

You need to have it declared above first use.

Also, it is much easier to create a variable in the Global Variables panel (from project tree), because when you do that in script you need to keep imports/exports in mind if you want to use same variable in multiple rooms.
Title: Re: Collecting enough points to proceed
Post by: Blondbraid on Mon 07/11/2016 18:10:39
Quote from: Crimson Wizard on Mon 07/11/2016 17:43:20
How and where did you declare "something" variable?

You need to have it declared above first use.

Also, it is much easier to create a variable in the Global Variables panel (from project tree), because when you do that in script you need to keep imports/exports in mind if you want to use same variable in multiple rooms.
I just made a int variable in the Global Variables, and set the value to zero. I tested using int something=0; at the start of the first room but then I got:
room1.asc(26): Error (line 26): Already referenced name as import; you must define it before using it
Title: Re: Collecting enough points to proceed
Post by: Cassiebsg on Mon 07/11/2016 18:14:27
You never answer what line 5 in room1 is. ???
Title: Re: Collecting enough points to proceed
Post by: Blondbraid on Mon 07/11/2016 18:20:38
Quote from: Cassiebsg on Mon 07/11/2016 18:14:27
You never answer what line 5 in room1 is. ???
Line 5 in room 1 was
Code (ags) Select
something++; The same code I added in the post with the error message, should have specified that more.
Title: Re: Collecting enough points to proceed
Post by: Crimson Wizard on Mon 07/11/2016 18:34:27
Quote from: Blondbraid on Mon 07/11/2016 18:10:39
Quote from: Crimson Wizard on Mon 07/11/2016 17:43:20
Also, it is much easier to create a variable in the Global Variables panel (from project tree), because when you do that in script you need to keep imports/exports in mind if you want to use same variable in multiple rooms.
I just made a int variable in the Global Variables, and set the value to zero. I tested using int something=0; at the start of the first room but then I got:
room1.asc(26): Error (line 26): Already referenced name as import; you must define it before using it

Do you have this "something" variable also declared manually in the global or room script? Because if you created it in Global Variables, you need to remove its declaration from other places.

In other words, you either declare it in script yourself, but do so before using it (setting or getting its value ever in scripts), OR you declare it in Global Variables pane, but then you obviously do not need to declare it in script.
Title: Re: Collecting enough points to proceed
Post by: Cassiebsg on Mon 07/11/2016 18:45:36
That seems odd. 8-0
Could you try and delete the line and just write another line of code?  Like Display("This");

Just to see if it throws the same error?
Title: Re: Collecting enough points to proceed
Post by: Blondbraid on Mon 07/11/2016 18:46:29
Quote from: Crimson Wizard on Mon 07/11/2016 18:34:27

Do you have this "something" variable also declared manually in the global or room script? Because if you created it in Global Variables, you need to remove its declaration from other places.

In other words, you either declare it in script yourself, but do so before using it (setting or getting its value ever in scripts), OR you declare it in Global Variables pane, but then you obviously do not need to declare it in script.
I have removed the declaration from the room script and I'm able to start the game now, but I still don't understand how I should add +1.
Title: Re: Collecting enough points to proceed
Post by: Crimson Wizard on Mon 07/11/2016 18:48:46
You add something to variable like this:

Code (ags) Select

something = something + 1; // long elaborate way
something += 1; // shorter way
something++; // this only works with 1, you cannot ++++ for 2 or ++++++ for 3


Where you put this code depends on when do you want this value to increase. For example, on hotspot click, like you tried to do earlier.
Title: Re: Collecting enough points to proceed
Post by: Blondbraid on Mon 07/11/2016 20:02:53
It works now, thank you both for your help! ;-D

I'm also wondering if there are a way to ensure that the +1 only happens once so that the player cannot spam one single hotspot,
how can I disable the function after one use per hotspot?
Title: Re: Collecting enough points to proceed
Post by: Khris on Mon 07/11/2016 20:15:37
function hHotspot_Interact()
{
  if (Game.DoOnceOnly("some arbitrary but unique text")) {
    // interaction code here
    something++;
    ...
  }
  else Display("You already did that.");
}
Title: Re: Collecting enough points to proceed
Post by: Blondbraid on Mon 07/11/2016 20:25:47
Thank you, it worked on the first try! :)