"Stacking" items

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
Sebastian
Posts: 31
Joined: Sun Dec 11, 2011 8:21 pm

"Stacking" items

Post by Sebastian »

Hi lads
I have a little problem, so im trying to make this game with halls tuts and i was working on inventory. TBH i dont like idea that if you buy 20, lets say, daggers when you go to inventory there is 20 daggers displayed. So i was trying to do some kind of stacking. My first idea was simple:
before item will be put into database
ill check if there is an item with the same name already
if there is, i wont add new object but just update "quantity field in old one
...but than i was like hmmm what about randid ? if i do it this way there will be one randid for all daggers in your inventory, is that safe?
My second thought was:
leave it like it is, so create object in database for every dagger you buy and keep them with seperate randid.
change the way of displaying them in inventory. So when you want to check whats in your bag instead of displaying 20 daggers it will count have many daggers you have and display one with 20 in "quantity". But i wasnt sure how to code it and also what will happen if you decide to sell one - which one will it pick up?
HELP :D
Also im having problem with using "SELECT SUM" I got:

Code: Select all

$totalarmor="SELECT SUM(rating) as armor from playerarmor where equip=1 AND pid='$pid'";
$totalarmor2=mysql_query( $totalarmor) or die("could not get player armor!");
but when i echo $totalarmor im getting "Resource id #7" 7 is ok but where i get "resource id" from ?
could someone clear it out for me ::D
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: "Stacking" items

Post by SpiritWebb »

Can we see the whole code please
Image

Image
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: "Stacking" items

Post by Torniquet »

I have made a series of tutorials similar to halls some time ago which you can find here http://www.youtube.com/playlist?list=PL1675F1D3FE4A0A65

Part 2.x shows 'hunting' and gathering items which are stacked. part 4.x show constructing the inventory.
New Site Coming Soon! Stay tuned :D
Sebastian
Posts: 31
Joined: Sun Dec 11, 2011 8:21 pm

Re: "Stacking" items

Post by Sebastian »

Torniquet
Thanks! good to know, ur style is a little bit different than halls, will check it out soon (still at work).
SpiritWebb
Thats most of code for SUM beside ECHO at the end. Well here is the whole file anyway, some of it is in polish ;) Its just to test formulas and show players stats.

Code: Select all

<?php
include_once 'connect.php';
session_start();
include 'topmenu.php';
?>
<link href="style.css" rel="stylesheet" type="text/css" />
<div id="srodek">
<?php
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
$pid = $playerinfo3['id']; 

$weaponinfo="SELECT * from playerweapons where equip=1 AND pid='$pid'";
$weaponinfo2=mysql_query( $weaponinfo) or die("could not get player weapon!");
$weaponinfo3=mysql_fetch_array( $weaponinfo2);

$sila = $playerinfo3['attack'];
$obrona = $playerinfo3['defense'];
$zrecznosc = $playerinfo3['dex'];
$wytrzymalosc = $playerinfo3['maxhp'];
$inteligencja = $playerinfo3['maxspoints'];
$mindmg = $weaponinfo3['damage'];
$maxdmg = $weaponinfo3['maxdamage'];
$minobrazenia = $mindmg * (1+$sila/10);
$maxobrazenia = $maxdmg * (1+$sila/10);
$minobrazenia = (int)$minobrazenia;
$maxobrazenia = (int)$maxobrazenia;

$totalarmor="SELECT SUM(rating) as armor from playerarmor where equip=1 AND pid='$pid'";
$totalarmor2=mysql_query( $totalarmor) or die("could not get player armor!");


echo "<center><u><b> " . $playerinfo3['name'] . "</b></u><br></center>";
echo "Sila = $sila<br>";
echo "Obrona = $obrona<br>";
echo "Zrecznosc = $zrecznosc<br>";
echo "Wytrzymalosc = $wytrzymalosc<br>";
echo "Inteligencja = $inteligencja<br><br>";

echo "Armor = $totalarmor2<br>";   ///echo for SELECT SUM

