ID:164003
 
I want to know how I would make the player get a random item like from fishing. The player could randomly get Bass, Brim, Trout, and Salmon. How would I do this?
Here is an example.
obj
Bass
mob
verb
Fish()
var/A = rand(1,4)
if(A==1)
var/obj/Bass/B = new(src)
usr << "[B] is now in your inventory."

if(A==2)
//Etc.
In response to Revojake
Revojake wrote:
Here is an example.

No, don't do that. Make a list of the possible items that the fisherman might catch:

var/list/fishing_results = list("/obj/trout", "/obj/salmon", "/obj/rubberboot", etc...)

mob/verb/Fish()
// yadda yadda, now that we've caught something...
var/obj/fish = new pick(fishing_results) (src)
src << "You caught \a [fish]!"


Now you'll have a new random fish in your contents.

If you want some fish to be less likely than others, you might look up Lummox's pickweight() proc, which is posted on one of his BYONDscape articles, but you can probably find it by searching on the forums.

[Edit]
Look at the bottom of Lummox JR's BYONDscape article on Associative Lists:
http://www.byondscape.com/ascape.dmb/LummoxJR.2002-0103/
In response to Foomer
...That seems abit harder, doesn't it?
In response to Foomer
Ok, now how would I make if the player's fishing level was 1, he would only catch shrimp but if the player's fishing level was 5, he could catch shrimp and sardines?
In response to DadGun
basically all u gotta do is create different lists for different levels. and then in the lists, input all the fish the person of that lvl could catch
In response to Revojake
actually his method is easier because you don't have to type up a lot of if statements. Also when you are using random like that use it like this:
mob
Test()//just a test verb to show you how it should be done
usr.random = rand(1,4)
switch(random)
if(1)//if value of random equals one
//stuff here if the value of random is 1
if(2)
//etc.
In response to Kakashi24142
Kakashi24142 wrote:
       usr.random = rand(1,4)
switch(random)


No need to use a variable with that.
switch(rand(1,4))

Works just fine.

I agree with Foomer's method though.
In response to Zagreus
I agree with Foomer's method, but I'm just letting him know that there's a more efficient method if he's gonna use the other way.
In response to DadGun
Ok I understand now but if the player catches a shrimp he/she gets 10 EXP but if the player catches a sardine he/she gets 15 EXP. How would I do that?
In response to DadGun
List association! My secret weakness. :(

I'll let someone else describe it to ya, hehe.
In response to Zagreus
Might want to look into that. Its actually pretty easy and insanely useful.
In response to Kakashi24142
What makes you think a big switch() is more efficient than pick()ing from a list?
In response to DadGun
Restricting which fish you can catch at which level - how you'd do that depends on whether you wanted to use the pickweight() proc to determine how likely each fish is to be caught.

If you don't use pickweight, then just associate each fish type in the list with a value to determine what skill level you must be to catch it.

If you DO use pickweight, then you'll need to create a second list with all the fish types and their associated skill levels alongside the list of fish types with their associated probability of catching.

I'll assuming that you're not going to use pickweight:

var/list/fishing_results = list("/obj/trout"=2, "/obj/salmon"=5, "/obj/rubberboot"=0, etc...)

// Sort through all the fish types and create a new list of
// all fish that are equal or below the specified skill level.
proc/GetFishForLevel(level)
var/list/L = list()
for(var/fish in fishing_results)
if(fishing_results[fish] <= level)
L += fishing_results[fish]
return L

mob/var/fishing_skill = 0

mob/verb/Fish()
// yadda yadda, now that we've caught something...
var/fish_type = pick(GetFishForLevel(src.fishing_level))
var/obj/fish = new fish_type (src)
src << "You caught \a [fish]!"


You should be able to do something similar to determine how much experience you get for each fish, however I feel the need to express that I think its ridiculous and stupid to determine how much experience the player gets based on a totally random outcome. So you might randomly get 5 experience or you might randomly get 25? That's dumb.
In response to Foomer
Foomer wrote:
You should be able to do something similar to determine how much experience you get for each fish, however I feel the need to express that I think its ridiculous and stupid to determine how much experience the player gets based on a totally random outcome. So you might randomly get 5 experience or you might randomly get 25? That's dumb.

Depends on what kind of fishing spot (Net, Pole, etc.) is what kind of fish you get. So if you fish in a Net area, you would catch small fishlike Shrimp and Crawfish.