Jump to content

[SOLVED] Problem with variables?


Kryllster

Recommended Posts

How do I assign a variable that has 2 other varibales that are predifined in config files.

 

First is the config file a weapon

<?php
// level 1 weapons
if($weapon == "Eastwind Katana" || $weapon == "Robin Bow" || $weapon == "Blue Ice Staff"){
$base_bonus = 2;
}

// level 2 weapons
if($weapon == "Aldonian Spatha" || $weapon == "Cobalt Bow" || $weapon == "Fire Staff"){
$base_bonus = 4;
}

//level 3 weapons
if($weapon == "Hitzman Claymore" || $weapon == "Fire Bow" || $weapon == "Plaz Staff"){
$base_bonus = 6;
}

// starter weapon
if($weapon == "Big Club"){
$base_bonus = 2;
}
?>

 

As you can see I have 2 variables the $weapon then $base_bonus now what I want to do is something like this

 

$attack = $weapon['$base_bonus'];

 

I know thats probably wrong but I am just trying to understand this.

 

Thanks in Advance,

Link to comment
Share on other sites

Hi Kryllster

 

As I understand:

 

1. check the name of the weapon, and find its corresponding $base_bonus

 

2. Assosiate that $base_bonus to something for future use.

 

It is done like so, using arrays:

 


<?php

//create the weapon array with the name of the weapon
$weapon = array('name'=>'weapon name');

//declare the $base_bonus as 0, innitially
$base_bonus = 0;


//evaluate the base_bonus (this is a tedious way of doing it, especially when using a config file, but that is not the issue you raised)

// level 1 weapons
if($weapon['name'] == "Eastwind Katana" || $weapon == "Robin Bow" || $weapon == "Blue Ice Staff"){
   $base_bonus = 2;
}

// level 2 weapons
if($weapon['name'] == "Aldonian Spatha" || $weapon == "Cobalt Bow" || $weapon == "Fire Staff"){
   $base_bonus = 4;
   }

//level 3 weapons
if($weapon['name'] == "Hitzman Claymore" || $weapon == "Fire Bow" || $weapon == "Plaz Staff"){
   $base_bonus = 6;
   }

// starter weapon
if($weapon['name'] == "Big Club"){
   $base_bonus = 2;
   }

//assign the $base_bonus the the weapon array as well, seeing as that is determined now.
$weapon['base_bonus'] = $base_bonus;


//to access the weapon array keys:
$name = $weapon['name'];
$base_bonus = $weapon['base_bonus'];
?>

 

Hope this helps

Link to comment
Share on other sites

You may want to consider storing the weapon data somewhere else, like say a XML file.  That way, instead of having to do these kinds of contortions to obtain the data and hard code the base values into your code, you can simply read in the data from the external source.  It'll make your game more flexible as you'd only have to modify the file to add/remove/change weapon data.  XML, in particular, is a great way to store this info given its hierarchical nature.  You could have a file with the following layout (rough sketch):

 

<weapons>
   <weapon>
      <name>Big Club</name>
      <baseBonus>2</baseBonus>
      <description>It's big, it's clumsy, but it hurts when it hits</description>
   </weapon>
      <name>Eastwind Katana</name>
      <baseBonus>2</baseBonus>
      <description>Forged by the warrior monks of the Hidden Vale</description>
   </weapon>

   <!-- etc -->
</weapons>

 

Simply access the file when needed.

 

Also, this kind of project is perfect for learning OOP.  Items (weapons, armor, misc) map to objects naturally, as do things like status effects and special attacks/spells.

Link to comment
Share on other sites

How would I go about reading the data from an xml file and applying it in this scenario. Where is a good tutorial on this subject as I would like to learn the syntax for it??

 

Thanks for the replies it does show me more where my next steps in learning php need to go!

Link to comment
Share on other sites

How would I go about reading the data from an xml file and applying it in this scenario. Where is a good tutorial on this subject as I would like to learn the syntax for it??

 

Thanks for the replies it does show me more where my next steps in learning php need to go!

 

As always, PHP's online documentation is the place to start.  In particular, look at SimpleXML.

Link to comment
Share on other sites

Ok Im reading here http://www.php.net/manual/en/simplexml.examples.php

 

And so far I have this xml code in a file called xmlweapons.php and it looks like this

