Active pet widget?

Forum
Last Post
Threads / Messages

Ittermat

The awesomesauce
Member
Joined
Feb 2, 2016
Messages
307
Points
18
Location
in front of my laptop
Mysidian Dollar
21,318
EDIT: heres the post with the working code-


How would I make a thing to display on my side bar that shows your active/spotlight pet? And maybe make them say random things?

So for a picture example- and we'll say this pets name is...Marian?

-----------------------------
c130f7955dbfe6ac72e874b9045b182f.png


Marian says: "Lets go exploring!"
_____________________________

But that there would be a list of different things you can make them say? So it would choose from a random assortment of Phrases Or sometimes nothing at all? Possibly depending on the pet species if possible?

So one species would have a different List of things they could say.
But if thats too hard its fine XD Same list is okay.

I was just wondering if this was possible. and if so how it would be done.
 
Last edited:
To make a favpet display inthe sidebar, first in the AdminCP you'll create a module (surprisingly not a widget):
sc_by_kyttias-d8dr0j8.png


Then in classes/class_sidebar.php you'll need to add these functions:
PHP:
public function getFavPetSB(){
		return $this->FavPetSB;
    }

protected function setFavPetSB(){
		$mysidia = Registry::get("mysidia");

		$userfavpet = $mysidia->db->select("users_profile", array("favpet"), "username = '{$mysidia->user->username}'")->fetchColumn(); 
		if ($this->userfavpet == "0"){
			$this->FavPetSB = new Paragraph; 
			$this->FavPetSB->add(new Comment("<b>No Favorite Pet Set</b>"));
		}
		else{
			$adopt = new OwnedAdoptable($userfavpet);
			$this->FavPetSB = new Paragraph; 
			$this->FavPetSB->add(new Comment("<b>Favorite Pet!</b> <br/>
				<a href='/myadopts/manage/{$userfavpet}'><img src='{$adopt->getImage()}'></a>
				"));
		}

		$this->setDivision($this->FavPetSB);
	}

Using "$adopt = new OwnedAdoptable($userfavpet);" you can also pull up data like {$adopt->getName()} and {$adopt->getCurrentLevel()} - these are functions found in classes/class_ownedadoptable.php, but you should also be able to use {$adopt->type} (which is not a function) to pull raw data from the database from the 'type' column - this holds the species.

But I'll get back to that. Let me know if you can at least get the pet into the sidebar and then I'll be back later help you set up a switch statement to display random phrases for pets to say. :meow:
 
Ittermat do you mind if I use this too? I was going to screw around with the code to try to make a active pet too.
 
That can be done, too. :pleased: Sounds quite fun, really! (I had work today so I'm super tired. I should be be able to get to this tomorrow, though.)
 
We're going to set it up like this: 1% of the time the pet may get a status effect. Of the 99% of the time that the pet does not come away with a status effect, 75% of the remaining time - nothing will happen at all. Of the other 25% of the time, 5% of the time it will either be because it found some money or an item (about 50% toward either). The remaining 20% of the time it will say a random phrase based on its species. Below is commented but empty code just to get the odds of each event happening right:
PHP:
$status_chance = mt_rand(1, 100);  
if ($status_chance <= 1){ # One percent chance the pet will obtain a status effect.
	/* We would apply a status effect here...*/
} else { # Ninety-nine percent chance the pet will not get a status effect. Will something else happen?
	$something_chance = mt_rand(1, 100); 
	if ($something_chance <= 25){ # Twenty-five percent chance something will happen.
		$gift_chance = mt_rand(1, 100); 
		if ($gift_chance <= 5){ # Five percent chance the user will receive a gift from their pet.
			$item_chance = mt_rand(1, 100);
			if ($item_chance <= 50){ # Fifty percent chance the gift from their pet will be an item.
				/* The pet has found an item for you! */ 
			} else { # Fifty percent chance the gift from their pet will be money.
				/* The pet has found some money for you! */
			}
		} else { # Twenty percent chance the pet will talk but have no gift.
			/* No gift will be given but a neat species-specific phrase can still be said! */
		}
	} else { # Seventy-five percent chance nothing will happen whatsoever.
		/* Nothing will happen with the pet at all. */
	}
}

Basically, just a tower of if-else statements so far, using PHP's built in mt_rand() function - we're feeding it the numbers 1 and 100 so it'll choose a random number between the two, just like rolling (if possible) a 100-sided dice. It's the easiest way to fudge in percents.

More soon! I've got to go look up some of the code I've used to give items and such again.

1) Is it alright if items and money are automatically collected, even without the user clicking or doing anything? It becomes much more complicated otherwise.

2) Inevitably if you wanted to, say, make it so your active pet can find an abandoned egg 1/500th of the time (about 0.2% chance of happening) but require that the user also be paying attention and clicking that message, remind me - I should be able to think this one through, given enough time. Let me know if this is something you want.

3) These percents should all be easy to modify. Let me know if you have questions and I can help tweak the code. If you change an mt_rand(1,100) to mt_rand(1,200) you will literally make something twice as hard to get and this will be relevant as soon as you want things to happen less than 1% of the time. One percent of that (1,200) would be 0.5% instead of (1,100)'s obvious 1%.

