Hi all - I've made some decent progress but I'm having a weird blocker. Here's my current process:
* I've set up a script that reads a pre-determined file - cbanims.txt. Each line in this file has a string (i.e. "cb_sm1-01_d") which is indexed into a variable (if I'm saying that correctly).
* My script has a function ("readfile.showcbanim()") which uses this index to figure out which animation to play.
* From FRED, my intention is then to call my scripted function with the desired index number as an argument to dynamically play different animations based on in-mission triggers - i.e. invoke script-eval with readfile.showcbanim(2) will read the second line in my text file, and then play the anim file it references. The whole purpose of this is to get around the script-eval character limit in FRED of course.
Seems simple enough, right? I have this all laid out and functional. If I write "readfile.showcbanim(some number)" at the very end of my script with an "On HUD Draw" hook, this works perfectly - I can change the number in the script and the associated string with an anim filename that it finds will load and play.
However, if I call the script through FRED via script-eval, this doesn't work at all - or more specifically, the gr.drawImage component doesn't work. I stuck a ba.warning into my function so I'd get a pop-up alert upon execution, and even though this alert DOES trigger (and does show the expected variable data), the animation itself never actually plays. The image / texture area itself actually never even appears - it's like gr.drawImage just gets arbitrarily passed over if FRED is involved.
I've experimented with gr.drawString and I'm getting a similar effect - I just can't seem to invoke either of these if I'm using a function called from FRED for some reason. If I call the function from the end of the script itself however, they work fine.
Here's my horrible mess of code - does anybody have any idea what I'm doing wrong?
#Conditional Hooks
$Application: FS2_Open
$On HUD Draw:
[
readfile = {} --Sets up our table and gets it ready to store strings.
readfile.strings = {}
readfile.filename = "cbanims.txt" --We're going to be pulling our strings from "cbanims.txt"
if cf.fileExists(readfile.filename, "data/scripts",true) then --Confirms the existence of, then opens cbanims.txt in the data\scripts sub-folder.
local file = cf.openFile(readfile.filename,"r","data/scripts")
local i = 1
while true do
local entry = file:read("*l")
if entry == nil then break end
readfile.strings[i] = entry --Store indexed lines from cbanims.txt in readfile.strings
i=i+1
--ba.warning("Doing the things!")
end
file:close()
end
readfile.showcbanim = function(line) --Function called from FRED for showing our anim - uses the line number in cbanim.txt as an argument
local t = readfile.strings[line]
if t then
local f = loadstring(t)
if t then
ba.warning("Attempting to render " ..t.. " ") --Blast a debug notice confirming what file we're loading (confirm data is being read correctly)
cbanim = gr.loadTexture(""..t.."",true) --Pull what anim resource to use from cbanims.txt and store it in cbanim
totalframes = cbanim:getFramesLeft() --Magic 3 lines to make the animation animate
animfps = cbanim:getFPS()
missiontime = mn.getMissionTime()
screenx = gr.getScreenWidth()
screeny = gr.getScreenHeight()
i = (missiontime * animfps) % totalframes
x = (screenx - cbanim:getWidth()) / 2
y = (screeny - cbanim:getHeight()) / 2 + 250
gr.drawImage(cbanim[i],x, y) --Play our animation
else
ba.warning("'"..t.."' loaded from line "..line.." of "..t.." and called from readfile.showcbanim() is not valid.") --Give a debug warning if string loaded isn't valid
end
else
ba.warning("readfile.showcbanim() has just attempted to load line "..line.." from "..readfile.filename.." and failed. Most likely this line or the file does not exist.") --Give a debug warning if the table entry is empty
end
end
]
#End
(You might see some similarities to FelixJim's
RunFromFile script here, so credit where credit is due as I've been using it to help learn how to get things working. I also appreciate the earlier suggestions on optimizing things and will be rolling those in once I'm past the "just get it working" phase)
Here's the debug warning I get from the ba.warning call in readfile.showcbanim() confirming that execution 'should' be working when I try to invoke this from FRED (the "Attempting to render (string)" bit - this accurately changes if I pass different line numbers from FRED, so it's definitely getting far enough to read my strings and start processing them)
---------------------------
Warning!
---------------------------
Attempting to render cb_sm1-01_d
ntdll.dll! ZwWaitForSingleObject + 21 bytes
kernel32.dll! WaitForSingleObjectEx + 67 bytes
kernel32.dll! WaitForSingleObject + 18 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! SCP_DumpStack + 354 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! Warning + 416 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! l_Base_warning_f + 72 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! luaD_precall + 785 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! luaV_execute + 5766 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! luaD_call + 161 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! f_call + 58 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! luaD_rawrunprotected + 86 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! luaD_pcall + 100 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! lua_pcall + 134 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! script_state::EvalString + 460 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! sexp_script_eval + 643 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! eval_sexp + 11499 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! eval_when_do_one_exp + 417 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! eval_when + 792 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! eval_sexp + 3659 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! mission_process_event + 547 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! mission_eval_goals + 517 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! game_simulation_frame + 1153 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! game_frame + 497 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! game_do_frame + 239 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! game_do_state + 379 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! gameseq_process_events + 237 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! game_main + 782 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! WinMain + 330 bytes
fs2_open_3_7_2_RC3-DEBUG.exe! __tmainCRTStartup + 358 bytes
[...]
[ This info is in the clipboard so you can paste it somewhere now ]
Use Yes to break into Debugger, No to continue.
and Cancel to Quit
---------------------------
Yes No Cancel
---------------------------
So.. I'm completely stumped. Is $On HUD Draw an inappropriate hook to use if you intend to call something from FRED, and if so, what should I be using instead? Why does this work fine when I call the function from inside the script, but not when I call it using script-eval in FRED?