echo "Obrazenia = $minobrazenia do $maxobrazenia<br>";


?>
</div>
<div id="downmenu">
<?php
include 'downmenu.php'
?>
</div>
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: "Stacking" items

Post by Torniquet »

Sebastian wrote:Torniquet
Thanks! good to know, ur style is a little bit different than halls, will check it out soon (still at work).
If you get stuck on any of it, feel free to ask. Although our styles may differ, the logic should be easily put into the code you already have.
New Site Coming Soon! Stay tuned :D
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: "Stacking" items

Post by Winawer »

You're missing a mysql_fetch_array (or similar):

Code: Select all

$totalarmor="SELECT SUM(rating) as armor from playerarmor where equip=1 AND pid='$pid'";
$totalarmor2=mysql_query($totalarmor) or die("could not get player armor!");
$totalarmor3=mysql_fetch_array($totalarmor2);
echo $totalarmor3['armor'];
 
Sebastian
Posts: 31
Joined: Sun Dec 11, 2011 8:21 pm

Re: "Stacking" items

Post by Sebastian »

Will check as soon as im home, should work, thanks a lot!

Torniquet
Will do! You know ive started working on this project and at the beginning i was quite sure what i wanted to do and how to do it...the further i went into it the more questions and ideas i had..and after a while the code is getting bigger and bigger and changing or adding anything takes ages!...now all i got in my head is just big chaos :D new ideas create more problems ;) I think ill have to pause, sit down again and make a detailed plan of what im gonna have in a game and what i have to do in what order.
Im suprised how helpful people are here, learning coding with ya is just a pleasure!
and i love the project station invasion too! will follow for sure and maybe "grab" some ideas (for coding;)

regards
Sebastian
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: "Stacking" items

Post by Torniquet »

It is supprising how quickly a 'simple this will be easy to do' idea becomes a 'holy shit there is more to do than i thought ARGHHH' idea lol.

Glad you like the PSI series, unfortunately I cannot find the time to sit there and record more recently :( and since i started working full time as a web developer, going home and doing the same thing is annoyingly boring lol. Also the lack of views i receive gets abit demotivating at times aswell :(
New Site Coming Soon! Stay tuned :D
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: "Stacking" items

Post by Torniquet »

Glad it is helping you nicely :)

The items database i would remove all of the columns
++++ weapon ++++
++++ hands ++++
++++ feet ++++
++++ body ++++
++++ head ++++
++++ usable ++++

In mine i do beleive i used a 'type' field, where a certain number depicted the type of item it was. 1=hands, 2=feet etc. it doesnt do much change in the way of the size of the database, but its more of a personal preference and can help better if you are searching by item types.

Code: Select all

$query = "SELECT * FROM items WHERE type IN (2,4)";
 
would select all items with a type of 2 or 4. aposed to...

Code: Select all

$query = "SELECT * FROM items WHERE shield=1 AND weapon=1";
 
producing user searches that way is alot more hassel than the one above that. So it is something to consider if you was to need that kind of functionality. (using 'IN' btw i have not long learnt lol and may be able to ustilise it in more places on the tut lol)

modifying items is abit trickier with this kind of structure.

Off the top of my head, i would have another field on the table called 'unique_mark'. when you go to insert a modified item, force a new item into the database with an additional 'unique mark' attached to the weapon. do not use the 'on duplicate..' This way when you go adjust the qty, you specify that the unique mark field is to be blank or 0. depending how you wanted to mark them. remember this field MUST be a uniqe value, either a random generated string or random number (i would go for the string) and not just a yes or no marker.

Does any of this make sense? :)
New Site Coming Soon! Stay tuned :D
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: "Stacking" items

Post by Torniquet »

Ya know... i really am not sure the best way to go about modified items lol. it is prob best to ignor my last post now i have looked @ it abit lol. If i think of an effective way i will get back to you lol.

as for remembering what is what. you can add comments to fields on tables in php myadmin. or keep them on a notepad doc for ref.
New Site Coming Soon! Stay tuned :D
Post Reply

Return to “Beginner Help and Support”