Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: EdLoen on Mon 05/05/2008 21:02:11

Title: Adding variable versus setting it(solved--thankyou<3)
Post by: EdLoen on Mon 05/05/2008 21:02:11
There's one issue that i've come across where just setting the value higher can't cheaply accomplish what I need.  That being that the items need to be collected can be given to the recipent in any order.  And if I were to try to set the values in every conditional order the players can literally do would be setting variables for over 250 variations.

I've been reading, and searching the wiki,manual, and forums and come across some infomation to help me get started on adding the variables like first defining it with somehting like


int varname = 0


and then to add 1 the the value it would be like


varname++


and to add the item at the end i'd need


player.addinventory(inventory[#]);



so with my severe lack of coding understanding (I have a game designers mind not a game programmer's brain) all I could come up with was somehting like this:


int varname = 0;

    if (varname = 0)
          varname++
         
    if (varname = 1)
           varname++
           
    if (varname = 2)
            varname++
           
    if (varname = 3)
              varname++

    if (varname = 4)
              DisplayMessage(#)
              player.addinventory(inventory[#]);
             
             

wich would probably crash the internt and blow up my computer in the process with my luck......

keeping in mind that all other actions (adding scores, additional message, sounds and removing items) are all done with the drop menus
Title: Re: Adding variable versus setting it
Post by: Radiant on Mon 05/05/2008 22:11:01
You're on the right track. However, consider this:


int varname = 0;

    if (varname = 0)
          varname++


First, the "if" command should have a double equals sign ( == ), because comparison is always a double sign. Initialization is a single sign.

Second, every command must end with a semi-colon. So you need to add a ; after the ++

After going through this code, varname will be equal to 1. Therefore, if the next code is this:

   
    if (varname = 1)
           varname++


...the game will think "hey, varname is equal to one, so let's increment it again!" and now varname is equal to two.
           
The solution is very simple, and it's called else.


int varname = 0;

    if (varname == 0)
          varname++;
   else if (varname == 1)
           varname++;



There you go.
Title: Re: Adding variable versus setting it
Post by: skuttleman on Mon 05/05/2008 22:30:56
You could abbreviate the code as thus:


int varname = 0;

if (varname < 4) varname ++;
else
{
   DisplayMessage(#);
   player.Addinventory(inventory[#]);
}
Title: Re: Adding variable versus setting it
Post by: EdLoen on Tue 06/05/2008 04:28:21
thank you both, If I have any trouble with it I'll check back.
Title: Re: Adding variable versus setting it
Post by: Wonkyth on Tue 06/05/2008 10:28:25
helped me to
thanks ;D
Title: Re: Adding variable versus setting it
Post by: EdLoen on Thu 08/05/2008 22:30:32
ok, i'm anoyed with it...  the game wont run without int varname = 0  in the same lines and the rest of it, but if it's there it'll keep setting it the varible to 0 every time i give the new item.  I tryed it with the starting value at 4 and it displayed and give the item correctly, so all I can think of is its resetting to 0 each time.

I did think that mabe it wont do it if the varibale is equal to what it's looking for, so I set the other line to lessthan 3, but no it's probably resetting to 0 with each item.

I tried putting the int elsewhere to call it (like when you first walk into the room or talk with the character) but it errors because it's not with the rest of the code.

If I have to run one huge script for all the item interactions instead of using the drop menu condition branches for each individual one let me know.

this and a timer issue is what holding me up from getting an alpha tester

Title: Re: Adding variable versus setting it
Post by: skuttleman on Thu 08/05/2008 22:41:05
What you need to do is declare the variable at the top of the global script (outside of and before any functions)


// main global script file
int varname = 0;


Then in a function that runs when the player picks up the item...

// function where the player picked up one of the items
function player_picked_up_one_of_the_items()
{
  if (varname < 4) varname ++;
  else
  {
     DisplayMessage(#);
     player.Addinventory(inventory[#]);
  }
}


That should keep the variable from being reassigned to 0. Hope that helps.
Title: Re: Adding variable versus setting it
Post by: EdLoen on Thu 08/05/2008 23:00:00
ok, now it works.  Still had to drop the lessthan value to 3 (for all 4 items to be given).  And I wrote a note down to remember that for later.  Thank you.