4) The fake status... placeholders for potentially real ones later? Is there a list of random events (nice ones are better than mean ones) you'd be interested in? Such as the pet suddenly gaining experience (as if clicked) or an entire level (if it's not frozen)? More complicated things (that existing item functions can't do) I might be interested in helping out with later, once everything else is done. For now, status effects will be the last on the list of things I'll do, and we'll fill it in with a message of "a status effect would have happened here" so long as we're testing. There are also some ideas I have that would be optional status effects that would require the user to click to accept and have it explained that the changes are somewhat permanent but entirely optional - such as the pet's gender or species changing, or the species' alternate appearance activated. Most of the time the user would be enthusiastically agreeing to this change but what if they really like their pet exactly how it is? We wouldn't want to force something, no matter how cool we think it is.
 
Last edited:
Yes automatically getting the items and money is fine! Also no need to worry about 2 right now..maybe later? And 3 ill mess with lol. As for status effects is it possible to give me bases for what different things can be done? Id like the effects to do things to them, and if possible have the pet say something like if they gain am item or lose money or something? But im unsure what i want the status effects to say lol..ill think more on it and use what you gave me when i get home from work ^_^

Also thank you again you are amazing!

_______

EDIT EDIT:

Heres some ideas for status effects.. if you give me the base coding to use I can modify from there for these. (like have it set the effects- but allow me to change what it says and add more of them to it you know?)

Status effects:


-Looks like ( Petname) found a some food! They've gained some exp! (Gains Clicks)

- (petname) Ate some garbage! Gross!

-Oh no! (Petname) has been cursed! (Clicks reset for day)

-They grow up so fast dont they? Lets fix that! (petname) is now a baby again! (reverts to baby stage)

-(petname) Won a fight! They gained a level! (gains one level)

-The love fairy came by and waved her wand! (petname) can now breed again! (enables breeding again)

-A mad scientist is loose! (Petname) no longer has species barriers when being bred! (Enables cross species breeding)

-----



Edit: Where do I put this code in at btw?
 
Last edited:
Well when debugging your site, I find that it has a very serious glitch resulted from this Active Pet Module. For a newly registered user, they do not own any adoptable yet, and therefore do not have favorite pet. However, the script assumes that this new user has favorite pet, and try to fetch the adoptable ID 0 instead(favorite pet ID defaults to 0 if it does not exist). This leads to an uncaught exception 'adoptable ID does not exist', and will prevent any newly registered users from browsing your site.

To fix this, you need to take care of the scenario in which the user has no favorite pet(ID = 0). In this case, you will either just not fetch adoptable at all, or catch exception using a try...catch block. The choice is up to you.


Edit: Strangely the solution Kyttias offered seems to have taken care of nonexistent favorite pet ID already, but when I dont have a favorite pet I still receive errors. Did you copy/paste her code exactly? Or something else is wrong?
 
The code (with all the percent discussion) isn't done yet/doesn't do anything yet. It'll go inside the setFavPetSB() we made, inside the else half of the if statement, all this inside class_sidebar.php.

:happyc: Sorry, I've had along day and don't think I can work on this tonight - good news is that I don't have work again until at least Sunday so I'll have a few days to think on all of this.

edit - @HoF, I vaguely remember this issue but I'm not sure how it was resolved. =/
 
Last edited:
no its fine kyttias! I know you have a life... Im sorry to be so bothersome..Im seriously bad at this coding thing...I am trying to learn though honest...its just not clicking or sticking..
 
Did we ever figure out how to fix this?

I even made an adoptable to put in its place-when one has no favorite pet

1zc0c50.jpg


The Adoptable id (in adopt_Adoptables table) is 30

if that helps any?
 
Last edited:
Actually the above won't work because if the user doesn't own pet 30 it might cause errors.

Send me the code we put in classes/class_sidebar.php - I'd like to make some changes to it.

PHP:
protected function setFavPetSB(){
    $mysidia = Registry::get("mysidia");
    $profile = $mysidia->user->getprofile();
    
    if ($this->userfavpet == "0"){
        $this->FavPetSB = new Paragraph; 
        $this->FavPetSB->add(new Comment("<b>No Favorite Pet Set</b>"));
    }

    if ($profile->getFavpetID() != "0"){
        $favpet = new OwnedAdoptable($profile->getFavpetID());
        $this->FavPetSB = new Paragraph; 
        $this->FavPetSB->add(new Comment("<b>Favorite Pet!</b> <br/>
            <a href='/myadopts/manage/{$favpet}'><img src='{$favpet->getImage()}'></a>
            "));
    }

    $this->setDivision($this->FavPetSB);
}

I'm changing where we get information from, basically. And then we're more closely checking only if its zero or not zero. If it's empty, at least it won't error.

If you're afraid of editting the code, I can do it.
 
If you haven't done anything to it then you should be able to just replace it.
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,277
Messages
33,122
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Latest Posts

Top