timsk 10 Posted October 11, 2013 Hi guys I'm trying to get some code to run whenever the mission is loaded. The init.sqf is only run when the game is created not when it is loaded (As in, you press the continue button when making the game). I tried using: _id = addMissionEventHandler ["loaded",{hint "MISSION EVENT HANDLER!"}]; inside my init.sqf but the code is never executed (I never see the hint). I've tested this in editor and in a multiplayer game. Is this even the right thing to use for what I want to do? If it is, any idea why it's not working? Share this post Link to post Share on other sites
cuel 25 Posted October 11, 2013 Might be too early to show a hint, try diag_log. What are you even trying to do? If you're just trying to get some code to run you might want to look into using description.ext to precompile code when the mission is loaded Generally, you'd use waitUntil {time > 0} to run code after mission is "started". Share this post Link to post Share on other sites
timsk 10 Posted October 12, 2013 I'm trying to load in arbitrary data about players when the game is started from a save. I already have a saving system in place, so now I'm trying to load the values. My issue is that init.sqf and description.ext only seem to be run when the game is started for the first time, I'm trying to find a way of running code when the game is continued from a save. I tried switching to diag_log but no luck, it's not in the rpt file at all. Share this post Link to post Share on other sites
timsk 10 Posted October 14, 2013 Just bumping this. It's easy enough to do on a dedicated server (OnPlayerConnected), but on a normal server, I really can't find a way to run a script when a game is hosted from a save. Savegame works alright, but I have custom variables I want to save and load. Share this post Link to post Share on other sites
tortuosit 486 Posted April 27, 2017 I created https://feedback.bistudio.com/T124597 Share this post Link to post Share on other sites
killzone_kid 1332 Posted April 27, 2017 57 minutes ago, tortuosit said: I created https://feedback.bistudio.com/T124597 Replied to you and updated the documentation: https://community.bistudio.com/wiki/Arma_3:_Event_Handlers/addMissionEventHandler#Loaded Share this post Link to post Share on other sites
tortuosit 486 Posted April 27, 2017 That's great. I'm not at my rig, what would be the quickest and easiest way? I wrote some functions the simple way... IIRC, I put the function definitions at early stages of a mod, e.g. in an init file. Before it is used. Like: tort_sum = {(_this select 0) + (_this select 1)}; ...and later call it e.g.: hint str ([10,20] call tort_sum); But that preinit flag, where would that be... I think it is more complicated. EDIT: I guess (mod environment) I have to define "class cfgFunctions" in config.cpp, is that correct? Share this post Link to post Share on other sites
tortuosit 486 Posted April 27, 2017 It's confusing to me. It's complicated, I don't like those init/cfgthings in ArmA... :-D In config.cpp, I created: class CfgFunctions { class tort_functions { class preInitFunctions { class tort_playmusic_loadPreinit { file = "\@tort_playmusic\script\preinit.sqf"; preInit = 1; }; }; }; }; Not sure about all that class naming. Content of \@tort_playmusic\script\preinit.sqf: tort_playmusic_loadPreinit = { tort_playmusic_loadedEH = addMissionEventHandler [Loaded, {' systemchat "Blah"; if !isnil tort_music_ehStopID then {removeMusicEventHandler [MusicStop, tort_music_ehStopID]}; playmusic ""; tort_playmusic = [tort_music_num_tracks, random tort_music_wait_current, tort_music_wait_seconds, false, tort_music_debug] execvm "\@tort_playMusic\script\play.sqf"; '}]; }; [] call tort_playmusic_loadPreinit; I.e., there is a function definition tort_playmusic_loadPreinit which is adding a mission event handler "tort_playmusic_loadedEH" and doing other stuff. Then the function is called. After savegame load, the systemchat is not happening. What's wrong? BTW, the handle tort_playmusic_loadedEH is -1 at any time. At least it is defined... Share this post Link to post Share on other sites
killzone_kid 1332 Posted April 27, 2017 29 minutes ago, tortuosit said: addMissionEventHandler [Loaded What your .rpt says? I bet there undefined variable error somewhere Share this post Link to post Share on other sites
killzone_kid 1332 Posted April 27, 2017 31 minutes ago, tortuosit said: if !isnil tort_music_ehStopID Really? Is tort_music_ehStopID a code? Is tort_music_ehStopID a string? Share this post Link to post Share on other sites
pierremgi 4905 Posted April 27, 2017 addMissionEventHandler ["Loaded",... not addMissionEventHandler [Loaded,... Share this post Link to post Share on other sites
tortuosit 486 Posted April 27, 2017 36 minutes ago, pierremgi said: addMissionEventHandler ["Loaded",... not addMissionEventHandler [Loaded,... Indeed. Guys, for some reason my code was totally broken. ()""\ got lost on copy/paste and I only partially repaired it. WTF. Must be something wrong between my pspad/notepad.... tort_music_ehStopID is a playmusic handle. OK, so I hope the concept is alright because nobody disagreed; will check again tomorrow. Share this post Link to post Share on other sites
pierremgi 4905 Posted April 27, 2017 4 minutes ago, tortuosit said: Indeed. Guys, for some reason my code was totally broken. ()""\ got lost on copy/paste. Must be something wrong between my pspad/notepad.... tort_music_ehStopID is a playmusic handle. OK, so I hope the concept is alright because nobody disagreed; will check again tomorrow. hum, wait, for me, playMusic command doesn't return anything (no handle). But I guess it's more an addMissionEventHandler handle, so a number. Share this post Link to post Share on other sites
tortuosit 486 Posted April 27, 2017 Yes it is a handle, onmusicstop. But please, that is irrelevant. Let's narrow it down. The question is, will \@tort_playmusic\script\preinit.sqf fire up the "Loaded" EH if it was like this. Sorry for not using code tags, it's a fucked up editor here, forum software hates me. tort_playmusic_loadPreinit = { tort_playmusic_loadedEH = addMissionEventHandler ["Loaded", {'systemchat "Blah";'}]; }; [] call tort_playmusic_loadPreinit; Share this post Link to post Share on other sites
pierremgi 4905 Posted April 27, 2017 On my mind config.cpp and preinit function are working through an addon, before the mission starts. Inside a mission scope, I'm not sure you can do anything with that but I didn't read something about that. Not the best answer, sorry. Hold fast. Share this post Link to post Share on other sites
tortuosit 486 Posted April 28, 2017 class CfgFunctions { class tort_functions { class preInitFunctions { class tort_playmusic_loadPreinit { file = "\@tort_playmusic\script\preinit.sqf"; preInit = 1; }; }; }; }; \@tort_playmusic\script\preinit.sqf does not seem to be called. No errors in rpt. Nothing happens after savegame load. Will take a break and retry in a few days, cannot afford the tinkering (with numerous ArmA restarts) right now. tort_playmusic_loadPreinit = { tort_playmusic_loadedEH = addMissionEventHandler ["Loaded", {'systemchat "Blah";'}]; }; [] call tort_playmusic_loadPreinit; Share this post Link to post Share on other sites
tortuosit 486 Posted April 28, 2017 OK guys, it's that function registering thing. Which I loved to avoid, because it makes me brainsick. I managed to have tort_fnc_doPreinit appear in the functions viewer: https://steamuserimages-a.akamaihd.net/ugc/824566203557777717/511671AAA16F4FC00B216F99959EF8FB3554ED5A/ I am calling it from init.sqf, not sure if that is necessary. --> [] call tort_fnc_doPreinit; The result: "Loaded" eventhandler tort_playmusic_loadedEH has the value "6" after mission start. But it does not run the event code. I randomize a global variables value in the event code, it does not happen, variable remains unchanged. But I am a step closer. KKs Wiki entry "NOTE: "Loaded" event handler should be added BEFORE the mission is loaded from save. Placing it in a function with preInit = 1; usually does the trick." -it sounds so easy. Share this post Link to post Share on other sites
killzone_kid 1332 Posted April 28, 2017 54 minutes ago, tortuosit said: tort_playmusic_loadedEH has the value "6" after mission start should be 0 Share this post Link to post Share on other sites
killzone_kid 1332 Posted April 28, 2017 Also from the screen shot you have your code wrapped in single quotes??? addmissioneventhandler ["Loaded", {'...code....'}]; I think I am going to withdraw myself from any further interaction about this before I say something I would regret later. Good luck with your mod. Share this post Link to post Share on other sites
tortuosit 486 Posted April 28, 2017 It's working now. The single quotes came from a copy/paste job. They are necessary in a radio menu (0-8) definition. I'm sorry. Share this post Link to post Share on other sites
Midnighters 152 Posted April 28, 2017 Holy shit, cut some slack. Interesting observation. Share this post Link to post Share on other sites
tortuosit 486 Posted April 28, 2017 For the completeness, this is how it worked. Don't know if it's state of the art or perfect. Sorry I made a mistake with the spoiler tags and cannot fix it here, the forum software and me are not friends. a) I added to addon.pbo\config.cpp: class CfgFunctions { class yourtag { class preInitFunctions { class doPreinit { file = "\@tort_playmusic\script\preinit.sqf"; preInit = 1; recompile = 1; }; }; }; }; With this you'd get a function "yourtag_fnc_doPreinit" - you can find it in function viewer. b) Added a file "\@tort_playmusic\script\preinit.sqf"; outside of pbo (I want code as open as possible). Contents: tort_playmusic_loadedEH = addMissionEventHandler ["Loaded", { // code here; // moar code; }]; EDIT: c) does not seem to be necessary c) Called the function in init.sqf via: [] call yourtag_fnc_doPreinit; Share this post Link to post Share on other sites
pierremgi 4905 Posted April 28, 2017 5 hours ago, tortuosit said: The result: "Loaded" eventhandler tort_playmusic_loadedEH has the value "6" after mission start. 4 hours ago, killzone_kid said: should be 0 Is there this remaining problem? If I'm right (precautious term to say it's an open discussion), that means you run 7 times an EH "loaded" before the mission start. So, there is probably an unwanted loop... or do you need all these "loaded" EHs? Share this post Link to post Share on other sites
tortuosit 486 Posted April 29, 2017 13 hours ago, pierremgi said: Is there this remaining problem? If I'm right (precautious term to say it's an open discussion), that means you run 7 times an EH "loaded" before the mission start. So, there is probably an unwanted loop... or do you need all these "loaded" EHs? Does it mean it ran 7 times? However, now with above code, after mission start the handle is 1. I added a test counter to preinit.sqf which also shows, the code runs once, as expected. I've also put a counter into the event code - it counts the # of saves correctly. Works as intended. I am currently in the process of registering some of my functions to the functions viewer, l am understanding it better and better. It's nice. Will need to take a closer look about when functions are compiled. Well, the normal learning process... Share this post Link to post Share on other sites