• Print

Author Topic: Did I do something wrong?  (Read 5173 times)

0 Members and 1 Guest are viewing this topic.

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Did I do something wrong?
« on: June 02, 2015, 02:41:02 am »
Hi, did a quick stab at this:
Code: Lua
  1. function ulx.search( calling_ply, engine, search )
  2.     if engine == "twitch" or engine == "Twitch" then
  3.         link = "\"http://www.twitch.tv/search?query="
  4.                 engine = "Twitch"
  5.     end
  6.    
  7.     if engine == "youtube" or engine == "Youtube" then
  8.         link = "\"http://www.youtube.com/results?search_query="
  9.                 engine = "Youtube"
  10.     end
  11.    
  12.     if engine == "google" or engine == "Google" then
  13.         link = "\"http://www.google.com.au/#q="
  14.                 engine = "Google"
  15.     end
  16.    
  17.     if engine == "bing" or engine == "Bing" then
  18.         link = "\"http://www.bing.com/search?q="
  19.                 engine = "Bing"
  20.     end
  21.    
  22.     if engine == "yahoo" or engine == "Yahoo" then
  23.         link = "\"http://search.yahoo.com/search?p="
  24.                 engine = "Yahoo"
  25.     end
  26.    
  27.     if link == undefined or link == null then
  28.         ULib.tsayError( calling_ply, "An error occurred, please try again.", true )
  29.         return
  30.     else
  31.                 search = search .. "\""
  32.         ULib.tsayError( calling_ply, "You searched for \"" .. search .. " on " .. engine, true )
  33.         calling_ply:SendLua("gui.OpenURL(".. link .. search ..")")
  34.     end
  35.    
  36. end
  37. local search = ulx.command( "Tom's Fun", "ulx search", ulx.search, "!search", true )
  38. search:addParam{ type=ULib.cmds.StringArg, hint="Engine: (Twitch, Youtube, Google, Bing, Yahoo." }
  39. search:addParam{ type=ULib.cmds.StringArg, hint="Search" }

It works fine but for some reason it remembers what link was used previously (not the "search" part) It also isn't appearing in a category on the menu.

Example:
If I type "ulx search google Pie" in console, it will work fine.
then If I type "ulx search asdfasdf poop" in console, it will search google for "poop" (and its not because of line 14)

And as far as I see it should be showing in "Tom's Fun" category, but nope.
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline XxLMM13xX

  • Sr. Member
  • ****
  • Posts: 265
  • Karma: -51
  • New to lua development
    • Twitch
Re: Did I do something wrong?
« Reply #1 on: June 02, 2015, 03:00:58 am »
Why not add a check to make sure the users using his a engin the command knows!

If engine == "" then
    ULib.tsayError( caling_ply, "This is not the right engine!")
    Return
End

I'm writing this on my phone so I hope it makes sense and helps you!

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Re: Did I do something wrong?
« Reply #2 on: June 02, 2015, 03:03:05 am »
Why not add a check to make sure the users using his a engin the command knows!

If engine == "" then
    ULib.tsayError( caling_ply, "This is not the right engine!")
    Return
End

I'm writing this on my phone so I hope it makes sense and helps you!

Yeah I thought so too, but it didn't work, and it wouldn't run the command to start off with if "engine" was blank.
*cough* *calling *cough*
« Last Edit: June 02, 2015, 03:08:10 am by Tomzen »
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Did I do something wrong?
« Reply #3 on: June 02, 2015, 08:35:54 am »
Undefined lua variables are nil. Not undefined or null. Lua will see your "undefined" and "null" as variables. Your test passes because they are both nil. But what if they weren't? It's better to just compare with nil.

You haven't specified who's allowed to access the command. You'll have to tell the defaultAccess function.

The reason it is sorta remembering the previous engine is because you've defined "link" as a global variable. It will remember the old value and reuse it. Make it local so it only exists within your function.

Edit: elaborated
« Last Edit: June 02, 2015, 08:45:59 am by Timmy »

Offline Caustic Soda-Senpai

  • Sr. Member
  • ****
  • Posts: 469
  • Karma: 54
  • <Insert something clever here>
    • Steam Page
Re: Did I do something wrong?
« Reply #4 on: June 02, 2015, 01:12:37 pm »
you've defined "link" as a global variable. It will remember the old value and reuse it. Make it local so it only exists within your function.

As well, if your category name isn't working, add the line
Code: Lua
  1. local CATEGORY_NAME = "Tom's Fun"
and replace
Code: Lua
  1. local search = ulx.command( "Tom's Fun", "ulx search", ulx.search, "!search", true )
with
Code: Lua
  1. local search = ulx.command( CATEGORY_NAME, "ulx search", ulx.search, "!search", true )

