local maps = find.File("maps/*.bsp", "GAME")
map = (maps[math.random( #maps )])
print("Map randomized to " .. map )
This code above works on "ULX" on Garry's Mod, it uses find.File to read the directory of garrysmod/maps and return (in TABLE) all of the files in it ending with .bsp (all of the maps), however I do not wan't it to include maps that begin with certain parts, like "arena_" and "gm_", is there a way I can go about removing them and/or making it keep checking until It gets a map not beginning with that.
Any way I could do this? and preferable pure Lua please.
Oh and the site I'm using to test it is MOAIFiddle
It looks like the function you meant is file.Find instead of find.File.
http://wiki.garrysmod.com/page/file/Find
The following should work in Garry's Mod.
local maps = {}
local ignoredPrefixes = {"gm_","arena_"}
for _,map in ipairs(find.File("maps/*.bsp", "GAME"))do
local hasPrefix=false
for _,prefix in ipairs(ignoredPrefixes)do
if string.Left(map,string.len(prefix)) == prefix then
hasPrefix=true
break
end
end
if not hasPrefix then
table.insert(maps,map)
end
end
local randomMap = table.Random(maps);
print("Map randomized to "..randomMap)
What it does is read all maps in maps/ and then add all maps that do not have a certain prefix, defined in ignoredPrefixes, to the maps table.
When it is done doing so, a random map will be selected from the table.
Related
I'm making an addon that adds some information to the LFG both when applying to them and when picking applicants for your own group. There are different functions and different templates for the frames for searching LFG and hosting LFG. For searching, you can use hooksecurefunc on LFGListUtil_SetSearchEntryTooltip and that will allow you to get the information on whatever group the player is hovering over.
hooksecurefunc("LFGListUtil_SetSearchEntryTooltip", function(gametooltip, resultID, autoAcceptOption)
--debugPrint("LFGListUtil_SetSearchEntryTooltip")
local entry = C_LFGList.GetSearchResultInfo(resultID)
if not entry or not entry.leaderName then
return
end
local skill, attitude, note, playerNumber = self:getPlayer(self:addServerName(entry.leaderName))
--debugPrint("hooked correctly!")
GameTooltip:AddLine("\n")
if skill ~= 0 then
GameTooltip:AddDoubleLine("Skill:", tostring(skill), 1,0.82,0.0,1,1,1)
end
if attitude ~= 0 then
GameTooltip:AddDoubleLine("Attitude:", tostring(attitude), 1,0.82,0.0,1,1,1)
end
if note ~= "" then
GameTooltip:AddLine(note,1,1,1,true)
end
end)
This method seems to almost work when you're looking for applicants to your own group. According to Wow's UI source xml, the function "LFGListApplicantMember_OnEnter" triggers whenever you enter a frame made with the LFGListApplicantMemberTemplate. As far as I can tell from the LFGList's .lua file, all of the members of an applying group should have this template. However, it only triggers when hovering over the not-leading member of a group or when hovering over their role icons.
Is there some other function I'd be able to hook into in order to make it work on the leading group? I know it's possible to add text to the tooltip of a leading member because the RaiderIO addon does it, but I can't figure out how they do it. It needs to have some way of getting the name of the player you're hovering over to use my getPlayer function on it.
This is my current test function, though obviously it doesn't work
hooksecurefunc("LFGListApplicantMember_OnEnter", function(...)
GameTooltip:AddLine("")
GameTooltip:AddLine("TestText")
debugPrint("Hooked Successfully")
end)
and this is debugPrint
local function debugPrint(...)
print(...)
end
Actually I am writting a programming language in Lua. It was quite fun. I've wrote a bit of standard library (stack op and simple io). Then I've thought about labels. It would look like in assembly. While and for loop aren't funny in any bit so programming in that language can be quite challenging. Here are some requirements for this system:
Label stack (or array, dictionary) must be accessible from global context.
Jump instruction handler will be in separate file.
This is how my label-handling function look like:
function handleLabel(name,currentLine)
end
I have no idea how to implement this kind of magic. First I've thought about that:
LabelLineIDS = {}
Labels = {}
Labelamount = 1;
function handleLabel(name,currentLine)
LabelLineIDS[Labelamount]=currentline
Labels[Labelamount]=name
Labelamount=Labelamount+1
end
-- In file "jump.lua":
local function has_value (tab, val)
for index, value in ipairs(tab) do
if value == val then
return index
end
end
print ("Error: Label not defined.") -- Bail out.
os.exit(1)
end
local function _onlabel()
local labelName = globalparams --Globalparams variable contain parameters to each function, at the moment
--this will contain label name. It _can_ be nil.
return LabelLineIDS[has_value(Labels, labelName)]
end
CurrLine = _onlabel() --Currline - current line that get's parsed.
--1 command per one line.
But I'm unsure is this well written or even work. Can you give me idea how to parse labels in my programming language (and write jump functions)? Or if my code is pretty ok at the moment, can you help me to improve it?
Using line counter in my parser I've decided to implement gotos like we can see in BASIC. Thanks everyone for help.
This code is for a modding engine, Unitale base on Unity Written in Lua
So I am trying to use a Boolean Variable in my script poseur.lua, so when certain conditions are met so I can pass it to the other script encounter.lua, where a engine Predefined functions is being uses to make actions happens base on the occurring moment.
I tried to read the engine documentation multiple times, follow the exact syntax of Lua's fonction like GetVar(), SetVar(), SetGobal(),GetGlobal().
Searching and google thing about the Language, post on the subreddit and Game Exchange and tried to solve it by myself for hours... I just can't do it and I can't understand why ?
I will show parts of my codes for each.
poseur:
-- A basic monster script skeleton you can copy and modify for your own creations.
comments = {"Smells like the work\rof an enemy stand.",
"Nidhogg_Warrior is posing like his\rlife depends on it.",
"Nidhogg_Warrior's limbs shouldn't\rbe moving in this way."}
commands = {"GREET", "JUMP", "FLIRT", "CRINGE"}
EndDialougue = {" ! ! !","ouiii"}
sprite = "poseur" --Always PNG. Extension is added automatically.
name = "Nidhogg_Warrior"
hp = 99
atk = 1
def = 1
check = "The Nidhogg_Warrior is\rsearching for the Nidhogg"
dialogbubble = "rightlarge" -- See documentation for what bubbles you have available.
canspare = false
cancheck = true
GreetCounter = 5
Berserk = false
encounter:
-- A basic encounter script skeleton you can copy and modify for your own creations.
encountertext = "Nidhogg_Warrior is\rrunning frantically"
nextwaves = {"bullettest_chaserorb"}
wavetimer = 5.0
arenasize = {155, 130}
music = "musAncientGuardian"
enemies = {"poseur"}
require("Monsters.poseur")
enemypositions = {{0, 0}}
-- A custom list with attacks to choose from.
-- Actual selection happens in EnemyDialogueEnding().
-- Put here in case you want to use it.
possible_attacks = {"bullettest_bouncy", "bullettest_chaserorb", "bullettest_touhou"}
function EncounterStarting()
-- If you want to change the game state immediately, this is the place.
Player.lv = 20
Player.hp = 99
Player.name = "Teemies"
poseur.GetVar("Berserk")
end
Thank you for reading.
The answer to my problem was to use SetGobal(), GetGobal().
For some reasons my previous attempt to simply use SetGobal()Resulted in nil value despite writing it like that SetGobal("Berserk",true) gave me a nill value error, as soon as I launch the game.
But I still used them wrong. First I needed to put it SetGobal() at the end of the condition instead of at the start of the the poseur.lua script because the change of value... for some reasons was being overwritten by it first installment.
And to test the variable in the function in my encounter.lua, I needed to write it like that
function EnemyDialogueStarting()
-- Good location for setting monster dialogue depending on how the battle is going.
if GetGlobal("Jimmies") == true then
TEEEST()
end
end
Also any tips an suggestions are still welcome !
Well firstly, in lua simple values like bool and number are copied on assignment:
global={}
a=2
global.a=a--this is a copy
a=4--this change won't affect value in table
print(global.a)--2
print(a)--4
Secondly,
SetGobal and the other mentioned functions are not part of lua language, they must be related to your engine. Probably, they use word 'Global' not as lua 'global' but in a sense defined by engine.
Depending on the engine specifics these functions might as well do a deep copy of any variable they're given (or might as well not work with complicated objects).
I asked earlier why my methods for a metatable weren't being located by Lua, and was told that by setting __index to my metatable, that it would resolve the issue, so I assumed that a method when called was searching by index in the metatable, but I've ran into an issue now that I need to use indexing brackets [ and ] on my metatable, so __indexis assigned to return an index from a table inside of it, how do I resolve the functionality needs of both using methods, and use of indexing brackets
I wrote a minimal example indicating the problem:
TestMetatable = {DataTable = {}}
TestMetatable.__index = TestMetatable
function TestMetatable.new()
local Tmp = {}
setmetatable(Tmp,TestMetatable)
Tmp.DataTable = {1}
return Tmp
end
function TestMetatable:TestMethod()
print("Ran Successfully")
end
function TestMetatable.__index(self,index)
return self.DataTable[index]
end
local Test = TestMetatable.new()
-- both functionalities are needed
print(Test[1])
Test:TestMethod()
You need to understand the difference between __index and __newindex, and their relationship with the current contents of the main table.
__newindex is only called/accessed when all the following are true:
When you are setting a value into the main table, via tbl[index] = expr (or equivalent syntax, like tbl.name = expr).
When the key you are trying to set into the main table does not already exist in the main table.
The second one trips people up often. And that's your problem here, because __index is only accessed when:
When the key being read from the main table does not already exist in the main table.
So if you want to filter every read from and write to a table, then that table must always be empty. Therefore, those reads and writes need to go into some other table you create for each new object. So your new function needs to create two tables: one that remains empty and one that has all the data in it.
Honestly, I wish Lua had a way to create just an empty piece of userdata that you could bind a user-defined metatable to, just to avoid these issues.
the way I resolved this problem, according to Nicol Bolas's solution, if it might give clarity to anyone else's confusion :-)
TestMetatable = {DataTable = {}, FunctionTable = {}}
function TestMetatable.new()
local Tmp = {}
setmetatable(Tmp,TestMetatable)
Tmp.DataTable = {1}
Tmp.FunctionTable = TestMetatable
return Tmp
end
function TestMetatable:TestMethod()
print("Ran Successfully")
end
function TestMetatable.__index(self,index)
if type(index) == "string" then
return self.FunctionTable[index]
else
return self.DataTable[index]
end
end
local Test = TestMetatable.new()
-- both functionalities are needed
print(Test[1])
Test:TestMethod()
Having this error when I run my gamemode.
[ERROR]
gamemodes/rp/gamemode/cl_init.lua:910: attempt to index field 'Config' (a nil value)
1. unknown - gamemodes/rp/gamemode/cl_init.lua:910
config is a table, with index's such as config["Run Speed"] and the entire table is set globally equal to GM.Config in the sh_config.lua file. Why is config not being registered as a value? Must I include the config file into the cl_init file? and if so, how? Using the include()?
function GM:Think()
if ( self.Config["Local Voice"] ) then **--Referred line(910)**
for k, v in pairs( player.GetAll() ) do
if ( hook.Call("PlayerCanVoice",GAMEMODE, v) ) then
if ( v:IsMuted() ) then v:SetMuted(); end
else
if ( !v:IsMuted() ) then v:SetMuted(); end
end
end
end
-- Call the base class function.
return self.BaseClass:Think();
end
Edit -- config table in sh_config.lua.
local config = {};
-- Command
config["Command Prefix"] = "/"; -- The prefix that is used for chat commands.
config["Maximum Notes"] = 2; -- Maximum notes per player
config["Advert Cost"] = 60; -- The money that it costs to advertise.
config["Advert Timeout"] = 150 -- How many seconds between adverts
config["OOC Timeout"] = 60 -- How many seconds between OOC messages
config["Item Timer"] = 7 -- How many seconds between item uses
config["Item Timer (S)"] = 20 -- How many seconds between specific item uses
config["Note Fade Speed"] = 12 -- How many minutes before nots disappear
-- Voice
config["Local Voice"] = true; -- Players can only hear a player's voice if they are near them. This is the index being called which is creating an error.
config["Talk Radius"] = 256; -- The radius of each player that
--other players have to be in to hear them talk (units).
-- Player Stuff
config["Walk Speed"] = 150; -- The speed that players walk at.
config["Run Speed"] = 275; -- The speed that players run at.
GM.Config = config;
I have includecs("sh_config.lua"); in sh_init.lua. include("sh_config.lua") and AddCSLuaFile("sh_config.lua") in init.lua. and include("sh_config.lua"); in cl_init.lua.
Im still getting this stupid error though. Can someone explain what the difference between including and Addcs'ing a file does. How do I make sh_config's variables global in other files? Or in other words how do I make the desired file(cl_init) read through the code in sh_config.lua and I can use code from it in the client side init?
You need to include sh_config.lua at the top of cl_init.lua.
include("path/to/file.lua")
Be sure to do AddCSLuaFile("path/to/lua.lua") in the init.lua file as well. You will also need to do include("path/to/file.lua") in init.lua as well. (That's only required for shared files, though)
Also I am pretty sure the scope of your config table would be limited to sh_config.lua so you should remove local from your variable declaration.
*sh_config* is a shared file - Meaning it'll have to be included both clientside, and serverside.
In init.lua - In top of the other includes. Put include("sh_config.lua")
Also in init.lua, put AddCSLuaFile("sh_config.lua") - This will make sure the file is downloaded by the client, and executed clientside.
In cl_init.lua - put include("sh_config.lua"). Also around the other includes. This should work as expected.
Seeing how this is a Config file, I assume it should be included first, or almost first. It may contain vital settings, for the rest of the script load.
Also - Often a shared.lua is included, to be equal to a shared_init file. This may be your case. If it is, you should add one include("sh_config.lua") with the includes, in shared.lua, and AddCSLuaFile("sh_config.lua") in a if CLIENT then-block