Before And After Looping

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Before And After Looping

Post by Epiales »

Okay, as ya know, I have my arena system kinda sorta done :D

What I want is to be able to count the amount of guards and hitpoints both users lose during the battle loops. I thought of earlier, getting the before the loop amounts BEFORE the loops / battle actually starts, and then the AFTER amounts. The problem I'm running into, with my thinking, is that if I create a variable that defines the hitpoints before the battle, then, of course, that variable will change with each loops in the battle, so it would be the same at the end. I think I may be answering myself as I type. Maybe the database is the key, but still wouldn't know how to keep the current hitpoints from changing as the battle went on.

Example:

$beforehitpoints = $currenthitpoints...

But the $currenthitpoints would change when you looped through the battle, so how would I store the original amount of hitpoints that they began with and then at the end, do some simple math to figure out how many hitpoints they lost and echo that to the user? If I just echo the hitpoints BEFORE the loop and then AFTER the loop, it does what I expected it to do; exact same amounts :cry: :cry:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Before And After Looping

Post by Winawer »

Why not just

Code: Select all

$beforehitpoints = $currenthitpoints;
while( isFighting() ) {
    $currenthitpoins -= calcDamage();
}
echo 'original HP: ', $beforehitpoints,  '<br />';
echo 'current HP: ', $currenthitpoints, '<br />';
echo 'HP lost: ', ( $beforehitpoints - $currenthitpoints );
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Before And After Looping

Post by Epiales »

Winawer wrote:Why not just

Code: Select all

$beforehitpoints = $currenthitpoints;
while( isFighting() ) {
    $currenthitpoins -= calcDamage();
}
echo 'original HP: ', $beforehitpoints,  '<br />';
echo 'current HP: ', $currenthitpoints, '<br />';
echo 'HP lost: ', ( $beforehitpoints - $currenthitpoints );
 
I tried something like that but the beforehitpoints, since they equal the currenthitpoints, then when you echo the before - the current, they equal one another out and it is always zero, as the before hitpoints is the same as the currenthitpoints. If that makes sense.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Before And After Looping

Post by Epiales »

Now I tried something like this:

Code: Select all

<?php
$a_arenahitpoints = $stats['arena'];
$updatebeforehpoints="update stats set arena='$stats[hpoints]' WHERE id='$_SESSION[uid]'";
mysql_query($updatebeforehpoints) or die ("Could not update player");
?>

I created that before the loops, and also added a table in the database for the stats arena....

Then I placed this after the fight:

Code: Select all

$losthp = $stats['hpoints'] - $a_arenahitpoints;
It seems to be doing something, as the numbers don't stay the same, but I'm having a hard time figuring out if it's actually working properly because my stats don't update as I need them. They are always one battle, or one hit behind. Plus, as the cronjob runs every minute, it gives more hitpoints, which makes the number sometimes positive and sometimes negative, so it's a big mess if you ask me LOL :evil: :evil: :evil:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Before And After Looping

Post by Epiales »

Winawer wrote:Why not just

Code: Select all

$beforehitpoints = $currenthitpoints;
while( isFighting() ) {
    $currenthitpoins -= calcDamage();
}
echo 'original HP: ', $beforehitpoints,  '<br />';
echo 'current HP: ', $currenthitpoints, '<br />';
echo 'HP lost: ', ( $beforehitpoints - $currenthitpoints );
 
This is what I get with doing exactly what you have put:

You have defeated carl, and have won 8 points!
original HP: 135
current HP: 135
HP lost: 0

See how they zero one another out?
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Before And After Looping

Post by Xaos »

A few options here.

Make just one variable, $hitpoints. Echo this at the beginning. Then run through the loop and etc. wontbe able to use it afterwards however
Make an array and after each 'round' of the fight, add the HP to the array. Ie $hitpoints[0] would be the starting hp.
Echo starting hp, then check if huge loop conditions are met, then change the beginning to current inside of the loop.
Have the two different variable. Use beginning and return it to $x , set $current to $beggining, after each round update $y as current.
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Before And After Looping

Post by Epiales »

Xaos wrote:A few options here.

Make just one variable, $hitpoints. Echo this at the beginning. Then run through the loop and etc. wontbe able to use it afterwards however
Make an array and after each 'round' of the fight, add the HP to the array. Ie $hitpoints[0] would be the starting hp.
Echo starting hp, then check if huge loop conditions are met, then change the beginning to current inside of the loop.
Have the two different variable. Use beginning and return it to $x , set $current to $beggining, after each round update $y as current.
Is that English?

:D :lol: :lol: :D :lol: :lol: :D

You lost me on array LOL! I don't know much about em lol. I'll do some googlin' :mrgreen: :mrgreen:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Before And After Looping

Post by Xaos »

Epiales wrote:
Xaos wrote:A few options here.

Make just one variable, $hitpoints. Echo this at the beginning. Then run through the loop and etc. wontbe able to use it afterwards however
Make an array and after each 'round' of the fight, add the HP to the array. Ie $hitpoints[0] would be the starting hp.
Echo starting hp, then check if huge loop conditions are met, then change the beginning to current inside of the loop.
Have the two different variable. Use beginning and return it to $x , set $current to $beggining, after each round update $y as current.
Is that English?

:D :lol: :lol: :D :lol: :lol: :D

You lost me on array LOL! I don't know much about em lol. I'll do some googlin' :mrgreen: :mrgreen:

I think a huge thing you could do is reassign them to other variables and that way you have a lot of manipulation. Especially if your ot good with arrays
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Before And After Looping

Post by Callan S. »

Are you refreshing your page (the page is reloaded) for this loop? Or it's all done on one page?

I'm not sure why recording the hitpoints into some storage value before you start the loop doesn't solve your problem?
Example:

$beforehitpoints = $currenthitpoints...

But the $currenthitpoints would change when you looped through the battle, so how would I store the original amount of hitpoints that they began with
You store it in $beforehitpoints before you start the loop (not during the loop)
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Before And After Looping

Post by Epiales »

Callan S. wrote:Are you refreshing your page (the page is reloaded) for this loop? Or it's all done on one page?

I'm not sure why recording the hitpoints into some storage value before you start the loop doesn't solve your problem?
Example:

$beforehitpoints = $currenthitpoints...

But the $currenthitpoints would change when you looped through the battle, so how would I store the original amount of hitpoints that they began with
You store it in $beforehitpoints before you start the loop (not during the loop)
No, the page is not refreshed. You click on their name, which takes you to an attack page, and on that page, the battle occurs and the loop occurs. Then at the end it lets you know the results. Well, I'm working on that. But no, it's not refreshed.

And you see the result above where I did the variable beforehitpoints before the loop and then echoed the hitpoints after the loop. They are always the same number, as both variables equal the current hitpoints. Whether before the loop or after, they are the same.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Beginner Help and Support”