PDA

View Full Version : Item Function Is Invalid


Hwona
07-22-2013, 11:07 PM
Hello, I was thinking about trying to make a new item function, but whenever I try to use an item, it says the item function is invalid. Can someone help or point me in the right direction? :3

Yes, I created a new row in adopts_items_functions.

Here's the code:
function items_genderf($item, $adopt){
$mysidia = Registery:: get("mysidia");
//Let's check if the adoptable is already female.
$gender = $mysidia -> db -> select ("gender") -> fetchObject();
if($gender = "f") {
//The adoptable is already female
$note = "Your adoptable is already female.";
}
else{
//The adoptable is male. It's gender can be switched to female.
switch($adopt->gender){
case "f":
$mysidia -> db -> update("owned_adoptables", array("gender" => 'f'),"aid='{$adopt->aid}' and owner ='{$item->owner}'");
$note = "Your adoptable {$adopt->name} is now female.";
}
//Update item quantity...
$delitem = $item->remove();
}
return $note;
}

It's suppossed to plug into the existing functions_items script, so I didn't include the php tags.

Hall of Famer
07-23-2013, 02:46 PM
Are you sure the function name matches the naming convention? Take a screenshot from PHPMyadmin and I will see how to help you.

Hwona
07-23-2013, 02:49 PM
Thanks - taking pic... I thought it would be more like an issue with the code.

Edit here it is :3:

http://i1290.photobucket.com/albums/b521/Wallie987/Helpme_zps2b12a65c.jpg (http://s1290.photobucket.com/user/Wallie987/media/Helpme_zps2b12a65c.jpg.html)

Hall of Famer
07-23-2013, 02:55 PM
Well there is indeed an error in your code. This line clearly is not doing the right thing, you aint using the database class properly.


$gender = $mysidia -> db -> select ("gender") -> fetchObject();

Hwona
07-23-2013, 02:58 PM
Ah thanks! Now if only I knew exactly how to fix it...

From the looks of it, I think I might have to add something like: ... ownedadopt ->... and so on..
But then, another half of me thinks it's suppossed to be something like getgender. :L

Do you mind telling me what I need to tweak.

$gender = $mysidia -> db -> ownedadoptable -> select ("gender") -> fetchObject();

Any closer to getting it right?

Hall of Famer
07-23-2013, 11:40 PM
Well its still incorrect, the syntax should be:


$gender = $mysidia -> db -> select ("owned_adoptables", array("gender"), "aid='{$adopt->aid}' and owner ='{$item->owner}'") -> fetchColumn();


Use fetchColumn() if you only want the gender info, but if you wish to pull a collection of information such as type, name, owner and other things out, you can use fetchObject().

Hwona
07-24-2013, 10:13 AM
:D thank you so much! So you have to put all that aid stuff there? :3

O.o but it still says the function is invalid. Is there any other problem with the code?

Hall of Famer
07-30-2013, 02:55 PM
Yeah there is still one more trick you need to apply at least with Mys v1.3.3. Go to the script file class_privateitem.php, find the long switch statement that defines each item function, add yours in the list, and it should be working. In Mys v1.3.4 that will be released in about a week or two, I will make it possible to get the item function name dynamically so you wont have to go through this pain.

Hwona
07-30-2013, 05:48 PM
:D Thanks!
- Oh, now it says that the registery doesn't exist. O.o

Hall of Famer
08-02-2013, 02:46 PM
Clearly you have misspelled the word registry.

Hwona
08-02-2013, 03:17 PM
x.x Poop... I'm suppossed to know how to spell registry... that's embarassing.

Edit: ok, it's working-ish. Unfortunately, it keeps saying my adopt is already female when it's male. O.o What did I do this time...

Hwona
08-05-2013, 12:14 PM
Yeah, I think I messed up again - it keeps displaying the "Your adopt is already female" message regardless of gender....

IntoRain
08-05-2013, 03:21 PM
Do an output of the $gender variable to see what it's in there (if you do echo $gender it will appear at the top left of the screen, or make your variable $note equal to $gender). If $gender actually is "f" then the mistake is at the expression, if $gender is not "f" you know you are fetching gender in a wrong way. Sorry, I don't really know the syntax, but this is a simple way of searching where the mistake is

Hwona
08-06-2013, 11:14 PM
Um, the gender might be "female" instead of "f"

Ok, I've tried swapping "f" with "female", but it's still not working... maybe I should try flipping the if and else?

