Adding variable versus setting it(solved--thankyou<3)

Started by EdLoen, Mon 05/05/2008 21:02:11

Previous topic - Next topic

EdLoen

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

Code: ags

int varname = 0


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

Code: ags

 varname++


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

Code: ags

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:

Code: ags

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

Radiant

You're on the right track. However, consider this:

Code: ags

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:

Code: ags
   
    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.

Code: ags

int varname = 0;

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



There you go.

skuttleman

You could abbreviate the code as thus:

Code: ags

int varname = 0;

if (varname < 4) varname ++;
else
{
   DisplayMessage(#);
   player.Addinventory(inventory[#]);
}

EdLoen

thank you both, If I have any trouble with it I'll check back.

Wonkyth

"But with a ninja on your face, you live longer!"

EdLoen

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


skuttleman

What you need to do is declare the variable at the top of the global script (outside of and before any functions)

Code: ags

// main global script file
int varname = 0;


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

// 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.

EdLoen

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.

SMF spam blocked by CleanTalk