Jump to content

Is there any dirty hacky way I can read a variable in my HOOKS.lua from my MISSION.lua?


TEMPEST.114

Recommended Posts

as title says.

I need access to certain 'gui' events but these only fire inside the HOOKS.lua environment (sigh.)

So is there any way I can have a HOOKS script that stores a boolean and then either send that to my MISSION script, or preferably, get my mission script to 'see' that boolean on demand from inside the HOOKS script?

Link to comment
Share on other sites

I *think* I was able to do this by executing setUserFlag in the server space. It requires that your scripts (in mission and hooks) share knowledge about the flag name to query to exchange the info between GUI and mission. Something like this (in GUI script):

local status,error  = net.dostring_in('server', " return trigger.action.setUserFlag(\"" .. flagName .. "\", " .. numValue .. "); ")

Warning: I haven't tried this for a long time.


Edited by cfrag
  • Thanks 1
Link to comment
Share on other sites

5 hours ago, cfrag said:

I *think* I was able to do this by executing setUserFlag in the server space. It requires that your scripts (in mission and hooks) share knowledge about the flag name to query to exchange the info between GUI and mission. Something like this (in GUI script):

local status,error  = net.dostring_in('server', " return trigger.action.setUserFlag(\"" .. flagName .. "\", " .. numValue .. "); ")

Warning: I haven't tried this for a long time.

 

Hang on.

So what you're saying is there has to be a flag named in the ME.

Then the hooks.lua sends that dostring message to turn the flag on.

when that flag is on in the mission editor there is another trigger to make a call in the missionScript.lua?

Or am I missing something? 

Ideally, I'd like a boolean to be set in the hooks.lua.

Then a way for the hooks.lua  to 'send' the current state of that boolean TO my missionScript.lua

 

Link to comment
Share on other sites

 

5 hours ago, cfrag said:

I *think* I was able to do this by executing setUserFlag in the server space. It requires that your scripts (in mission and hooks) share knowledge about the flag name to query to exchange the info between GUI and mission. Something like this (in GUI script):

local status,error  = net.dostring_in('server', " return trigger.action.setUserFlag(\"" .. flagName .. "\", " .. numValue .. "); ")

Warning: I haven't tried this for a long time.

 

Also, the 'server' lua env. This isn't a server script per se.

Would 'mission' work?


Edited by Elphaba
Link to comment
Share on other sites

32 minutes ago, Elphaba said:

So what you're saying is there has to be a flag named in the ME.

Pretty much - you need to decide on the name of a flag (or flags) that you want to use to transfer the state between GUI and mission states. Let's say we call that flag "coolOption".

Now, should teh GUI script detect that the user does something that warrants setting that flag, the hooks script sets the flag via dostring_in() to some value (say 1)

On the miz side, you'll have to poll that flag, i.e. repeatedly (once a second perhaps, depending on how time-critical this is) and when it changes value, you know that a state change was signalled from the GUI script. How you interpret that value that the GUI script sets is up to you, but obviously, more than 0/1 are supported 🙂

 

Link to comment
Share on other sites

  • 1 year later...

hey guys, I'm only just diving into server hooks here, so please bear with me as I struggle here. As I understand it, I should be able to create a Scripts/Hooks/set_server_id.lua file, which has these contents:

local status = net.dostring_in("server", "return trigger.action.setUserFlag('server_id', 99); ")
log.write("[ZYLL SCRIPTING]", log.WARNING, "started")

and then in a mission trigger, I just wait 3 seconds and then try to output the value of the server_id flag:

env.info("server_id:" .. trigger.misc.getUserFlag("server_id"))

It shouldn't be any easier than this, but the server_id flag value is always 0, implying that the mission is not able to see the flag that was set in the hook script. The hook script is running with no errors, as evidenced by the log message being written successfully.

I've tried different variations of escaping the string "server_id" but no matter what I do, I don't see the flag from the mission. Can you please enlighten me?

thanks!

Link to comment
Share on other sites

Posted (edited)
1 hour ago, Zyll said:
local status = net.dostring_in("server", "return trigger.action.setUserFlag('server_id', 99); ")

Hey.

Remember, this line is working in the 'server' environment. Environments are a key concept in Lua that can keep grouped things together, but keep disparate things apart: https://www.lua.org/pil/14.html

The 'server' environment is completely separate from the 'mission' environment that DCS Scripting Engine and the Mission Editor reside in. So they can't talk to each other unless there is a constructed bridge between them.

You need to specifically either GET something from the other environment from within a different environment and you can only do that if you have some environment or table in common. 

This is the problem. The hackiest solution is your own TCP server sending out the 'server' stuff, and a 'mission' TCP client reading, but networking is a PITA. 

 

 


Edited by TEMPEST.114
  • Like 1
Link to comment
Share on other sites

Posted (edited)
2 hours ago, Zyll said:

I should be able to create a Scripts/Hooks/set_server_id.lua file, which has these contents:

local status = net.dostring_in("server", "return trigger.action.setUserFlag('server_id', 99); ")
log.write("[ZYLL SCRIPTING]", log.WARNING, "started")

and then in a mission trigger, I just wait 3 seconds and then try to output the value of the server_id flag:

env.info("server_id:" .. trigger.misc.getUserFlag("server_id"))

Yes. There are a couple of caveats for this to work, though:

  • after you drop your server script into Hooks, you MUST FULLY restart DCS, not just the server. Onerous, annoying, time-consuming but absolutely critical. The Hooks folder is read once, when DCS starts up.
  • the mission that attempts to read the flag must be run as MULTIPLAYER. Otherwise the user flag space isn't shared with the 'server' scope

So, you may want to try this:

  • fully restart your computer (better safe than sorry)
  • start DCS 
  • run the test mission as local multiplayer host

Now the client should return 99 as value for the flag server_id - provided that it was set up correctly. You need to show your code when you invoke setting the flag server_id.

BUT: Remember that when a mission is started up, the entire 'server' flag space is cleared (is that documented somewhere? hell no. This is DCS - it's assumed that this comes to you naturally after you hit yourself over the head a sufficient number of times). So you must ensure that the flag 'server_id' is set every time and after the mission initializes (i.e. you need to trap OnSimulationStart() on the server and re-set the flag server_id every time that happens. This may explain why @TEMPEST.114 abandoned this quest - too few hits over the head 🙂 


Edited by cfrag
  • Thanks 2
Link to comment
Share on other sites

holy crap it actually worked. Thank you guys. This is my server script: /Scripts/Hooks/set_server_id.lua:

local ssi = {}

function ssi.onSimulationStart()
	local status = net.dostring_in("server", "return trigger.action.setUserFlag('server_id', 99); ")
	log.write("[set_server_id]", log.INFO, "server_id flag set")
end

DCS.setUserCallbacks(ssi)

this is what's being run inside the miz to validate it sees the flag:

env.info("server_id:" .. trigger.misc.getUserFlag("server_id"))

this now opens the door to fancier stuff (or maybe not, because 100 DCS restarts later, I'm kinda done testing this thing now, lol)

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...