WoW Lua error; attempt to call a global(a nil value) - lua

I'm new to Lua and was following the tutorial on https://wowwiki.fandom.com/wiki/AddOn_tutorial but i just cant get it to work; i have copied the code but i get the error message "attempt to call a global 'functionname' (a nil value)" on both the SetMapToCurrentZone() and the GetPlayerMapPosition("player") functions.
This is the entire Lua file;
local zone = nil
local TimeSinceLastUpdate = 0
local function UpdateCoordinates(self, elapsed)
if zone ~= GetRealZoneText() then
zone = GetRealZoneText()
SetMapToCurrentZone()
end
TimeSinceLastUpdate = TimeSinceLastUpdate + elapsed
if TimeSinceLastUpdate > .5 then
TimeSinceLastUpdate = 0
local posX, posY = GetPlayerMapPosition("player");
local x = math.floor(posX * 10000)/100
local y = math.floor(posY*10000)/100
eCoordinatesFontString:SetText("|c98FB98ff("..x..", "..y..")")
end
end
function eCoordinates_OnLoad(self, event,...)
self:RegisterEvent("ADDON_LOADED")
end
function eCoordinates_OnEvent(self, event, ...)
if event == "ADDON_LOADED" and ... == "eCoordinates" then
self:UnregisterEvent("ADDON_LOADED")
eCoordinates:SetSize(100, 50)
eCoordinates:SetPoint("TOP", "Minimap", "BOTTOM", 5, -5)
eCoordinates:SetScript("OnUpdate", UpdateCoordinates)
local coordsFont = eCoordinates:CreateFontString("eCoordinatesFontString", "ARTWORK", "GameFontNormal")
coordsFont:SetPoint("CENTER", "eCoordinates", "CENTER", 0, 0)
coordsFont:Show()
eCoordinates:Show()
end
end
How do i fix it?

These functions have been renamed and moved to a wrapper object in 8.0, to C_Map.GetBestMapForUnit (requiring "player" as parameter to yield the same result) and C_Map.GetPlayerMapPosition respectively.
You can probably expect more functions that are called later to throw the same error, their lines just couldn't be reached before. I can check the whole code example when I'm at a desktop again, but you may simply look up these functions on Wowpedia, especially other map-related functions.
I suggest using Wowpedia over Wowwiki, it's a personal preference/impression, the former seems to receive more constant updates.
(The two seem to be merging again now, after splitting 10 years ago)

Related

Luau Couroutine stopping thread

I'm trying to make a OnChange Event Listener, I thought of using coroutines to poll the value in a loop, and see if it changed, so
function Changed(Value)
local StartingValue = Value
while true do
if StartingValue ~= Value then
print(Value)
StartingValue = Value
break
end
end
end
local n = 0
local co = coroutine.wrap(function()
Changed(n)
coroutine.yield()
end)
co()
n = (n + 1)
print("Script ended")
Is the Code I thought of for now, But it gets stuck in the Coroutine.. nothing happens after co()
What's going on?
The function Changed() never returns.
At the very beginning you're assigning the Value to the StartingValue, and the break is inside of the if statement that is only executed when the values are different. But they're not because of that initial assignment. So you have the infinite loop that has no means of ending.

Attempt to call global "Sicherheitskreis" (a nil value ) stack traceback