<?php
$xmlweapons = <<<XML
<?xml version='1.0' standalone='yes'?>
<weapons>
<weapon>
<name>Eastwind Katana</name>
    <basebonus>2</basebonus>
    </weapon>
    <weapon>
    <name>Robin Bow</name>
    <basebonus>2</basebonus>
    </weapon>
    <weapon>
    <name>Blue Ice Staff</name>
    <basebonus>2</basebonus>
    </weapon>
    <weapon>
    <name>Aldonian Spatha</name>
    <basebonus>4</basebonus>
    </weapon>
    <weapon>
    <name>Cobalt Bow</name>
    <basebonus>4</basebonus>
    </weapon>
    <weapon>
    <name>Fire Staff</name>
    <basebonus>4</basebonus>
    </weapon>
    <weapon>
    <name>Hitzman Claymore</name>
    <basebonus>6</basebonus>
    </weapon>
    <name>Fire Bow</name>
    <basebonus>6</basebonus>
    </weapon>
    <name>Plaz Staff</name>
    <basebonus>6</basebonus>
    </weapon>
    <name>Big Club</name>
    <basebonus>1</basebonus>
    </weapon>
</weapons>
XML;
?>

so would I use a for loop to get the data and basebonus from the file or is there something easier Im misssing cause I dont really understand for loops or anything like that but just thought it might apply here??

 

Thanks

 

Forgot to add that I have another player config that gets info from a database and it is just in this form

 

$weapons = $row['weapons'];

Link to comment
Share on other sites

I suggest storing your XML data in pure xml format, meaning 'weapondata.xml', and looking like this:

 

<?xml version='1.0' standalone='yes'?>
<weapons>
   <weapon>
   <name>Eastwind Katana</name>
    <basebonus>2</basebonus>
    </weapon>

    <weapon>
    <name>Robin Bow</name>
    <basebonus>2</basebonus>
    </weapon>

    <weapon>
    <name>Blue Ice Staff</name>
    <basebonus>2</basebonus>
    </weapon>
  
</weapons>

 

Now to access that file in php using simpleXML (code not tested):

 

<?php

//create a variable to store the xml data you're going to import from the file (in the same dir. as your script):
$xml = simplexml_load_file('weapondata.xml');

//this creates a simpleXML object that can be worked with like so:

//create an empty array:
$weapon_array = new array();

//for each weapon/item in $xml:
foreach($xml->weapon as $weapon){

//get the info from the simpleXML object:
$name = $weapon->name;
$bonus = $weapon->basebonus;

//add them to the weapon array:
$weapon_array[$name] = $bonus; 
//here having the weapon name as the key of the array,
//and the base bonus thing as the value.

}

//and then printing the data:

echo "<h3>Weapon data:</h3><br /><br />";

foreach($weapon_array as $name=>$bonus){
echo "Name: {$name}. Bonus: {$bonus}<br />";
}

?>

Link to comment
Share on other sites

I suggest storing your XML data in pure xml format, meaning 'weapondata.xml', and looking like this:

 

<?xml version='1.0' standalone='yes'?>
<weapons>
   <weapon>
   <name>Eastwind Katana</name>
    <basebonus>2</basebonus>
    </weapon>

    <weapon>
    <name>Robin Bow</name>
    <basebonus>2</basebonus>
    </weapon>

    <weapon>
    <name>Blue Ice Staff</name>
    <basebonus>2</basebonus>
    </weapon>
  
</weapons>

 

Now to access that file in php using simpleXML (code not tested):

 

<?php

//create a variable to store the xml data you're going to import from the file (in the same dir. as your script):
$xml = simplexml_load_file('weapondata.xml');

//this creates a simpleXML object that can be worked with like so:

//create an empty array:
$weapon_array = new array();

//for each weapon/item in $xml:
foreach($xml->weapon as $weapon){

//get the info from the simpleXML object:
$name = $weapon->name;
$bonus = $weapon->basebonus;

//add them to the weapon array:
$weapon_array[$name] = $bonus; 
//here having the weapon name as the key of the array,
//and the base bonus thing as the value.

}

//and then printing the data:

echo "<h3>Weapon data:</h3><br /><br />";