Hall of Famer
08-18-2013, 02:33 AM
Yeah give it a shot, it never hurt to try. XD Anyway if you are talking about an issue with your own code it can be confusing to other people who do not get a complete picture of what you are trying to accomplish.

Hwona
08-18-2013, 11:18 AM
*smacks forehead*
Now I feel dumb - I haven't even told anyone what I'm trying to do...
technically, I'm trying to make an item class that will change your adopt's gender to female. :3

Hall of Famer
08-18-2013, 02:22 PM
I see, in this case the gender is indeed 'f' instead of 'female', but it may not be the problem. Make sure you have an adoptable object that is actually in the item function you create. Use var_dump() on $adopt to see whether it does exist, if it exists the problem may be with your update query.

Hwona
08-18-2013, 06:48 PM
So I just stick it in the functions_items code? :3
Note: I basically did the same thing as all the other function codes... but I can't figure out what's wrong...

Hall of Famer
08-19-2013, 01:13 AM
I see, mind posting the actual source code of your item function here?

Hwona
08-19-2013, 07:41 AM
O.o source code? Uh oh... I made a big mess up - again.
*searches through files in hopes of finding an example of a source code*

Hwona
08-20-2013, 10:21 PM
Uh oh, I'm a bummer - can someone show me an example or the location of the source codes? :(

IntoRain
08-20-2013, 10:50 PM
Hall Of Famer means the function you made, the whole code from the file

Btw you have the if still liek this?

if($gender = 'f')

It should be if($gender == 'f')