I'm trying to secure the doors of my CNC with switchs that send a Signal to my Laptop (I got Mach 4 on it). I created this Code that should make the Spindle stay still if the doors aren't locked, but I always get an error that says :
[string""]1576 attempt to call global 'SicherheitsKreis' (a Nil value) stack traceback:
I've tried to move the Code around and read topics on this, but Nothing works.
Does someone have a solution ?
Here's all my Code :
function SicherheitsKreis(Schliesserstate, Oeffnerstate)
if (Schliesserstate ==0 and Oeffnerstate ==1 ) then
mc.mcSpindleSetDirection(inst,0)
elseif (Schliesserstate == 1 and Oeffnerstate == 0 ) then
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGet State(sigh);
if (sigState == 1) then
mc.mcSpindleSetDirection(inst,0)
else
mc.mcSpindleSetDirection(inst,1);
end
else
mc.mcSpindleSetDirection(inst,0)
end
end
if (mc.mcInEditor() == 1) then
SicherheitsKreis()
end
The Code that I'm using to call SicherheitsKreis is :
local inst = mc.mcGetInstance()
local hsigSchliesser = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT8);
local hsigOeffner = mc.mcSignalGetHandle (inst, mc.ISIG_INPUT9);
local Schliesserstate = mc.mcSignalGetState(hsigSchliesser);
local Oeffnerstate = mc.mcSignalGetState(hsigSchliesser);
SicherheitsKreis(Schliesserstate, Oeffnerstate)
This Script is typed in Mach 4 and the function is saved as a m function (nach4 has free m function that the user can customize) in the Memory of Mach 4 (for my Computer it is m146)
Your code:
function SicherheitsKreis(Schliesserstate, Oeffnerstate)
...
end
First possibility is that code is in some other function or other element, so it is not global. Second possibility is that you run SicherheitsKreis(Schliesserstate, Oeffnerstate) before loading this part of code. Third (very uncommon) is that you override it by SicherheitsKreis = nil or equivalent. There is no other possibilities.

bad argument #1 to 'random' (number expected, got nil)

Problem using math.random maybe?
Here's the code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_BLUE)
setCombatParam(combat, COMBAT_PARAM_AGGRESSIVE, false)
function onGetFormulaValues(cid, level, maglevel)
local min = (((level/5)+(maglevel*1) +1))
local max = (((level/5)+(maglevel*2) +3))
return min, max
end
setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")
local condition = createConditionObject(CONDITION_REGENERATION)
setConditionParam(condition, CONDITION_PARAM_SUBID, 1)
setConditionParam(condition, CONDITION_PARAM_BUFF, true)
setConditionParam(condition, CONDITION_PARAM_TICKS, 1 * 60 * 1000)
setConditionParam(condition, CONDITION_PARAM_HEALTHGAIN, math.max(math.random(min, max)))
setConditionParam(condition, CONDITION_PARAM_HEALTHTICKS, 1000)
setCombatCondition(combat, condition)
function onCastSpell(cid, var)
return doCombat(cid, combat, var)
end
Its says the errors is in line 17, which is the math.random one. I have very little knowledge in Lua and this code I got from a forum, that the person posted in 2012, so I can't ask them.

Trying to implement object-oriented programming in Lua, but it's not quite working