foreach($weapon_array as $name=>$bonus){
echo "Name: {$name}. Bonus: {$bonus}<br />";
}

?>

 

Ah, beat me to the punch. :P

 

To the OP, a couple of things -

 

1. You should brush up on your loops.  They're integral to coding in general, and all have their uses, even the erstwhile do...while() loop.

 

2. Storing things in a database vs. XML files has its pros and cons.  A database makes it easy for your script to add/edit/delete info.  It should be used for storing dynamic data - users, a particular player character in your game, a character's inventory, etc.  XML should be used for data that is more static - the abilities available to a particular character class, base character stats, their leveling stats, etc.  Application-wide data that doesn't change very often.  For this kind of data, which you typically only need to read once and don't have to bother with saving anything after its use, it's far more economical to get it directly from a XML file rather than fetching it from a database.

 

Weapon data could fit equally well in both storage mechanisms.  How you use it, and how often you need to modify it should be considerations.  Example:

 

My game (don't we all try writing one? ;) ) stores items in the db.  Why?  Because I need to be able to relate that info to individual player characters' inventories.  My character classes, including the attacks available to characters of that class at certain levels, are stored in XML files.  I only need to read that info at certain times, and I don't need to relate attacks to characters as all characters of a certain class will have the same attacks available to them at the same level.

 

The point of all this is to realize you have options, and to make you informed of them so you can make a decision based on knowledge.  There isn't a one-size-fits-all way to address this kind of problem.  Instead, you'll need to use the best tools for the right part of the job.

Link to comment
Share on other sites

Ok I'm reading and trying different things but I can't even get it to print out the data from the xml file. Looking at your example code it seems to me it should work I changed the new array(); to array(); and got "Weapon data:" to print out after that nothing. I have spent the better part of my day wracking my brains trying to figure this one out. Could I possibly see some code that works? Thanks for turning me on to this I understand foreach loops and arrays better now because of this. If anything I can make a config with the new info I learned but I would rather use the xml way??

Link to comment
Share on other sites

Ok I sent it over to my linux box and this is what I got maybe this will help?

 

Warning: simplexml_load_file() [function.simplexml-load-file]: weapondata.xml:33: parser error : Opening and ending tag mismatch: weapons line 2 and weapon in /var/www/testing/weapontest.php on line 3

Warning: simplexml_load_file() [function.simplexml-load-file]: </weapon> in /var/www/testing/weapontest.php on line 3

Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /var/www/testing/weapontest.php on line 3

Warning: simplexml_load_file() [function.simplexml-load-file]: weapondata.xml:34: parser error : Extra content at the end of the document in /var/www/testing/weapontest.php on line 3

Warning: simplexml_load_file() [function.simplexml-load-file]: <name>Plaz Staff</name> in /var/www/testing/weapontest.php on line 3

Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /var/www/testing/weapontest.php on line 3

Notice: Trying to get property of non-object in /var/www/testing/weapontest.php on line 10

Warning: Invalid argument supplied for foreach() in /var/www/testing/weapontest.php on line 10

 

Looking it up on google but its very cryptic to me!!

Link to comment
Share on other sites

After much reading and messing around with it I finally got it. made some simple additions such as "" on line 17. Anyway here is the code. And thanks once again for showing me this.

 

<?php
//create a variable to store the xml data you're going to import from the file (in the same dir. as your script):
$xml = simplexml_load_file('weapondata.xml');
//this creates a simpleXML object that can be worked with like so:

//create an empty array:
$weapon_array = array();

//for each weapon/item in $xml:
foreach($xml->weapon as $weapon){

//get the info from the simpleXML object:
$name = $weapon->name;
$bonus = $weapon->basebonus;

//add them to the weapon array: * Had to add quotes to $name
$weapon_array["$name"] = $bonus; 
//here having the weapon name as the key of the array,
//and the base bonus thing as the value.

}

//and then printing the data:

echo "<h3>Weapon data:</h3><br /><br />";

foreach($weapon_array as $name=>$bonus){
// Removed the {} from $name and $bonus
echo "Name: $name. Bonus: $bonus<br />";
}

?>

I kept getting this error:

Warning: Illegal offset type in /var/www/testing/weapontest.php on line 17

 

so the solution was found here:http://us2.php.net/types.array

 

Way cool!!

Link to comment
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.