ID:1379632
 
(See the best response by SuperSaiyanGokuX.)
Code:
mob
Player
icon='Player.dmi'
verb
Build_in_View()
set category = "Actions"
new/turf/Grass2(view)


I'd like to know what I'm doing wrong. I've tried oview also.

mob/verb

Build_In_View()

for(var/turf/t in view())

new/turf/Grass2(t.loc)


EDIT: oview() doesn't include the center object, so if you replaced view() with oview() every turf in your view except the turf you're standing on will be changed.

the default params for view() are view(distance=world.view, center=usr), but if this was being used for a limited area then you would change the distance to your wanted number.
Flys can you explain the for loop please as I do not see an icreamentor or a decreamentor, also the difference between:


For (var/turf/t in view ())


And


For (turf/t in view ())


Thank you.
In response to Akando5959
Please bare with me, because I am a terrible explainer.

There are multiple ways to use for(), if I remember correctly there are 3 ways to use for(), but I can only remember 2.
for(var/i in 1 to 5)
for(var/i=10, i > 0, i--)


Anyways, the for() loops for all the turfs in the users view, and then places a new Grass2 at t's location.

---------

Sorry I cannot explain it better. I'm better at giving examples than I am telling someone.
The "for list loop" (check the Reference) loops through every item in a list. If you include a type, it loops through only objects of that type. You can make a variable inside it that can only be used inside the loop, just like the other forms.
Lol that is understandable.

Ok that all makes sense but my main thing is the:

Var/turf/t

Would that not be same as:

Var
Turf
T

</dm

Or does it read more of var/(whatever t is refrencing)
In response to Akando5959
Have you never used typed variables before? Yes, slashes can be replaced with new lines, but in a for() loop you might as well keep the variable declaration all in one line.
I've used

Mob
Attack (mob/t in oview ())

but never with var in the front, what is the difference?
In response to Akando5959
I can tell (or I'm hoping) you're writing from a phone because of your unnecessary capitalizations.

That doesn't mean you can't use the web version of the Reference, though.
I am on phone, I can see the refrence.

this is called typed variables?
Best response
Wks60 wrote:
Code:
mob
> Player
> icon='Player.dmi'
> verb
> Build_in_View()
> set category = "Actions"
> new/turf/Grass2(view)
>

I'd like to know what I'm doing wrong. I've tried oview also.

Flysbad's code should fix the issue, but let me explain what went wrong with how you tried to do it:

The argument in new() (as in "new/turf/Grass2([THIS RIGHT HERE])") must be a location/position (including an obj/mob if you want to stick the item directly into their contents)

"view" and "oview" are not locations or objects. They are lists (well, technically, they are procedures that produce lists), so you cannot use them directly as positions for the new turfs you're creating.

I know that it seems like you should be able to just say "create this new turf over the entire view", but that's not quite how it works.

What Flysbad's code does, it use a for() loop to grab every turf that is in your view, and use each of those turfs as the location/position for one of your new turfs. It'll still convert every tile in view() to the new turf, but you have to do it this way; tile-by-tile, to get each specific location/tile. Let me break it down (Akando, pay attention here as well, as this part is mostly for your benefit!):

for(var/turf/t in view())
new/turf/Grass2(t.loc)


So what's this doing? Well, let me explain it element-by-element.

1. First up, the "for()" loop itself. As the name implies, "for()" is a looping function. In english, it means this: "For every item inside the parentheses, do what's indented under this line."

So what goes into the parentheses of "for()" is going to be a list (or treated as one). Even if it is just one item, it's a list of one item...lol

The other uses of "for()" that you've seen actually break down to this principle as well.

"for(var/i in 1 to 5)
for(var/i=10, i > 0, i--)"

Both of these are ways of creating a list of numbers. The first will generate "1,2,3,4,5", the second will generate "10,9,8,7,6,5,4,3,2,1". But both of them create a list. "for()" in these cases, then just runs through that series of numbers, and performs its embedded function for each number in the list.

2. So, what's going on inside this "for()" loop? Well, you're looping through a list of all turfs in view(), and sticking them one-by-one (temporarily) into a new local var, "t" for use inside of the loop's embedded functions. (once the embedded function is carried out with one turf in the list, the next turf is stuck into "t", and it repeats, and so on until you've used all of the turfs in the list.

But why write it as "var/turf/t"?

First, the "var/" is necessary, because you are declaring/creating this variable right here. It is not a "permanent" variable, like one you would define in your code under "mob" or wherever. It is only a local variable, created for use inside of a proc, and it will cease to exist once the proc is complete. So, the "var/" is necessary, just as it is necessary for the definition of "permanent" variables attached to atoms.

Next, why "turf/"? This is a limiting factor. this is where Kaio's "typed variable" terminology applies. you stick a modifier inside of your variable definition to limit that variable to only items of that type. If you want only mobs, instead of turfs, you would do "var/mob/t" (well, most people would do "var/mob/m", but the actual name of the variable is completely up to you, it could be virtually anything; it's just convenient to use the first letter of the type, as a reminder later on what this thing is supposed to be)

Anyway, why do we need this? Well, our "for()" is looping through the "view()" list. "view()" creates a list of everything in your view. If we did not limit var/t by specifically saying it must be of the type "turf", then our for() loop would run through everything, including any other atoms (mobs, objs, etc.) that are also in view. We don't want that. We only want it to grab the turfs, so we stick that type limiter in there to force it to only look at things of that type (we could get as specific as we want, too! if you have turf subtypes defined in your code, and placed on your map; like "turf/sand" or even more specific, like "turf/ground/dirt/topsoil"; then you could use those more-specific paths as well, to only grab turfs of those specific types.)

3. So, putting that all together, this line is saying:

"For every thing in my view, if that thing is of type /turf, stick that thing into a new, local var named "t", then run the below code using that. Repeat for all such items 9re-using "t" for each one as we come to it)."

4. So now, the next line. "new/turf/Grass2(t.loc)" This creates a new turf of turf subtype "Grass2", and places it at the position/location in the parentheses. "t.loc" means "the location of the turf that is currently referenced by "t""

This line will run once for every item in the list within the above "for()". Each turf that is in the player's view will trigger this line to run (and then the "for()" loop will move on to the next, until they've all had their turn; if your view is 11x11, then this will run 121 times)

==============================

Lots of reading, but I hope it clears things up?
Thank you all. And SuperSaiyanGokuX, I thank you specially for explaining the procs and how to use them, very appreciated. :D
Don't mention it.