Ok, so I'm trying to follow the instructions found here: https://www.lua.org/pil/16.1.html to do something resembling OO programming in Lua (and the LOVE game framework), but it's not working. Here's the core of my code. I have a generic Object class:
local Object = {}
function Object:load(arg)
end
function Object:update(dt)
end
function Object:draw()
end
function Object:new(arg)
o = {}
setmetatable(o, self)
self.__index = self
o:load(arg)
return o
end
return Object
and a Ship class that inherits from it:
Object = require('objects.object')
local Ship = Object:new()
Ship.sprite = love.graphics.newImage('assets/sprites/ship.png')
Ship.sprite:setFilter('nearest', 'nearest', 0)
Ship.px = Ship.sprite:getWidth()/2
Ship.py = Ship.sprite:getHeight()/2
function Ship:load(arg)
self.x = arg.x or 0
self.y = arg.y or 0
self.sx = arg.sx or arg.s or 1
self.sy = arg.sy or arg.s or 1
self.rot = arg.rot or 0
self.tint = arg.tint or {255, 255, 255}
end
function Ship:draw()
love.graphics.setColor(self.tint)
love.graphics.draw(self.sprite, self.x, self.y, self.rot,
self.sx, self.sy, self.px, self.py)
love.graphics.setColor({255, 255, 255})
end
return Ship
Now the problem is, I create two of these Ships as members of another object with this code:
self.ship1 = Ship:new({x=50, y=self.y1, s=2, tint={0, 0.5, 1}})
self.ship2 = Ship:new({x=750, y=self.y2, s=-2, tint={1, 0.5, 0}})
But when I draw them, I only see one - the second. As it turns out, it's like the code above doesn't assign the new instances of Ship to ship1 and ship2, but directly to self, for reasons that I can't understand. Did I do something wrong or is this a weird bug of the interpreter?
It is not bug of the interpreter, it is designed behavior of the language. o={} creates global variable, which is not expected by the programmer here. "Why it is so?" is a frequent question to the language creator. There are many efforts to take control over that behavior simpler .
o = {} without local creates global variable global variable is accessible and shared between all the functions in the program, unless you use fancy environment scoping techniques. Using global variable inside a function opens up doors for various side effects, you should be careful with side effects.
I've removed some of the syntactic sugar and added code above can be equivalently written as follows:
Object.new = function(table_before_colon,arg)
highlander = {} --global variable, there can be only one
setmetatable(highlander,table_before_colon);
table_before_colon.__index = table_before_colon;
highlander:load(arg) -- table_before_colon.__index.load(highlander,arg)
return highlander
end
local Ship = Object:new()
--global highlander == Ship
--Ship.new points to Object.new
function Ship:load(arg)--equivalent to: Ship.load=function(self,arg)
--code that sets fields of the `self` object and is called from within new
self.x = arg.x or 0 -- equivalently highlander.x=arg.x or 0
end
Now, the presence of the global variable would not matter, if nothing happened to it in the period from the start of the new till the new returns. But, apparently, your other code is similar to this:
local OtherObject = Object:new()
--otherObject == highlander
OtherObject.load = function(new_other_obj,arg)
--highlander == new_other_obj
new_other_obj.ship1 = Ship:new({x=50, y=self.y1, s=2, tint={0, 0.5, 1}})
--highlander == new_other_obj.ship1
new_other_obj.ship2 = Ship:new({x=750, y=self.y2, s=-2, tint={1, 0.5, 0}})
--highlander == new_other_obj.ship2
end
So, OtherObject.load calls other functions, and those functions also access and modify the same global variable.
local some_object = OtherObject:new()
returns the global variable as it is at the end of the call, which is last set to the ship2 inside the call to Ship:new inside the call to OtherObject.load inside call to OtherObject:new.
Solved! Apparently, what was needed was this little snippet:
function Object:new(arg)
local o = {}
setmetatable(o, self)
self.__index = self
o:load(arg)
return o
end
Making o local in all my new methods solved everything. I don't know how it worked exactly but I assume having it in the global variable space broke something when the meta tables are set.

How can I skip back a few seconds after pause in VLC Media Player?

