Jump to content

Recommended Posts

Well, I am working on an RPG script right now and I wanted this:

 

Member searches for enemy -

 

Finds enemy-

 

Different enemies have different health, attack, etc -

 

If a member damages an enemy I want to update the enemy's stats so that it reflects the damage, but I do not want to use a database or anything. I am currently just echoing the enemies stats like

<strong>Enemy:</strong>

<strong>Attack:</strong>

 

Etcetera. But how could I reload the page with the random enemy (I'm using mt_rand() ) still selected and change its health to reflect damage.

 

I don't know if this makes sense.  :'(

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/
Share on other sites

Umm yea dont use get or post for that, umm if you dont want to do database your best bet is use $_SESSION

 

How would I use SESSION to reflect damage and health decrements?

 

yes they could modify it. If you dont want them to access it use a form and POST the data

 

<input type="hidden" name="damage" value="65" />

 

 

Then access it like this

$_POST['damage'];

 

I would have to have a Submit button for this, right? I think it would look awkward having a little "Confirm Damage" button or something.

Could I utilize JS for this at all? Maybe....when they click the ok button on an alert popup then something happens? I don't know. This is making me go insane.

 

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-905540
Share on other sites

Also, I am reeeeeally bad at $_GET.

If you're bad at $_GET (and I'm not even sure what that means), then you're in a world of hurt.  You add values to the URL after a ? in the form of param=value and separate them with an ampersand.  Then you access them in your page with $_GET.  What's so hard about that?

 

yes they could modify it. If you dont want them to access it use a form and POST the data

<input type="hidden" name="damage" value="65" />

That can be modified just as easily as GET.

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906055
Share on other sites

As roopurt said, if you are afraid of the user modifying the information, then both POST and GET would probably be a bad idea as the user could easily modify both if they have the know how.

 

I would suggest using javascript to make it much more dynamic, so the user doesn't have to refresh the page everytime he does one attack. However, judging from the fact that you don't know javascript, and you have trouble with get variables, this may be a daunting task.

 

I would suggest trying a smaller project to get the fundamentals of PHP down before you try tackling an RPG, but if you must make an RPG, I would strongly suggest some sort of Javascript/Ajax attack system.

 

but I guess you could always use sessions too... just create session vars for the health and damage or whatever else you need, and whenever you preform an attack, decrement the value of the session variable

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906058
Share on other sites

I understand the URL format for $_GET but what I don't understand how to use $_GET. I don't know why but I can't wrap my mind around it. Someone once gave me this code:

<?php 
if(!array_key_exists , 'page'){

//A WHOLE BUNCH OF $_GET STUFF

}
?>

 

(Or something along those lines) And I had no idea how to make $_GET to work...I just used the code and it worked.

 

I do not know why I can't understand it. I could do it with forms and understand it, but not with other things.

 

@mikesta

Well I get other things about PHP, just not $_GET and I never needed to teach myself JS so I didn't. If I need it now I will learn it.

 

I will also try and learn more about $_GET before moving on, but I probably will have issues with it like I did when I started PHP.

 

 

So, with sessions, I could start a session when they find an enemy, and then what?  :shrug:  :-\  :shy:

 

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906060
Share on other sites

create a few session vars, for whatever data you need to keep and update. like

#after you get the monster stuff
$_SESSION['mon_health'] = $monster_health;
$_SESSION['mon_name'] = $monster_name;
#etc.

 

 

as far as get goes, its mad ease.

 

say I have a URL that goes

www.mysite.com/food.php?id=tacobell

 

id is the get variable, and tacobell is the value of said get variable. To access the variable, on food.php you write something like

$fastfood = $_GET['id'];
echo "You are eating at " . $fastfood;

Usually to pass get variables to a page you have to create a hyperlink, and write it as the URL i wrote above, so for example, If I made a link that looked like

<a href="food.php?id=tacobell">Taco Bell</a>

 

the food.php page would look like:

 

You are eating at tacobell.

 

if I changed the URL to say food.php?id=Mcdonalds the page would then say

 

You are eating at Mcdonalds

 

 

Make sense?

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906071
Share on other sites

Nope, its defined in the URL itself.

 

So when you go to the page: www.mysite.com/food.php?id=tacobell

 

a get variable with the key 'id' is created.

 

similarly if you went to

www.mysite.com/food.php?socks=tacobell

 

a get variable named socks would be created, which could be accessed the way I posted above.

 

You can think of the $_GET global as an array, much like the following

$arr = new Array();
$arr['my'] = "array";
$arr['is'] = "awesome";

 

And when you go to a URL that is like

 

mysite.com/food.php?id=tacobell

 

its as if you wrote the PHP

 

$_GET['id'] = "tacobell";

 

Make more sense?

 

As you see, since you define the variables in a URL, any user can theoretically create or alter your get variables, so its best not to use them for passing sensitive data to the script, and you should always sanitize get variables before you do anything with them.

 

 

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906118
Share on other sites

well, if you are just beginning I would hold off until you understand the fundamentals of the HTML DOM, and some Ajax functionality (That doesn't mean don't learn JavaScript. JavaScript is awesome. So dam awesome). However, as for your problem, I would suggest using sessions for now. You can always change things at a later date.

 

 

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906131
Share on other sites

Oooh. Okay.

 

 

I am using $_SESSION, but I get the white screen of doom. If I already have a session running for usernames, etc. do I need to cancel it before starting a new one? Also, can I just add in the new $_SESSION variables to the existing session?

 

 

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906163
Share on other sites

yeah you just need to create a new session. No need to destroy any other sessions. Make sure you have session_start() at the beginning of your page though.

 

creating a new session is pretty straight forward, just like

$_SESSION['newSess'] = "New Value";

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906167
Share on other sites

Yeah, that's what I did. My code must have an error, then.


<?php
//there is other stuff before this so the $row thing IS right. =) 

while($row=mysql_fetch_array($result)){ 
echo "<strong>".$row['name']."</strong><br/>
<strong>".$row['species']."</strong><br/>
<strong> Health: ".$row['health']."</strong><br/>
<strong> Attack: ".$row['attack']."</strong><br/>
<strong> Defense: ".$row['defense']."</strong><br/>
<strong>"; 

	if($row['species']=="Alien"){ 
		echo "Stealth: ".$row['stealth']; 
	} 

	if($row['species']=="Marine"){ 
		echo "Melee: ".$row['melee'];
	}

echo "</strong>
<strong>Money: ".$row['money']."</strong>";
echo "</td><td>";

	if(!$_POST['choose']){
		echo "Choose an Enemy to Fight 
		<form action='fcenter.php?page=planet1' method='post'>
		<select size='4' name='fight'>
		<option value='Aliens'>Aliens</option>
		<option value='Marines'>Marines</option>
		<option value='Native Animals'>Native Species</option> 
		<option value='Totally Random'>Randomize</option>
		</select> 
		<input type='submit' name='choose' value='Search'/> 
		</form>"; 
	}

	if($_POST['choose']){ 

		if($_POST['fight']=="Aliens"){ 
			$randA = mt_rand(1,20);

			if($randA=="1" || $randA=="7" || $randA=="2" || $randA=="20"){ 
				echo "You have not found an enemy to fight. Please try again."; 
			} 

			if($randA=="3" || $randA=="6" || $randA=="11" || $randA=="8" || $randA=="16"){ 
                                         // HERE ARE MY NEW SESSIONS 


                                        $_SESSION['attackDrone']=25; 
                                        $_SESSION['defenseDrone']=20; 
                                        $_SESSION['healthDrone']=20; 

				echo "<strong>Enemy:</strong> Drone Alien<br/> 
				<strong>Attack:</strong> ".$_SESSION['attackDrone']."<br/>"
				<strong>Defense:</strong> ".$_SESSION['defenseDrone']."<br/>"
                                        <strong>Health:</strong> ".$_SESSION['healthDrone']."<br/>"
				<strong>Money for Kill:</strong> 100<br/> 
				<strong>Experience:</strong> 10<br/>";

				if(!$_POST['confirm']){
					echo "<form action='fcenter.php?page=planet1' method='post'>
					<input type='radio' name='attack' value='Attack'/>Attack<br/>
					<input type='radio' name='attack' value='Flee'/> Flee <br/> 
					<input type='submit' name='confirm' value='Choose'/>
					</form> "; 
				}

				if($_POST['confirm']){ 

					if($_POST['attack']=="Attack"){ 
						$i = $row['attack'];	
						if(intval($i) > 20){
                                                                $dam = $row['attack']-$_SESSION['defenseDrone'];
                                                                $_SESSION['healthDrone']=$_SESSION['healthDrone']-$dam; 
							echo "Testing HellO! ";
						}

						if($i<20){ 
							echo "Hi"; 
						}
					} 

					if($_POST['attack']=="Flee"){ 
						echo "Hi";  
					}

				}


			}
		}
	}
}

echo "</td></tr></table>";
?>		

 

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906168
Share on other sites

I fixed my fatal error!!!! Now, I have a new problem:

 

<?php
if($_POST['confirm']){ 

					if($_POST['attack']=="Attack"){ 
						$i = $row['attack'];	
						if(intval($i) > 20){
                                                                $dam = $row['attack']-$_SESSION['defenseDrone'];
                                                                $_SESSION['healthDrone']=$_SESSION['healthDrone']-$dam; 
							echo "Testing HellO! ";
						}

						if($i<20){ 
							echo "Hi"; 
						}
					} 
?>

 

When this part executes it does NOTHING. It just redirects me to the page I'm on....

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906385
Share on other sites

Would an improper form do that?

 

<?php
				if(!$_POST['confirm']){
					echo "<form action='fcenter.php?page=planet1' method='post'>
					<input type='radio' name='attack' value='Attack'/>Attack<br/>
					<input type='radio' name='attack' value='Flee'/> Flee <br/> 
					<input type='submit' name='confirm' value='Choose'/>
					</form> "; 
				}

				if(isset($_POST['confirm'])){ 

					if($_POST['attack']=="Attack"){ 
                                                         $i = $row['attack'];	
						if(intval($i) > 20){
                                                                $dam = $row['attack']-$_SESSION['defenseDrone'];
                                                                $_SESSION['healthDrone']=$_SESSION['healthDrone']-$dam; 

							echo "Testing HellO! ".$_SESSION['healthDrone'];
						}

						if(intval($i) < 20){ 
							echo "Hi"; 
						}

					} 

					else if($_POST['attack']=="Flee"){ 
						echo "Hi";  
					}

				}
?>

Link to comment
https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906409
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.