Jump to content

A Different Way To Count Resources Collected Other Than onMessage() - Salmon and Trout Caught Calculator


teddros

Recommended Posts

//If you start this code with fish still in your inventory it will count those fish, so be careful
	
	private int numSalmonCaught;//Total number of salmon caught. Initialize at 0.
    private int numTroutCaught;//Total number of trout caught. Initialize at 0.
	
    private int inventorySlotIndex;//This is the inventory slot number that we are currently examaning
    								//The slot numbers from inventory are from 0-27 NOT 1-28!!
    								//Meaning that if you are fishing for trout initalize the variable at 2 
    								//Because the first two inventory slots are taken with the rod and feathers.
    
    
    //I implemented this by either running the method countLureCatches() in the paint, or creating a thread and run it.
    //place code inventorySlotIndex = 2 somewhere in your code after you bank or drop the fish. 
    //This way the counter can start again
    
    public void countLureCatches(){
    	if((inventory.getItemInSlot(inventorySlotIndex) != null) || (inventorySlotIndex <= 28)){//Will only run if the 
    		//slot that we are currently at has something stored in it, or that our index value is less than 28 
    		//meaning we don't have a full inventory
    		
    		if(inventory.getItemInSlot(inventorySlotIndex).getName().equals("Raw salmon")){//Checks to see what item is in 
    			//said inventory slot and checks to see if its salmon.
    			//because .getItemInSlot(int) returns an Item object we must use .getName() to check it against a string.
    			//We must use .equals() to compare strings because they are objects.
    			numSalmonCaught++;//Increments our salmon
    			inventorySlotIndex++;//Increments our inventory slot variable
    	
    		}else{//Only goes through this code if the if statement above is found to be false.
    			  		
    			if(inventory.getItemInSlot(inventorySlotIndex).getName().equals("Raw trout")){
    				numTroutCaught++;
    				inventorySlotIndex++;
    			}
    		}
    	
    	}
    }
	

So I was writing my own fishing script for Barbarian Village and couldn't help but be bothered by the lack of a way to calculate how many trout I caught and how many salmon I caught. Sure I could use onMessage() and have a total fish caught but I wanted a way to have a count that was distinct for both types of fish.

 

So allow me to explain what my code does.

First we initialize the variable inventorySlotIndex = 2 in the onStart(). This is because we have something in slot 0 and something in slot one (feathers and fishing rod). Keep in mind the slot numbers are from 0-27 NOT 1-28.We then find out what is in the slot number (trout or salmon) once we detect that something is there. Once we find out what is in that slot say we caught a salmon we then increment both the salmonCaught and the inventorySlotIndex, and do the same thing for the next slot.

Once the inventory is full the inventorySlotIndex should be at 28 which is out of bounds for our inventory and cause the script to stop counting. Once we bank or drop the fish it is important that you have the inventorySlotIndex reset to 2.

 

If you are fishing lobster then obviously you start at slot 1. If you are doing something where you have no slots taken with equipment then you'd set it to 0.

 

Any questions suggestions or criticisms are welcomed.

If you need help implementing this into your code I'm also willing to help.

Edited by teddros
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...