![]() |
Point and Copy: Cloning Device
PHP Code:
I havn't tested it, but it should work. Now, from the looks of it, it's a simple NPC, yes, but it's good enough for the new scripters to learn some of the basics. Let me break it down: This NPC shows a good way of using the keypressed function as a source for finding keycodes by designating such in the functions parameters. I used a weird combination to show that it's possible to use key combinations to perform different tasks. This NPC also shows a good way of setting up a variable to be defined as the mouse button that the player presses in the mousedown function. You can basically put anything inside the parameters and use it to read if the player has pressed the "left" mouse button, "right" mouse button, "double" mouse button (I.E double click), and "middle" mouse button (I.E pressed on the scroll ball). Once you define this, reading what mouse button the player clicked is easy, as displayed above. This NPC also shows a good way of using timevar2 as a cooldown device, without the need of using a timeout. You can use timevar2 for most cooldown systems as an alternative to timeouts, which will decrease the amount of CPU time that your system takes. As you see here, I created a cooldown system without the need of a timeout, which is more efficient when it comes to lag. This NPC also shows a way of looping through all of the players and NPCs in order to perform certain tasks that you'd wish to designate. This NPC also shows how you can manipulate certain events that will in turn cause an NPC and/or player to act as you'd like. In this case, I check to see if when the player double clicks their mouse, if their mouse position is within a certain range of a player or NPC, then the person will end up copying the outfit of the player or NPC. You can use this method to perform plenty of tasks, not just copying attire. And finally, this NPC also shows a good way of using a loop to loop through a player/npcs clothes colors without the need of separate this.colors[0], this.colors[1], etc. lines. You can use loops to do a lot of things much faster and more efficiently, such as when reading what keys the player pressed and so forth. Though it is simple to those experienced, it can actually teach the newer scripters quite a few things on how to work with several functions and so forth to make scripting a bit easier and more efficient. I scripted this NPC simply for educational purposes, though it can also be used for what it is, which is a cloning device. NOTE: If the designated NPC is not a character, then your outfit will be set to the default noob outfit. Hope this helps, Enjoy ^^ |
Just at a quick glance:
PHP Code:
PHP Code:
PHP Code:
Finally, can't you prefix your temp vars with temp.? :( |
Quote:
Quote:
I don't seem to be understanding where you're coming from, mind elaborating a bit? Quote:
But does it make a difference? From my experience this. temp vars can be used throughout the NPC whereas temp vars can't unless you send them over through the parameters of a function. |
Quote:
temp.var = !temp.var; If you are working with just true and false, then this is basically a switch. Because temp.var uninitialized is 0 (false), then !0 is 1 (true), therefore !temp.var == true. Now, if temp.var is 1 (true), then !1 is 0 (false), therefore !temp.var == false. echo(temp.foo ? "bar" : "baz"); This basically just checks if temp.foo (or any expression, even player.chat == "foo" or so) is true, and returns "bar" if it's true, and "baz" if it isn't. Quote:
Say me, you and Stefan are in the level. It'll first pick me up, and scan every NPC. It'll then move on to you, and scan every NPC. It'll finally move onto Stefan and scan every NPC. I don't think you need to scan the NPCs as many times as there are players in the level. Quote:
|
Quote:
Ah, thanks. Will try to keep that in mind from now on. Quote:
How would you recommend doing it then? Separating the loops? |
PHP Code:
if-then-else So if temp.foo is true, then that code will output "bar" else it will output "baz" |
Quote:
|
Quote:
Yeah, I updated the post where I asked how you would do it asking if I should separate the loops, which is what you ended up saying anyways :p I guess I'll do that. EDIT: Updated first post |
Skyld threw in his critques but ill put mine in anyway
PHP Code:
The nested loop was something i pointed out to skyld. isntead of for(players) { for(npcs) { } } we want for (players) { } for (npcs) { } We want to return when we have achieved our goal, so we are not achieving it multiple times (in my opinion). Also, i threw in a check to make sure the npc was a character. The rest of my notes are in the script. |
"Conditions belong on one line" is completely opinion based and it all depends on the scripters style.
I, for one, find it more efficient to style as I do simply because it's easy to go back and edit things or add on things if needed. If you put the condition bracket on the same line then you fall in risk of losing your bracket positioning and possibly missing brackets or having too many. Plus, this way looks a lot more clean IMO. As for the character check for the NPCs, it doesn't really matter, since if it's not a character it'll set you to the default noob body, which isn't exactly a problem or anything. It's a fast way to get the noob body on for people that want it, though :p Also, I try to separate condition statements for a reason. I don't really care about line length, moreso I care about flexibility. By separating your condition statements, it makes it easy to add else checks later on if you need to, without having to separate it at a later date and re-align the entire script just because you wanted to save a few lines in the beginning. It's not really lengthy at all with the way I style since everything is clearly presented. It's just easier to manage if you need to go back to it later on and fix or add-on anything. |
I guess nested Ifs come down to mere preference, I like not tabbing my code that far over.
|
| All times are GMT +2. The time now is 09:24 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.