I've searched this on the internet, but can't seem to find anything. I know there are hotkeys to skip back a few seconds, and I know there are hotkeys to to pause and play audio/video in VLC Media Player. However, I am using a single foot pedal for transcription and essentially need it to do both. I would like the pedal tap to pause the audio. Then, after tapping again, I would like it to play the audio, but skipping a few seconds back when doing so. Is this possible?
Save this code as rollback.lua and place it in Program Files\VideoLAN\VLC\lua\extensions folder. Then activate it through View > Rollback X Seconds.
function descriptor()
return {
title = "Rollback X Seconds",
capabilities = {"input-listener", "playing-listener"}
}
end
function activate()
end
function close()
end
function deactivate()
end
function input_changed()
end
function playing_changed()
local TIME_DELTA = -3
if vlc.playlist.status()=="paused" then
local timePassed = vlc.var.get(vlc.object.input(), "time")
local newTime = timePassed + TIME_DELTA
vlc.osd.message(TIME_DELTA, vlc.osd.channel_register(), "top-left", 1400000)
vlc.var.set(vlc.object.input(), "time", newTime)
end
end
Change the variable TIME_DELTA to whatever time change you want on pause
I expanded on the code provided by Tee by adding a GUI.
I would also like to add a few details of how to get it working.
Create a empty file, name it rollback.lua.
Copy the code provided underneath this list, paste into the file and save.
Move the file to VLCs folder for lua extensions, it should look something like this:
c:/Program Files/VideoLAN/VLC/lua/extensions
Restart VLC player.
Activate the script by going to (Note! Needs to be done each time you start VLC to use the plugin)
view > Rollback X Seconds
Select time in seconds you want to rollback and hit save (or save and close).
In order to get your pedal working with this script, simply configure the hotkey in VLC for play/pause toggle.
VLC > Tools > Preferences > Hotkeys > Set "Play/Pause" Global value to be your pedal.
(After you change a Global hotkey, you need to restart VLC in order to get it working...)
The code:
micro_second_unit = 1000000
TIME_DELTA = 1 --Default,
SHOW_OUTPUT = true
TIME_TO_DISPLAY = 1.5 --Default
function descriptor()
return {
title = "Rollback X Seconds",
capabilities = {"input-listener", "playing-listener"}
}
end
function activate()
dlg = vlc.dialog("Rollback X Seconds")
rollback_input = dlg:add_text_input("1", 2, 1 )
rollback_input_label = dlg:add_label("Seconds to back", 1, 1)
checkBox = dlg:add_check_box("Show output time ", wasChecked, 3, 3 )
timeTo_display_box = dlg:add_text_input(1.5, 2 ,3)
timeTo_display_box_label = dlg:add_label("Seconds To Display", 1, 3)
w2 = dlg:add_button("Save settings", change_step, 3, 4, 1, 1)
w2 = dlg:add_button("Save and close", updateAndClose, 3, 5, 1, 1)
done = dlg:add_label( "" , 3, 6)
end
function close()
end
function deactivate()
vlc.deactivate()
end
function input_changed()
end
function playing_changed()
if vlc.playlist.status()=="paused" then
local timePassed = tonumber(vlc.var.get(vlc.object.input(), "time"))
local newTime = timePassed - seconds_to_microsecond(TIME_DELTA)
local newTime_inSeconds = (newTime/1000000)
local newTime_inMinutes = (newTime_inSeconds/60)
local newTime_inSeconds_restOfMin = math.fmod(newTime_inSeconds,60)
local newTime_str = math.floor(newTime_inMinutes) .. "min " .. round(newTime_inSeconds_restOfMin,1) .."sec"
local timePassed_inSeconds = (timePassed/1000000)
local timePassed_inMinutes = (timePassed_inSeconds/60)
local timePassed_inSeconds_restOfMin = math.fmod(timePassed_inSeconds,60)
local timePassed_str = math.floor(timePassed_inMinutes) .. "min " .. round(timePassed_inSeconds_restOfMin,1) .."sec"
if SHOW_OUTPUT == true then
vlc.osd.message("Seconds to Back:" .. TIME_DELTA .. "sec", vlc.osd.channel_register(), "top-right", seconds_to_microsecond(TIME_TO_DISPLAY))
vlc.osd.message("New time:" .. newTime_str, vlc.osd.channel_register(), "top-left", seconds_to_microsecond(TIME_TO_DISPLAY))
vlc.osd.message("Old time:" .. timePassed_str, vlc.osd.channel_register(), "bottom-left", seconds_to_microsecond(TIME_TO_DISPLAY))
end
vlc.var.set(vlc.object.input(), "time", newTime)
end
end
function updateAndClose()
change_step()
dlg:delete()
end
function change_step()
done:set_text("Saved")
TIME_DELTA = tonumber(rollback_input:get_text())
SHOW_OUTPUT = checkBox:get_checked()
if SHOW_OUTPUT == true then
TIME_TO_DISPLAY = tonumber(timeTo_display_box:get_text())
end
end
function round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
function microsecond_to_seconds(timeToConvert)
return timeToConvert / micro_second_unit;
end
function seconds_to_microsecond(timeToConvert)
return timeToConvert * micro_second_unit;
end
This will work just like Tee's script, the video will jump back each time it's paused.
Only difference is that you can set the amount of time to jump back each time you start the script.
The amount is given in seconds, however you can use decimals for more control...
There's also a checkbox you can tick if you want to display information about the jump.
I also want to thank Tee for his answer!
This is the first lua code I've written (or rather, modified) and it's a bit messy, but it works and I have used it every day since so I thought I might as well share it.

Resources