Zo Kath Ra wrote:Duncan1246 wrote:I have added "disable self" to the lever now, but the upvalue issue can occurs still if a save is made between the two actions. I hope Minmay (or another guy more skilled in Lua then I am... ) can suggest a solution. Rewriting some of these scripts is a hard work, and the result could be worse...
If you want to fix this function:
Code: Select all
function upparty()
local dx=0
local dy=0
local h=0
dungeon_air_pit_1.light:enable()
dungeon_air_pit_1.sound:enable()
dungeon_air_pit_1.particle:enable()
fw.script:set('party.Finisterrae.onMove',function(hook,party,dir)
local parx=party.go.x
local pary=party.go.y
local el=party.go.elevation
local fac=party.go.facing
if parx==15 and pary==7 and dir==1 then
dx=1 dy=0 h=1
party.go.party:shakeCamera(0.03,4,0.5)
elseif parx==16 and pary==8 and dir==0 then
dx=0 dy=-1 h=1
party.go.party:shakeCamera(0.03,4,0.5)
elseif parx==17 and pary==7 and dir==3 then
dx=-1 dy=0 h=1
party.go.party:shakeCamera(0.03,4,0.5)
elseif parx==16 and pary==6 and dir==2 then
dx=0 dy=1 h=1
party.go.party:shakeCamera(0.04,4,0.5)
else return
end
party.go:setPosition(parx+dx,pary+dy,fac,h,party.go.level)
if (party.go.x==16 and party.go.y==7 and party.go.elevation==1) and (dir==2 or dir==3) then
turnpf_timer.timer:stop()
turnparty_timer.timer:start()
turnpf_timer.timer:start() end
end)
end
Then you just have to remove the keyword "local" from each of these lines:
local dx=0
local dy=0
local h=0
Actually this won't do what you want it to because the environment of the function will change to the fw ScriptComponent when the game is reloaded, removing those variables.
Read section 2 here. Instead you should move the variables inside the function, since you are never using them outside the function anyway:
Code: Select all
function upparty()
dungeon_air_pit_1.light:enable()
dungeon_air_pit_1.sound:enable()
dungeon_air_pit_1.particle:enable()
fw.script:set('party.Finisterrae.onMove',function(hook,party,dir)
local parx=party.go.x
local pary=party.go.y
local el=party.go.elevation
local fac=party.go.facing
local dx=0
local dy=0
local h=0
if parx==15 and pary==7 and dir==1 then
dx=1 dy=0 h=1
party.go.party:shakeCamera(0.03,4,0.5)
elseif parx==16 and pary==8 and dir==0 then
dx=0 dy=-1 h=1
party.go.party:shakeCamera(0.03,4,0.5)
elseif parx==17 and pary==7 and dir==3 then
dx=-1 dy=0 h=1
party.go.party:shakeCamera(0.03,4,0.5)
elseif parx==16 and pary==6 and dir==2 then
dx=0 dy=1 h=1
party.go.party:shakeCamera(0.04,4,0.5)
else return
end
party.go:setPosition(parx+dx,pary+dy,fac,h,party.go.level)
if (party.go.x==16 and party.go.y==7 and party.go.elevation==1) and (dir==2 or dir==3) then
turnpf_timer.timer:stop()
turnparty_timer.timer:start()
turnpf_timer.timer:start() end
end)
end
If you have more than one reference to a function in your dungeon, its environment will potentially change when the game is saved and loaded, so you should only use the function's local namespace (not that of its containing script entity) and the global namespace. If you need to use variables belonging to this specific script entity in that function then you actually need to do something like this:
Code: Select all
dx=0
dy=0
h=0
function setDx(v)
dx=v
end
function setDy(v)
dy=v
end
function setH(v)
h=v
end
function upparty()
dungeon_air_pit_1.light:enable()
dungeon_air_pit_1.sound:enable()
dungeon_air_pit_1.particle:enable()
fw.script:set('party.Finisterrae.onMove',function(hook,party,dir)
local parx=party.go.x
local pary=party.go.y
local el=party.go.elevation
local fac=party.go.facing
if parx==15 and pary==7 and dir==1 then
goround_script.script.setDx(1) goround_script.script.setDy(0) goround_script.script.setH(1)
... etc
This is not exactly convenient, so if you want to use this sort of hook in combination with persistent variables that belong to the script entity, you should probably change the hook framework to store the id of the entity, ScriptComponent, and function, like it does for connectors, instead of storing the actual function there.
Technically you can guarantee which environment a function with multiple references will end up with, if you are able to ensure that the savegame code reaches the relevant entities in a predictable order - I think that if you put the fw script entity at the bottom right corner of the last-numbered level, and kept permanent references to all your hook functions in the ScriptComponents where you apply them, the functions would keep their original ScriptComponents' environment (assuming, of course, that you do not have even more references to them somewhere else). But this would be very risky, bad practice; I would strongly recommend making your hook functions environment-independent instead.