![]() |
Simple Warp/Summon
I just created this script and thought it was nice. It is my first actual code so I come here seeking advice, suggestions, and perhaps ways to make the script simpler. I am in no way trying to show off the usefulness of this script, I was merely making it for practice. I know it may be pointless, however, that is not what is in question here. Save yourself some time and a post if you are going to say that :)
Functions: -Obvious summon and warping ability ("/warp name" or "/summon name") -Can find players based on partial names (instead of "PhilSmith" it could work with "Phil" or "P") -when using a partial name, it logs all the names found in an array. If one name is found, it warps/summons. If zero names are found, it says"No Players Found". If more than one name are found, it pms the array to the player. PHP Code:
|
It's not really a big deal but you can do the "search" processing on the client-side.
Then in the case of multiple results, you can just display a dialog and have the player select who they wanted to summon. I would suggest using.. string.starts("text") or string.pos("text") for checking for partials. I.e: PHP Code:
PHP Code:
|
I did have just the dialog, but the problem was, was that the player.chat reset after like 2 seconds. I thought this was not enough time so i ended up pming it. if there is a way to keep the text from resetting, I'm all ears.
And just after i posted this i realized i forgot community names. I'm working on those :D |
PHP Code:
Note: interestingly enough, that loop at the beginning of the code has a name: filter. I have wrapped up some common patterns such as filter in this thread. With my code the first loop would simply be: PHP Code:
|
It's not bad at all, but there are some things that can be improved.
PHP Code:
You can improve the substring check by using str.starts(substring). I doubt it's really any more efficient but it's semantically better as it makes your purpose clearer to the reader. PHP Code:
PHP Code:
It's also better practice to pass parameters to functions instead of setting variables. For example, in your player found function: PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
edit: sorry for the repeats, all of the posts except the OP were while I wrote this :p There is a way to keep a player's chat from resetting after a few seconds but it can only be done on clientside, so you would have to trigger back. PHP Code:
|
I'm not sure I understand this all right now, but I'll take a few more minutes to read it over and maybe absorb the information. If Ii have questions after that, I'll ask :)
|
I think i covered most of your edits. I was confused by two of them:
1) check in all lowercase. I'm not really sure what the point of this was. Could you elaborate a little more on its purpose please? 2) the filter. I understand that it is a quicker way of checking than mine, but i just couldnt get it to work. aside from those, I corrected the other parts and added the community name and clientside shared.chat. PHP Code:
|
Thread should be moved from CodeGallery, yes?
|
It is a working code. I was just asking for thoughts & comments
|
Quote:
For example, temp.foundPlayers.size()==1 and temp.foundPlayers.size()<1 both clearly can never be true at the same time, but you should always make that clear just in case the conditions are not so clearly disjoint like they are here. Also, temp.foundPlayers.destroy(); at the start of a function makes no sense. temp.foundPlayers won't even exist then. If you are going to use it as an array, it is best to create one at that point: temp.foundPlayers = {}; Regarding the filter thing, it's just an interesting note. Most people don't program like that in GS2, but I think more people should. The less code you write, the less that can go wrong. If you're curious about it, let me know what you tried and I can hopefully tell you what went wrong. |
First, I normally do use else ifs. I dont know what i was doing with this one, but i must have forgot.
Second, my temp.foundPlayers.destroy() was me being dumb. I looked up on the wiki what i actually needed to put and fixed it. Idk why it is still there in this one... third, i believe i put something like this: PHP Code:
|
Quote:
|
| All times are GMT +2. The time now is 01:40 PM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.