= means you are giving the variable a value. == means you are comparing it (it's a function that says if it is false or true)

Btw if you already have the switch-case you can take the if off. Like

switch(...)
{

case 'f':
//it's female
break;
case 'm':
// it's male
break;
default:
//error
break;
}

Also if the function receives an adopt, to get the gender of it you don't need to use select, you even use $adopt->gender on the switch. That is confusing me a lot xD To verify if the owner of the item and the owner of the adopt are the same you can use an if($adopt->owner == $item->owner), right?

Hwona
08-21-2013, 07:57 AM
Oh, it has to be 2 equal signs - I'll try that! Thanks for the tip. :3

Edit: Thanks, you fixed half of my problem! The item says "action completed" and gets deleted, but my male adoptable is still male, and the "gender change successful messge" does not pop up. :L

Also, here's the code:
<?php

// File ID: functions_items.php
// Purpose: Provides specific functions defined for items

function items_valuable($item, $adopt){
$note = "The item {$item->itemname} is a valuable item, which cannot be used on any adoptable but may sell a good deal of money.";
return $note;
}

function items_level1($item, $adopt){
$mysidia = Registry::get("mysidia");
$newlevel = $adopt->currentlevel + $item->value;
$lev = $mysidia->db->select("levels", array(), "adoptiename='{$adopt->type}' and thisislevel ='{$newlevel}'")->fetchObject();

//Check if the adoptable's level is already at maximum.
if(!is_object($lev)){
// object not created, the level is already at maximum.
$note = "Unfortunately, your selected adoptable's level cannot be raised by using item {$item->itemname}.";
}
else{
//Update item quantity...
$delitem = $item->remove();
//Execute the script to update adoptable's level and clicks.
$mysidia->db->update("owned_adoptables", array("currentlevel" => $newlevel, "totalclicks" => $lev->requiredclicks), "aid ='{$adopt->aid}' and owner='{$item->owner}'");
$note = "Congratulations, the item {$item->itemname} raised your adoptable's level by {$item->value}";
}
return $note;
}

function items_level2($item, $adopt){
$mysidia = Registry::get("mysidia");
$newlevel = $item->value;
$lev = $mysidia->db->select("levels", array(), "adoptiename='{$adopt->type}' and thisislevel ='{$newlevel}'")->fetchObject();

//Check if the adoptable's level is already at maximum.
if(!is_object($lev)){
// object not created, the level is already at maximum.
$note = "Unfortunately, your selected adoptable's level cannot be raised by using item {$item->itemname}.";
}
else{
//Update item quantity...
$delitem = $item->remove();
//Execute the script to update adoptable's level and clicks.
$mysidia->db->update("owned_adoptables", array("currentlevel" => $newlevel, "totalclicks" => $lev->requiredclicks), "aid ='{$adopt->aid}' and owner='{$item->owner}'");
$note = "Congratulations, the item {$item->itemname} increases your adoptable's level to {$item->value}";
}
return $note;
}

function items_level3($item, $adopt){
$mysidia = Registry::get("mysidia");
//Update item quantity...
$delitem = $item->remove();
//Execute the script to update adoptable's level and clicks.
$mysidia->db->update("owned_adoptables", array("currentlevel" => 0, "totalclicks" => 0), "aid ='{$adopt->aid}' and owner='{$item->owner}'");
$note = "Congratulations, the item {$item->itemname} has reset the level and clicks of your adoptable.";
return $note;
}

function items_click1($item, $adopt){
$mysidia = Registry::get("mysidia");
$newclicks = $adopt->totalclicks + $item->value;
$mysidia->db->update("owned_adoptables", array("totalclicks" => $newclicks), "aid='{$adopt->aid}'and owner='{$item->owner}'");
$note = "By using {$item->itemname}, the adoptable's total number of clicks has raised by {$item->value}<br>";
//Now lets check if the adoptable has reached a new level.

$ownedAdopt = new OwnedAdoptable($adopt->aid);
if($ownedAdopt->hasNextLevel()){
//new level exists, time to check if the total clicks have reached required minimum clicks for next level.
$nextLevel = $ownedAdopt->getNextLevel();
$requiredClicks = $nextLevel->getRequiredClicks();
if($newclicks >= $requiredClicks and $requiredClicks != 0 and $requiredClicks != ""){
// We need to level this adoptable up...
$mysidia->db->update("owned_adoptables", array("currentlevel" => $nextLevel->getLevel()), "aid ='{$adopt->aid}' and owner='{$item->owner}'");
$note .= "And moreover, it has gained a new level!";
}
}
//Update item quantity...
$delitem = $item->remove();
return $note;
}

function items_click2($item, $adopt){
$mysidia = Registry::get("mysidia");
$newclicks = $item->value;
$mysidia->db->update("owned_adoptables", array("totalclicks" => $newclicks), "aid='{$adopt->aid}'and owner='{$item->owner}'");
$note = "By using {$item->itemname}, the adoptable's total number of clicks has raised by {$item->value}<br>";
//Now lets check if the adoptable has reached a new level.

$ownedAdopt = new OwnedAdoptable($adopt->aid);
if($ownedAdopt->hasNextLevel()){
//new level exists, time to check if the total clicks have reached required minimum clicks for next level.
$nextLevel = $ownedAdopt->getNextLevel();
$requiredClicks = $nextLevel->getRequiredClicks();
if($newclicks >= $requiredClicks and $requiredClicks != 0 and $requiredClicks != ""){
// We need to level this adoptable up...
$mysidia->db->update("owned_adoptables", array("currentlevel" => $nextlevel), "aid ='{$adopt->aid}' and owner='{$item->owner}'");
$note .= "And moreover, it has gained a new level!";
}
}

//Update item quantity...
$delitem = $item->remove();
return $note;
}

function items_click3($item, $adopt){
$mysidia = Registry::get("mysidia");
$date = date('Y-m-d');
$mysidia->db->delete("vote_voters", "adoptableid = '{$adopt->aid}' and date='{$date}'");
//Update item quantity...
$delitem = $item->remove();
$note = "By using item {$item->name}, you have make your adoptables eligible for clicking by everyone again!";
return $note;
}

function items_breed1($item, $adopt){
$mysidia = Registry::get("mysidia");
// Update the lastbred info.
$mysidia->db->update("owned_adoptables", array("lastbred" => 0), "aid ='{$adopt->aid}' and owner='{$item->owner}'");
$note = "The item has been successfully used on your adoptable, it can breed again!<br>";
//Update item quantity...
$delitem = $item->remove(1, $item->owner);
return $note;
}

function items_breed2($item, $adopt){
$mysidia = Registry::get("mysidia");
// Note this function exists but is not useful until Mys v1.3.2, when adoptables can carry/attach items.
$mysidia->db->update("owned_adoptables", array("lastbred" => 0), "aid ='{$adopt->aid}' and owner='{$item->owner}'");
$note = "The item has been successfully used on your adoptable, it can breed again!<br>";
//Update item quantity...
$delitem = $item->remove();
return $note;
}

function items_alts1($item, $adopt){
$mysidia = Registry::get("mysidia");
// First lets check if alternative image exists for an adoptable at this level.
$lev = $mysidia->db->select("levels", array(), "adoptiename='{$adopt->type}' and thisislevel ='{$adopt->currentlevel}'")->fetchObject();
if($lev->alternateimage == ""){
// The alternate image does not exist, cannot convert adoptable into its alternate form
$note = "It appears that your adoptable does not have an alternate image at its given level...<br>";
}
else{
// The alternate image exists, conversion between primary and alternate image is possible.
switch($adopt->usealternates){
case "yes":
$mysidia->db->update("owned_adoptables", array("usealternates" => 'no'), "aid ='{$adopt->aid}' and owner='{$item->owner}'");
$note = "Your adoptable has assume the species primary form.";
break;
default:
$mysidia->db->update("owned_adoptables", array("usealternates" => 'yes'), "aid ='{$adopt->aid}' and owner='{$item->owner}'");
$note = "Your adoptable {$adopt->name} has assume the species alternate form.";
}
//Update item quantity...
$delitem = $item->remove();
}
return $note;
}

function items_alts2($item, $adopt){
$note = "This feature will be available soon after we redesign the adoptable class, enjoy!";
return $note;
}

function items_name1($item, $adopt){
$note = "umm just realized that people can change adoptables names freely, will have to think about it later.";
return $note;
}

function items_name2($item, $adopt){
$note = "For now the items can only be used on adoptables, so user-based item usage will be implemented later.";
return $note;
}
function items_genderf($item, $adopt){
$mysidia = Registry:: get("mysidia");
//Let's check if the adoptable is already female.
$gender = $mysidia -> db -> select ("owned_adoptables", array("gender"), "aid='{$adopt->aid}' and owner ='{$item->owner}'") -> fetchColumn();
if($gender == "f") {
//The adoptable is already female
$note = "Your adoptable is already female.";
}
else{
//The adoptable is male. It's gender can be switched to female.
switch($adopt->gender){
case "f":
$mysidia -> db -> update("owned_adoptables", array("gender" => 'f'),"aid='{$adopt->aid}' and owner ='{$item->owner}'");
$note = "Your adoptable {$adopt->name} is now female.";
}
//Update item quantity...
$delitem = $item->remove();
}
return $note;
}
?>

IntoRain
08-21-2013, 11:03 AM
I simulated what you wanted to do and it's working, I used only if's instead switch-cases (not to mix if's and switch-cases since they both do the same):

function items_genderf($item, $adopt){
$mysidia = Registry::get("mysidia");

//Let's check if the adoptable is already female.
if($adopt->gender == 'f')
{ //The adoptable is already female
$note = "Your adoptable is already female.";
}
else{
//The adoptable is male
$mysidia->db->update("owned_adoptables", array("gender" => 'f'),"aid='{$adopt->aid}' and owner ='{$item->owner}'");
$delitem = $item->remove();
$note = "Your adoptable {$adopt->name} is now female.";
}

return $note;
}

Just switch:


$mysidia = Registry:: get("mysidia");
//Let's check if the adoptable is already female.

switch($adopt->gender)
{
case "f":
$note = "Your adoptable is already female.";
break;
case "m":
$mysidia->db->update("owned_adoptables", array("gender" => 'f'),"aid='{$adopt->aid}' and owner='{$item->owner}'");
$note = "Your adoptable is now female.";
$delitem = $item->remove();
break;
default:
$note = "It appears your adoptable doesn't have a gender.";

}
return $note;

You can also use your own code, you one error only. In the switch-case, it should be case 'm'. Case 'f' means "if it is female, change to female.", so case 'm' means "If it is male, change to female.".

Also, the item deletes anyway at the end because it's not inside the the case. You have to delete the item if it succeeds, so it should be inside the case.

Also each case must end with break; so it knows when to stop.

Since your adopt was male, it was reaching the switch-case (the else). But it would only activate if it was female, so it ignored it and proceeded to item deletion so that's why it was decreasing and not changing the gender. To avoid this kind of confusion, you only delete the item if the switch-case is sucessful.

With your own code:

$mysidia = Registry:: get("mysidia");
//Let's check if the adoptable is already female.
$gender = $mysidia -> db -> select ("owned_adoptables", array("gender"), "aid='{$adopt->aid}' and owner ='{$item->owner}'") -> fetchColumn();
if($gender == "f") {
//The adoptable is already female
$note = "Your adoptable is already female.";
}
else{
//The adoptable is male. It's gender can be switched to female.
switch($adopt->gender){
case "m":
$mysidia -> db -> update("owned_adoptables", array("gender" => 'f'),"aid='{$adopt->aid}' and owner ='{$item->owner}'");
$note = "Your adoptable {$adopt->name} is now female.";
//Update item quantity...
$delitem = $item->remove();
break;
}
}
return $note;

One thing, you really don't need to get the gender from a select, it's ok to just do $adopt->gender

Hall of Famer
08-21-2013, 12:49 PM
I guess my post was a bit misleading, I was merely referring to the source code of the very item function you created, not the source code of the entire functions_items.php file. For this case it is not a problem, but at some circumstances it may be time consuming to read through an entire script file. Id say just post the code that aint working is sufficient when bringing up a problem.

Hwona
08-21-2013, 04:35 PM
Ah. I see! xP
Well, in that case, here's the code! :D

function items_genderf($item, $adopt){
$mysidia = Registry:: get("mysidia");
//Let's check if the adoptable is already female.
$gender = $mysidia -> db -> select ("owned_adoptables", array("gender"), "aid='{$adopt->aid}' and owner ='{$item->owner}'") -> fetchColumn();
if($gender == "f") {
//The adoptable is already female
$note = "Your adoptable is already female.";
}
else{
//The adoptable is male. It's gender can be switched to female.
switch($adopt->gender){
case "f":
$mysidia -> db -> update("owned_adoptables", array("gender" => 'f'),"aid='{$adopt->aid}' and owner ='{$item->owner}'");
$note = "Your adoptable {$adopt->name} is now female.";
}
//Update item quantity...
$delitem = $item->remove();
}
return $note;
}

Edit: O.o Thank you guys sooooooo much! - It works! :D

Here's the finished code :D:
function items_genderf($item, $adopt){
$mysidia = Registry:: get("mysidia");
//Let's check if the adoptable is already female.
$gender = $mysidia -> db -> select ("owned_adoptables", array("gender"), "aid='{$adopt->aid}' and owner ='{$item->owner}'") -> fetchColumn();
if($gender == "f") {
//The adoptable is already female
$note = "Your adoptable is already female.";
}
else{
//The adoptable is male. It's gender can be switched to female.
switch($adopt->gender){
case "m":
$mysidia -> db -> update("owned_adoptables", array("gender" => 'f'),"aid='{$adopt->aid}' and owner ='{$item->owner}'");
$note = "Your adoptable {$adopt->name} is now female.";
}
//Update item quantity...
$delitem = $item->remove();
}
return $note;
}

IntoRain
08-21-2013, 04:40 PM
I already explained why it wasn't working, see the post above Hall Of Famer's. Your switch case is wrong

case "f":
....

^ The switch-case only does the code (update thingy) you have after it if your adopt's gender is female. So it will only change the gender from female to female. You need to change it to case "m".

Hwona
08-21-2013, 04:51 PM
Yeah, I edited my last post. Anyways, thank you so much! I probably would have kept changing the code and fixing nothing if you didn't point that out. >^.^<

IntoRain
08-21-2013, 05:04 PM
Yeah, I edited my last post. Anyways, thank you so much! I probably would have kept changing the code and fixing nothing if you didn't point that out. >^.^<

Yay that's great! :D

ilrak
09-26-2014, 03:31 PM
Sorry to bump up this thread, but I was seeing if I could try out this mod (and then do one similar for color/marking changing) and have been running into the same item function invalid error. I'm using 1.3.4, so I'm not sure if maybe that's the issue or not or if I've run into an error with my itemfunctions table in general (I assume that the intent is for adoptable, but I may have done it wrong) . ^v^;

IntoRain
09-27-2014, 10:50 AM
Sorry to bump up this thread, but I was seeing if I could try out this mod (and then do one similar for color/marking changing) and have been running into the same item function invalid error. I'm using 1.3.4, so I'm not sure if maybe that's the issue or not or if I've run into an error with my itemfunctions table in general (I assume that the intent is for adoptable, but I may have done it wrong) . ^v^;

What error is it?
In the item_functions table, the function parameter is the name of your item function (in this example, it would be genderf) and in this case the intent is for Adoptable yes.
After that, you will have to go to the classes folder and edit the file class_privateitem.php. In the apply() function, inside the big switch-case, you have to put another 'case' to add the new item type

ilrak
09-27-2014, 09:27 PM
Yay! Thank you so much! It works wonderfully! Now I'll tinker with it for some breed and color change mods (maybe marking change too)