Health Meter using a button SOLVED.

Started by R4L, Sat 23/01/2010 17:12:22

Previous topic - Next topic

R4L

OK, so I have the concept down of using a button on a GUI as a meter; it's just implementing it I need help with.

In my script header I imported this function:

Code: ags

// Main header script - this will be included into every script in
// the game (local and global). Do not place functions here; rather,
// place import definitions and #define names here to be used by all
// scripts.

import function AddHealth(int amount) {
  player_health += amount;
  btnHealthMeter.Height = player_health;
}


I keep getting a "cannot define body of imported function" at line 6 however. Is it because I have defined things in the function? Or is something else wrong? Any help is appreciated.

R4L

Matti

Code: ags

import function AddHealth (int amount);


should be all..

Calin Leafshade

yeah you define the contents of a function in the script and the definition in the header.

R4L

Ahhh. OK. I keep thinking you have to define everything in the header first.

Thanks guys.

monkey0506

Just to clarify for you R4L...NEVER define anything in the header (except struct definitions). := What you want to do is use imports to declare items. If you put anything in the header, be it a variable, pointer, instance of a struct, function, or otherwise, a new copy will be created for every script (including room and dialog scripts).

If you put an import then only the import gets copied which will make each script look for the single item being imported.

As I said, the only thing that should be defined in a script header is a struct definition and that's only because that's the only way for other scripts to know what the structure of the struct is...and only necessary if you need the struct (or an instance thereof) to be global.

R4L

#5
Alright. Makes sense. I should know this after all these years, but alas...

EDIT: Something else is wrong now.

Besides the silly fact I used the Height property instead of the Width property, other things have popped up.

I set W and L on the keyboard to decrease and increase the value of player_health. However, if I press W once, the whole bar disappears, and pressing L doesn't bring it back. Even if I set the initial integer player_health to 100, it still happens.

Here's my code in my global script:

Code: ags

int player_health=100;
function AddHealth (int amount){
  player_health += amount;
}

function repeatedly_execute() {
  
  // put anything you want to happen every game cycle, even when
  // the game is paused, here
  HealthMeter.Width = player_health;
  
  if (IsGamePaused() == 1) return;

  // put anything you want to happen every game cycle, but not
  // when the game is paused, here
}


and for the buttons, I just overwrote what was listed under on_key_press:

Code: ags

// GAME COMMAND SHORTCUTS
  if (keycode == 'W') AddHealth(player_health)-1; //Notice this alternate way to indicate keycodes.
  if (keycode == 'L') AddHealth(player_health)+1; //Note that all we do here is set modes.
  if (keycode == 'U') mouse.Mode=eModeInteract; //If you want something else to happen, such as GUI buttons highlighting,
  if (keycode == 'T') mouse.Mode=eModeTalkto; //you'll need some more scripting done.
  if (keycode == 'I') mouse.Mode=eModeUseinv; //But this will, as-is, give you some standard keyboard shortcuts your players will very much appreciate.


I'm not sure what's going on. The code seems fine, I mean it registers the buttons and SOMETHING happens, I just don't know what that thing is.

tzachs

You should replace "AddHealth(player_health) + 1" to "AddHealth(1)" and replace "AddHealth(player_health) - 1 to "AddHealth(-1)".

What you did is adding 100 (player_health) to the width of the button on each key press ('W' or 'L', they both did the same)...

And btw, you should probably look at the Slider, I think it might be more suitable to act as a meter...

R4L

Quote from: tzachs on Sat 23/01/2010 18:09:12
You should replace "AddHealth(player_health) + 1" to "AddHealth(1)" and replace "AddHealth(player_health) - 1 to "AddHealth(-1)".

What you did is adding 100 (player_health) to the width of the button on each key press ('W' or 'L', they both did the same)...

And btw, you should probably look at the Slider, I think it might be more suitable to act as a meter...

Hmm. That's weird. Didn't think it would add that much each time. Thanks mate!

As for the slider, I can easily manage that, but I wanted to be able to use custom images for my meter. I was at first going to use raw draw commands, but I probably wouldn't be able to manage it, as you can see from my subpar scripting skills...

Anyway, that works! Makes sense too now that I think about it. Thanks for all the help guys.  :=

SMF spam blocked by CleanTalk