Ok, I pushed the concept a bit:
markerScript entity:
Code: Select all
-- Table of markers data
md = {}
function getMarkersData()
local markers = grimq.fromAllEntitiesInWorld()
:where("name", "onParty_marker")
:toIterator()
for m in markers do
-- variables to look for in markers
local data = {}
if m.var1 then
data.var1 = m.var1
end
if m.var2 then
data.var2 = m.var2
end
if m.var3 then
data.var3 = m.var3
end
setMarker(m.id, m.level, m.x, m.y, data)
end
end
function moveMarker(id, level, x, y)
local data = md[id]
md[id] = nil
findEntity(id..".trigger"):destroy()
setMarker(id, level, x, y, data)
end
function setMarker(id, level, x, y, data)
md[id] = {}
md[id].level = level
md[id].x = x
md[id].y = y
for i, v in pairs(data) do
md[id][i] = v
end
if findEntity(id..".trigger") then
findEntity(id..".trigger"):destroy()
end
spawn("pressure_plate_hidden", level, x, y, 0, id..".trigger")
:setTriggeredByParty(true)
:setTriggeredByMonster(false)
:setTriggeredByItem(false)
:setSilent(true)
:addConnector("activate", "markerScript", "onParty")
:addConnector("deactivate", "markerScript", "onUnparty")
end
function removeMarker(id)
md[id] = nil
findEntity(id..".trigger"):destroy()
end
function onParty(trigger)
id = string.sub(trigger.id, 0, -9)
if md[id] then
--do anything you want here, calling other functions using stored data. For example:
print("on:",id)
end
end
function onUnparty(trigger)
id = string.sub(trigger.id, 0, -9)
if md[id] then
--do anything you want here, calling other functions using stored data. For example:
print("off:",id)
end
end
function autoexec()
getMarkersData()
end
and this in an .lua:
Code: Select all
cloneObject {
name = "onParty_marker",
baseObject = "script_entity",
editorIcon = 104,
}
So now it's really modular:
1. It scans onParty_marker objects in the dungeon and stores in the md table all predefined variables to look for.
2. Then, when the party enters the marker square, it calls onParty, from where you can do anything with that data.
3. When the party leaves the square, it call onUnparty.
And there's a moveMarker function, I realized that since we don't rely on the original onParty_marker object to get data, but rather on the position index of the md table, we can move that freely, just changing the index and respawning the pressure plate.
So you can use this to define an npc encounter with any amount of variables, and have it move around too.
EDIT: How it looks in the editor:
I think it makes defining events very user-friendly.
EDIT 2: Updated the code with a setMarker and removeMarker functions to allow those markers to be also generated dynamically from a script. Also added an onUparty trigger. If you just want to check what's the current id the party stepped on from onDrawGui, you could always call something like gw.setCurrentEventId(md[mIndex].markerId) from onParty and gw.setCurrentEventId(nil) from onUnparty.
EDIT 3: Realized all the level.x.y key was not necessary as I can get the id from the trigger plate. Code is much simpler now. The only problem is that you need to predefine which variables can be put in the marker. I think that using a wallText object instead of a script_entity object, we could parse it to get any variables from it without predefining them.