As well, as Timmy ALSO said, you failed to add who can access the command. Add the line:
Code: Lua
  1. search:defaultAccess( ULib.ACCESS_USER )
along with your other permissions.


Also, is there a reason as to why you have all of these as individual if statements? Why not if and elseif?
Once you get to know me, you'll find you'll have never met me at all.

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Re: Did I do something wrong?
« Reply #5 on: June 02, 2015, 06:07:33 pm »
As well, if your category name isn't working, add the line
Code: Lua
  1. local CATEGORY_NAME = "Tom's Fun"
and replace
Code: Lua
  1. local search = ulx.command( "Tom's Fun", "ulx search", ulx.search, "!search", true )
with
Code: Lua
  1. local search = ulx.command( CATEGORY_NAME, "ulx search", ulx.search, "!search", true )

As well, as Timmy ALSO said, you failed to add who can access the command. Add the line:
Code: Lua
  1. search:defaultAccess( ULib.ACCESS_USER )
along with your other permissions.


Also, is there a reason as to why you have all of these as individual if statements? Why not if and elseif?

Alright, I changed all that and It works (haven't tested the search part, gotta go out now) but If I use

Code: Lua
  1. search:defaultAccess( ULib.ACCESS_USER )
I get "bad argument #1 to ULib.cmds.TranslateCommand:defaultAccess (string expected)"

So right now I'm using:
Code: Lua
  1. search:defaultAccess( ULib.ACCESS_ALL )
Which allows it in the menu but USER dosent have access by default

In Full this is what I currently have:
Code: Lua
  1. local CATEGORY_NAME = "Tom's Commands"
  2. function ulx.search( calling_ply, engine, search )
  3.  
  4. if engine == "" then
  5.         return
  6. end
  7.  
  8.     if engine == "twitch" or engine == "Twitch" then
  9.         link = "\"http://www.twitch.tv/search?query="
  10.                 engine = "Twitch"
  11.     end
  12.  
  13.     if engine == "youtube" or engine == "Youtube" then
  14.         link = "\"http://www.youtube.com/results?search_query="
  15.                 engine = "Youtube"
  16.     end
  17.  
  18.     if engine == "google" or engine == "Google" then
  19.         link = "\"http://www.google.com.au/#q="
  20.                 engine = "Google"
  21.     end
  22.  
  23.     if engine == "bing" or engine == "Bing" then
  24.         link = "\"http://www.bing.com/search?q="
  25.                 engine = "Bing"
  26.     end
  27.  
  28.     if engine == "yahoo" or engine == "Yahoo" then
  29.         link = "\"http://search.yahoo.com/search?p="
  30.                 engine = "Yahoo"
  31.     end
  32.  
  33.     if link == nil or link == "" then
  34.         ULib.tsayError( calling_ply, "An error occurred, please try again.", true )
  35.         return
  36.     else
  37.                 search = search .. "\""
  38.         ULib.tsayError( calling_ply, "You searched for \"" .. search .. " on " .. engine, true )
  39.         calling_ply:SendLua("gui.OpenURL(".. link .. search ..")")
  40.     end
  41.  
  42. end
  43. local search = ulx.command( CATEGORY_NAME, "ulx search", ulx.search, "!search", true )
  44. search:addParam{ type=ULib.cmds.StringArg, hint="Engine: (Twitch, Youtube, Google, Bing, Yahoo." }
  45. search:addParam{ type=ULib.cmds.StringArg, hint="Search" }
  46. search:defaultAccess( ULib.ACCESS_ALL )

Also the reason I was using "undefined" and "null" was out of habit, I used to do a lot of javascript and this nature of code normally requires that.
« Last Edit: June 02, 2015, 06:36:06 pm by Tomzen »
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: Did I do something wrong?
« Reply #6 on: June 02, 2015, 07:50:49 pm »
"link" is still a global variable so it will still remember your links.  Add a local declaration in there.  (local link = false)

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Re: Did I do something wrong?
« Reply #7 on: June 02, 2015, 08:32:52 pm »
Ahh, thanks, I'll apply it when I get home.
And how about the other problem?
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline Caustic Soda-Senpai

  • Sr. Member
  • ****
  • Posts: 469
  • Karma: 54
  • <Insert something clever here>
    • Steam Page
Re: Did I do something wrong?
« Reply #8 on: June 02, 2015, 09:26:36 pm »
I flamingoed-up when i did the ULib.ACCESS_USER, it should indeed have been: ULib.ACCESS_ALL. If user doesn't have permission by default, just give it to them in the XGUI permissions.
Once you get to know me, you'll find you'll have never met me at all.

Offline joalars

  • Newbie
  • *
  • Posts: 1
  • Karma: -1
  • "I don't eat very much."
Re: Did I do something wrong?
« Reply #9 on: June 11, 2015, 10:38:43 am »
--totally unrelated test post--

  • Print