Jump to content

Removing task in ListenForEvent

Recommended Posts

well, according to this http://stackoverflow.com/questions/17763223/lua-table-address-and-address-table

two runs of lua interpreter show same address


e$ luaLua 5.2.2 Copyright (C) 1994-2013 Lua.org, PUC-Rio> print({})table: 0x7fdaca4098c0> ^De$ luaLua 5.2.2 Copyright (C) 1994-2013 Lua.org, PUC-Rio> print({})table: 0x7fb02a4098c0> ^De$
but like in any interpreter - its not real memory address, its just emulation and for my task its doesnt matter.

The point made in that StackOverflow thread is that the addresses are not the same, as you can see by looking at your code snippet (it just happens that the addresses have the same prefix and suffix; the differ in the middle).

And it's not an "emulated" address, it's a real memory address. It's not a physical memory address, but that's due to the OS's virtual memory mapping: it'd be the same situation in a program run directly, such as a C or Assembly application.

To track the onattacked function set up by the follower component, you can use address comparison because the components uses the same onattacked function for every entity, so the address is the same within one instance of the game (but not throughout different instances). Though you don't need to actually extract the address via tostring() or something like that, you can simply use an equality test between functions.

But that's far from ideal. Just remove the callback based on its source:

local _G = GLOBALlocal debug = _G.debug--[[--// Removes the "attacked" callback set up within the prefabs/abigail.lua--// file.--]]AddPrefabPostInit("abigail", function(inst)	--// Will store the "attacked" callback set up by prefabs/abigail.lua	local onattacked	if inst.event_listening.attacked and inst.event_listening.attacked[inst] then		for _, fn in ipairs(inst.event_listening.attacked[inst]) do			local info = debug.getinfo(fn, "S")			if info and info.source then				if info.source:match("prefabs[/\\]abigail.lua$") then					onattacked = fn					break				end			end		end	end	if onattacked then		inst:RemoveEventCallback("attacked", onattacked)	endend)
Link to comment
Share on other sites

wow so thats lua magic that i asked for

local info = debug.getinfo(fn, "S")            if info and info.source then                if info.source:match("prefabs[/\\]abigail.lua$") then                    onattacked = fn                    break                end            end

big thx.

there also "debug.getlocal" to learn

Link to comment
Share on other sites

there also "debug.getlocal" to learn

debug.getlocal is rarely useful, because you can only get local variables of functions that are currently running (i.e., a function that called your current function, or the one that called that, and so on; in Lua a file is just a function, whose body is the contents of the file). debug.getupvalue is what you're looking for. But using the debug library should be considered a last resort: slight misuse may cause huge bugs, and these functions are slow. If you can do things some other way (unless it's something just as hackish, like getting memory addresses :razz:), then do things some other way.

Link to comment
Share on other sites

All that stuff just to edit one function call ?


Not edit a function call, extract a local function added as an event callback. Much trickier :razz:.

Though of course it'd be much simpler for us if event callbacks were added together with a unique string which could be used to reference them. Since this is not the case, and the only identifier is the function itself, there's to way to avoid extra complexity.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

  • Create New...