ID:158856
 
How would i make a turret that could be placed by a usr?
By creating that Turret object (hint: new)
In response to GhostAnime
..... thats not what i meant. I mean how would i code the turret, eg. making it fire when someone comes near it?
In response to Neos300
Precisely the same way as you'd write any other mob AI, only it wouldn't move. You'd find something like this in Zilal's Beginner Tutorial (ZBT).

Also: "usr" does not mean "player". Don't confuse them or it'll make you screw something up very badly.
In response to Garthor
Okay, its just that ive never coded ai before
In response to Neos300
Basically, there's no one best way to do it. With practice coding, you'll eventually start breaking down what you want to do into things you know how to do.

Example: I want a turret that can be placed by the user and will attack nearby enemies.

Execution:

* I want a turret that can be placed by the user...

This part defines that this turret object you're creating is going to be portable. What solutions can we think of here?

One solution I can think of is to have this turret riding around in the player mob.contents for awhile and the player is able to drop the turret where he wants.

Another is you could have some kind of verb the user can use which simply creates it where the user specifies if certain conditions are met.

* ... and will attack nearby enemies.

This part defines the behavior of the turret when it's activated. What solutions can we think of here?

One solution is you could put the turret on a while() loop that contains a sleep() so it comes back every once in awhile to loop again. This loop will have code that regularly generates a list from enemies in view() and fires at the nearest one.

That's actually a bit bloaty because now you've a process for every turret you have in the game. What would be more efficient? Perhaps instead of having turrets scanning for targets constantly, you could code the turf.Enter() proc to broadcast to any turrets in view() that the target is now able to be shot at. That would be a lot more efficient because turrets that are way out in the boonies with no targets in range can sleep without any clock cycles at all.

Don't worry if these solutions didn't jump out at you right away. As you continue to practice, you will have more tools rattling around in your head to be able to come up with solutions like this. Before long, you probably would even be able to come up with way better solutions. The trouble becomes not figuring out how to make the turret, the trouble becomes stopping making better and better turrets long enough to finish the game!

You know, we should probably have a "basic coding practice" sticky at the start of this forum. It wouldn't talk about specifics you'd see in the references, it would talk about the mental mindset establishment for coding. Although, maybe that's too big of a fish to put in one sticky?
In response to Geldonyetich
Geldonyetich wrote:
That's actually a bit bloaty because now you've a process for every turret you have in the game. What would be more efficient? Perhaps instead of having turrets scanning for targets constantly, you could code the turf.Enter() proc to broadcast to any turrets in view() that the target is now able to be shot at. That would be a lot more efficient because turrets that are way out in the boonies with no targets in range can sleep without any clock cycles at all.

If mobs are moving often, then this would be even worse. Because they both are looping through view(), the question to ask is: are turrets searching for targets more often than mobs are moving? Probably not.

Also, Enter() would not be the place to put this, as it should only be used for pathfinding type stuff. Entered() would be more appropriate, but still wrong. mob/Move() is where it would belong.
In response to Garthor
Garthor wrote:

Also, Enter() would not be the place to put this, as it should only be used for pathfinding type stuff. Entered() would be more appropriate, but still wrong. mob/Move() is where it would belong.

Right, Entered() is what I meant.

Mob.Move() is not a bad idea, but the difference is minimal. Assuming we're going to use Mob.Move() all the time on the grounds we want turrets to be able to see them, both Mob.Move() and turf.Entered() are called every time the mob moves. So the execution amount would be about the same, except you'd have to put a different/additional kind of type check on Turf.Entered().

Anyway, this is just the same point I was making: there's many ways to skin a cat. Which is right for your game will be up to you with a bigger picture concept of the overall scenario.
In response to Geldonyetich
The difference is that turf/Entered() is a proc that belongs to turfs, and mob/Move() is a proc that belongs to mobs. Because this is a concept encapsulated by mobs, it makes much, much more sense to put it in mob/Move().
In response to Garthor
Garthor wrote:
The difference is that turf/Entered() is a proc that belongs to turfs, and mob/Move() is a proc that belongs to mobs. Because this is a concept encapsulated by mobs, it makes much, much more sense to put it in mob/Move().

Sure, you could say that turret tracking is all about the mobs. In many cases, you'd be completely right here - define the tracking on the appropriate types of mob and it will be a little more efficient than turf.Entered().

Alternately, you could say that turret tracking is all about the terrain's relationship between the mobs and turrets. Does it, then, mean that it makes a lot more sense to define the turret tracking code on the turf?

Not necessarily. Basically, we're talking about such an abstract concept of non-existing games that it's impossible to derive which scenario we're dealing with. Whichever approach you're more likely to go with will very much depend on what's more important to your game.

This is what I was trying to convey to the original poster: there's many ways to go about this, and how will depend very much on the scenario.
In response to Geldonyetich
No, you don't get to play the "everybody is right" card. This isn't kindergarten. Object-oriented prorgamming is flexible, but that doesn't mean you can justify ANYTHING as being "right". While your method would functionally work, it would be a mess and unorganized. I'd give an example but it should be blatantly obvious that code pertaining to mobs should belong to mobs. Turfs have nothing to do with it!

I don't understand how you aren't grasping this. Do you just refuse to accept that you were wrong?
In response to Garthor
turf/Entered() would be more appropriate, actually, for 2 main reasons:
1) We only want to catch movements into turfs here.
2) More importantly, Entered() as a global solution in every project is more robust; Move() is just one proc that can move an object, but a good programmer may also add others since Move() isn't suitable for all kinds of movements. Those procs would of course trigger Entered() properly. If you override Move(), you don't catch entering-into-turfs that could've been initiated from different moving-procs. Of course, not every project may have those, but if you take into account the possibilities then this is more robust.
As for the object orientism, that's more of a tidiness issue, not a functionality issue like above, but it doesn't really apply since you could easily farm out the code to a movable proc that's called from turf/Entered().
In response to Garthor
Garthor wrote:

I don't understand how you aren't grasping this. Do you just refuse to accept that you were wrong?

Honestly, I stopped caring about being right and caring more about questioning the validity what I thought was right years ago, thereby thinking outside the box and attaining an understanding that there's really nothing more debilitating than believing there is only one right answer to anything, even in the stark logic governing the manipulation of cold 1s an 0s.

However, I do remember my impetuous youth when being The Right Guy was so very important to me, so if that's what you want, you can have it.

You're so right. Thank you for your needed clarification that defining the code on the mob is always the right thing to do. If, at some point in the future, it seems to make more organizational sense to me to do it otherwise, clearly I would be suffering from some kind of delusion. It's good thing people like you are here to stop my idiocy from spreading ignorance upon any fool who would listen to me. I thank you, and humanity thanks you.

Even if that wasn't said tongue-in-cheek, was that as productive and/or necessary as you thought it would be?

I don't play tug-of-war on forums. Not anymore. It's a young man's game. Creatively coming up with ways to argue you're right is actually not that challenging: you'd be very surprised at just how flexible ideas really are. Two people could argue and be completely right from their own perspective for dozens of pages of text. No, the real trick is in understanding what the other guy is getting at. If you're going to insist that defining turret behavior on the mob is totally and always right and don't you even argue with that then you're not even trying to understand any perspective other than your own possesses validity. Consequently, you may be right in specifics, but you will always be wrong universally.

Thus, I regretfully cannot in good conscience grant you your requested consensus that your way is and shall ever be the only right way. However, as I was young once as well, and am still prone to bouts of stubbornness as well (this very back and forth we've been having lately is proof of that) I still respect you as an individual.