ID:144069
 
Code:
area
area1
icon='turf.dmi'
icon_state="blank"
Entered(mob/M)
if(istype(M))
var/pick=rand(1,4)
if(pick==1)
src<<"Monster 1 attacks!"
if(pick==2||3)
src<<"Common monster 2 attacks!"
if(pick==4)
src<<"Monster 3 attacks!"


Problem description: I was just playing around with this to see if I could make it so random monsters come up. The text lines are just there for placeholders. What isn't working is that the text doesn't come up on the map. Because of this, the procs I'm working on won't work as well. What is currently wrong with this? By the way, does area need an icon for it or is it just there? Thanks.

Two problems:

- You're displaying the text to "src". In this context, "src" is the area, and displaying text to an area won't accomplish much. :P You would want to display text to M, in this case

- The code: if(pick==2||3) won't do what you seem. What the compiler will interpret is to run the code indented if the value of pick is 2, or if 3 is a true value. Well, since 3 is always a true value, that block of code will always occur. The correct way to compare pick for both 2 and 3 is to do two separate equality expressions: if(pick==2||pick==3). In your situation, you can easily use a switch statement to compare one value for many. Look up switch in the DM Reference for more information about it

Therefore, your code may look something like this:

area/area1
Entered(mob/M)
if(istype(M)) // or ismob(M)
var/pick = rand(1, 4)
switch(pick) // check what the value of "pick" is
if(1)
M << "Monster 1 attacks!"
if(2 to 3)
M << "Common monster 2 attacks!"
if(4)
M << "Monster 3 attacks!"
In response to Unknown Person
Unknown Person wrote:
Just doing an istype() check with no second parameter is not very useful in your situation. istype() alone will only check if the object is a reference. You have to make sure it's a mob by either comparing to /mob or using ismob() to check if the enterer is a mob

Using is istype() without a second argument makes the second argument default to the declared type, in this case mob.
In response to YMIHere
YMIHere wrote:
Using is istype() without a second argument makes the second argument default to the declared type, in this case mob.

Oh, whoops. I probably mistaken it for something else. Edited.
In response to Unknown Person
Don't feel bad. I saw this post earlier, noticed the OR error, but didn't see why it wouldn't show the text and left stumped. =D
In response to Unknown Person
Thanks a lot, the switch proc is real nice. But the text still doesn't come up as I run through the map.
In response to DarkD3vil666
DarkD3vil666 wrote:
Thanks a lot, the switch proc is real nice. But the text still doesn't come up as I run through the map.

The only thing that I can think of that may cause the problem is that you filled the area object onto the same place your character is being located into.

When you move into an area (not setting loc, but an actual Move()), it calls the area's Entered() proc. This is only the case when you enter it's location, and not when you run around inside the area. Since there is only one instance of an area in a program, Entered() will not be called every time you run around inside of it.

If you want to spawn a monster while running inside an area, you will have to either overwrite turf/Entered() (since that is called every time you enter an individual turf) and check the turf's loc for an area, or overwrite mob/Move() and check if the mob is enclosed in the area. You can do your probability programming inside the function, so it won't spawn a monster every time you take a step.

Here is a simple example snippet. In the snippet, I'm using a type path of /area called /area/battle. You can define more subtypes of /area/battle to let the player face different monsters. Each area has a proc that will be called (similar to how you used Entered() in your previous case) to do monster handling:

area/battle
proc/RandomEncounter(mob/M) // M is the player running around

area1
RandomEncounter(mob/M)
var/pick = rand(1, 4)
switch(pick) // check what the value of "pick" is
if(1)
M << "Monster 1 attacks!"
if(2 to 3)
M << "Common monster 2 attacks!"
if(4)
M << "Monster 3 attacks!"

mob/Move()
. = ..()
if(.) // do random encounter only if you have successfully moved
if(istype(src.loc.loc, /area/battle)) // look at the player's loc (turf)'s loc (area) to see if they are on the battling location
// if so, call its random encounter proc
var/area/battle/A = src.loc.loc
A.RandomEncounter(src)


In this snippet, RandomEncounter() will be called all the time, so it's up to you to work with the probability.
In response to Unknown Person
Thank you so much.
In response to Unknown Person
Note that you should be usually taking more caution with things such as 'src.loc.loc', there, for example, it may not always be valid.