add value

Started by Vincent, Sun 16/11/2014 17:56:15

Previous topic - Next topic

Vincent

Good evening to all AGSer folks.

For the reason that there's not a forum below the beginner section, i will post it here.
I already found a million threads in the forum about this.
But i dunno why still doesn't work,
the evidence sometimes makes bad jokes.
Can someone please enlighten me ? :-\

i simply need to add a value from an integer to another integer.

Code: ags

// 'Mechanics' ASH Script

enum eItems
{
  Chips // potato chips
};


struct Main_Player
{
  float inv_weight;
  short health, money;
  bool IsDead;
};

struct Items 
{
  String name, type;
  float weight;
  short index, count, recover_energy, sprite;
  bool IsCookable;
};


// 
import function init_item();


// import section
import Items oggetto[1000];
import Main_Player protagonista; 



// 'Mechanics' ASC Script
Main_Player protagonista;
Items oggetto[1000];
export oggetto, protagonista; // the export section is at the very end...

function init_item()
{
  /////////////////////////////////// POTATO CHIPS
  oggetto[Chips].index = Chips; 
  oggetto[Chips].sprite = 71; // POTATO CHIPS
  oggetto[Chips].type = "Food";
  oggetto[Chips].name = "Potato Chips";
  oggetto[Chips].recover_energy = 5;
  oggetto[Chips].IsCookable = false;
  oggetto[Chips].weight = 0.5;
  oggetto[Chips].count = 1;
} 


// 'GlobalScript' ASC Script
Main_Player protagonista;
Items oggetto[1000];
export oggetto, protagonista; // 

function game_start() 
{
  init_item(); // calling this work 

  // Set Up Player // This part too
  protagonista.health = 90; // testing chips
  protagonista.inv_weight = 5.5;
  protagonista.IsDead = false;
  protagonista.money = 0;
}


function Repeatedly_execute()
{
  GUIControl *c = GUIControl.GetAtScreenXY(mouse.x, mouse.y); // not used yet
  GUI *g = GUI.GetAtScreenXY(mouse.x, mouse.y);
  if (g == gLifeBar)
  { LabelDescription.Text = String.Format("Health %d %", protagonista.health); }
  else 
  {
    Labeldescription.Text = "";
    Label *l = gDescription.Controls[0].AsLabel;
  }
}

// use inventory on main character
function cCharacter_UseInv()
{
    if (mouse.IsButtonDown(eMouseLeft))
    {
      if (player.ActiveInventory == iChips && protagonista.health < 100)
      {
        protagonista.health += oggetto[Chips].recover_energy; // <---- this doesn't work.
        // player.SayBackground("Nice chips");
        player.ActiveInventory = null;
      }
      else if (player.ActiveInventory == iChips && protagonista.health > 95)
      {
        // player.SayBackground("I am full");
        player.ActiveInventory = null;
      }
    } 
  }
}



basically, in this room test, there is nothing apart of this piece of script..

:EDIT: yes, and i added the function Repeatedly_execute just after the game_start for tasting the label.

Gurok

How do you know it doesn't work? Do you get an error message? Is the value of protagonista.health incorrect when you check it afterwards?
[img]http://7d4iqnx.gif;rWRLUuw.gi

Vincent

Ciao Gurok, thanks for answering.


Quote from: Gurok on Sun 16/11/2014 21:59:13
How do you know it doesn't work?

Well i know that doesn't work by cheking in rep_ex the formatting string :
Code: ags

LabelDescription.Text = String.Format("Health %d %", protagonista.health); 


When i try to add that value to protagonista.health it remain unvariable.
Code: ags

protagonista.health += oggetto[Chips].recover_energy; // <---- this doesn't work.



Firstly, this interaction was under the Useinv on character function (the default one by AGS),
but for tasting again i moved the entire script on rep_ex and nothing (roll)


Quote from: Gurok on Sun 16/11/2014 21:59:13
Do you get an error message?
Nope, any of them when i compile the game. Everything fine :-\

Gurok

#3
Try replacing this line of code:
Code: ags
protagonista.health += oggetto[Chips].recover_energy;


with these three lines:
Code: ags
Display("1: health = %d, recover_energy = %d", protagonista.health, oggetto[Chips].recover_energy);
protagonista.health += oggetto[Chips].recover_energy;
Display("2: health = %d", protagonista.health);


And maybe put the results here.

There are a couple of things you haven't eliminated. You don't know that the code is being executed. You don't know if there isn't something else interfering in the meantime. ^^ That code should answer both questions. e.g. If you don't see the Displays, something about your if statement might be off. If you do see them, you'll quickly see if there's a problem with variables. If there isn't a problem, something else is interfering. Hope this helps.
[img]http://7d4iqnx.gif;rWRLUuw.gi

Vincent

Ciao Gurok, oh well :) i got this !

Code: ags

 // 1: health = 0, recover_energy = 5
 protagonista.health += oggetto[Chips].recover_energy;
 // 2: health = 5



after doing this "operation" the labeldescription show always that protagonista.health is 90 (how i set it in the game start)
NOTE: the labeldescription is unique. and it work for every information when the mouse is over something.
So i'm updating the label like this

Code: ags

Gui*g = Gui.GetAtScreenXY(mouse.x, mouse.y);
if (g == glifebar)
{
  labeldescription.Text = String.Format("Health %d %", protagonista.health);
}



Yes, Displaying the variables was helpful ^^ Thank you :)
But i still dunno how to solve this :-\


If i just do something like this
Code: ags

protagonista.health += 5; 

That work fine. On (UseInv on character) or (rep_ex). And the labeldescription show 95 !
I'm wonder why when i use the item on the character the Display show health 0, then, health 5 but the labeldes show 90 :)

Gurok

#5
Quote from: Vincent on Mon 17/11/2014 01:03:50
I'm wonder why when i use the item on the character the Display show health 0, then, health 5 but the labeldes show 90 :)

Ah, I think it's pretty simple now that I look at your code again. You should remove this:
Code: ags
Main_Player protagonista;
Items oggetto[1000];
export oggetto, protagonista; // 

from GlobalScript.asc

You're alreadhy defining a global variable in the Mechanics script and exporting it in the Mechanics header. That should be fine. I suspect right now, one piece of code is using the GlobalScript protagonista (90) and the other using the Mechanics protagonista (0, then 5).
[img]http://7d4iqnx.gif;rWRLUuw.gi

Vincent

Goddammit. :)

Oh well, it is very too late here in italy, i should go to sleep now.
Tomorrow I MUST test your advice. AND, i really think that the problem it is like that !!! ;-D

Thank youuu again very much, Gurok ^^

Quote from: Gurok on Mon 17/11/2014 00:23:10
There are a couple of things you haven't eliminated.

Maybe i just realize it, i will let you know tomorrow by posting the result here ^^
Ciao, Buona notte

Vincent

Quote from: Gurok on Mon 17/11/2014 01:58:11
Ah, I think it's pretty simple now that I look at your code again. You should remove this:
Code: ags
Main_Player protagonista;
Items oggetto[1000];
export oggetto, protagonista; // 

from GlobalScript.asc

By doing this, now the display show :
Code: ags

 // 1: health = 90, recover_energy = 5
 protagonista.health += oggetto[Chips].recover_energy;
 // 2: health = 95


Vinvin's noob, Gurok saved Vinvin's butt ^^;
Ciao, Grazie :)

SMF spam blocked by CleanTalk