Jump to content

Recommended Posts

Posted (edited)
On 1/5/2023 at 10:53 PM, Elphaba said:

So they're in the same 'protected sandbox' that my regular script is running in? So I can address a function in the HOOK.lua file directly?

That rather depends. There are multiple Lua environments (sandboxes) running simultaneously. There's your mission environment, which is created and destroyed with every mission. The scripts that are placed in /Hooks usually run in a separate Lua environment called "GUI" (IIRC, might be "server") which starts with DCS. Each environment is walled of, and cross-environment access is severely restricted. Your mission-based script can't easily invoke a method that resides in GUI space. There is a net.doString_in() method to execute a named method in another environment and return the result, but the current implementation appears to work only for setUserFlag() and getUserFlag().

So the scripts in your "mission" environment (which incarnates and dies with the mission) can only talk to your "GUI" (Hooks) environment (that incarnates and dies with the DCS application) via net.doString_in(), and currently only by setting flags. 

Edited by cfrag
Posted
9 minutes ago, cfrag said:

That rather depends. There are multiple Lua environments (sandboxes) running simultaneously. There's your mission environment, which is created and destroyed with every mission. The scripts that are placed in /Hooks usually run in a separate Lua environment called "GUI" (IIRC, might be "server") which starts with DCS. Each environment is walled of, and cross-environment access is severely restricted. Your mission-based script can't easily invoke a method that resides in GUI space. There is a net.doString_in() method to execute a named method in another environment and return the result, but the current implementation appears to work only for setUserFlag() and getUserFlag().

So the scripts in your "mission" environment (which incarnates and dies with the mission) can only talk to your "GUI" (Hooks) environment (that incarnates and dies with the DCS application) via net.doString_in(), and currently only by setting flags. 

 

Do_string works for anything. There is a missconception that this is for flags only. Dont believe everything they say

Posted
Just now, mirq said:

Do_string works for anything

Thank you for that correction. In the past I was unable to invoke net.loadMission() in "GUI" or "server" space via do_string(), and was under the impression that this was related to the (assumed) restrictions placed on do_string(). I'll try and find out if that has changed. If there are no restrictions on do_string(), that would open up quite a few new avenues for me to experiment with, thank you!

Posted

 

9 minutes ago, mirq said:

I will send you the exact code that does that

Thank you, that is much appreciated. It resolves the one thing that a mission should, but can't do: restart itself. I was unable to write a script that simply gets it's own name, and then invokes that loadMission() on the server to restart itself, no matter what the mission is called. It should be simple, and I failed embarrassingly.

 

Posted (edited)
1 hour ago, cfrag said:

 

Thank you, that is much appreciated. It resolves the one thing that a mission should, but can't do: restart itself. I was unable to write a script that simply gets it's own name, and then invokes that loadMission() on the server to restart itself, no matter what the mission is called. It should be simple, and I failed embarrassingly.

 

Restart itself is much simpler. I can even do a restart with all players slot preserved. I will send the code for restart. As regards preserved slots it is complicated and it involves much logic. You can do it if study the order in which callbacks work in hooks.

 

But why the hack would you invoke do_string for restarting mission? Hooks can officialy load a mission or load next mission by net.load_mission(). You dont need mission env for that. I will send the code.

 

Edited by mirq
Posted (edited)

The code for restart:

 

local Timer = 0
local PERIOD = 36000 -- seconds
local PERIOD_MIN = PERIOD -120
 
 
 
 
function restart.onSimulationFrame()
    Timer  = DCS.getModelTime()
    if (Timer < PERIOD_MIN )
    then
        return
    else
        if (Timer < PERIOD )
        then
                return
        else
            Timer=0
            net.load_next_mission() -- you need to set your sever for "loop" -ing missions, and then you dont botter with lfs. If you have only one mission
            -- or
            --net.load_mission(lfs.writeDir() .. 'Scripts\\Hooks\\Missions\\' .. 'mission_name.miz')
           
        end
     
    end
end

 

As regards separation between hooks and msision, this is so well made that even if you block the GUI by a wrong function call in hooks,  lets say this results in an infinite loop and framerate in GUI drops to 0, still in mision env those who are spawned and are playing already, they wont feel the drop of framerate in GUI at all, until they move to specs or another slot. They will freeze then.

 

So the framerate drop that occurs in net.onSiulationFrame is absolutely negligible, it doesnt even exist. You can write all the logic for restarting in that callback

 

 

Edited by mirq
Posted (edited)

Thank you for that script!

I feel I need to apologies for being obtuse: To solve the restart issue I wrote a /hooks script that can get triggered by a mission (i.e. from the mission context) when it sets a flag (here a flag called 'simpleMissionRestart') which is essentially this:

smr = {}
smr.restartFlag = "simpleMissionRestart"
function smr.getServerFlagValue(theFlag)
	local val, errNo  = net.dostring_in('server', " return trigger.misc.getUserFlag(\""..theFlag.."\"); ")
	if (not val) and errNo then
		return 0
	else
		return tonumber(val)
	end
end

function smr.restartMission()
	local mn = DCS.getMissionFilename( )
	local success = net.load_mission(mn)
	if not success then 
		net.log("+++smr: FAILED to load <" .. mn .. ">")
	end
end

-- main update loop, checked once per second
local lTime = DCS.getModelTime()
function smr.onSimulationFrame()
	if lTime + 1 < DCS.getModelTime() then
		lTime = DCS.getModelTime()
		if not DCS.isServer() then return end 
		if smr.getServerFlagValue(smr.restartFlag) > 0 then 
			smr.restartMission()
		end
	end 
end

-- install smr in hooks
DCS.setUserCallbacks(smr)

In a running mission, change the flag 'simpleMissionRestart' to any positive value, and the mission restarts itself. 

But for this to work, the mission requires a counterpart in /Hooks installed - meaning that it can't function on a DCS install without that /hooks counterpart. My holy grail was to create a mission-space script that can call into GUI or server space and do the restart of the same mission without requiring a counterpart in /hooks.

I thought that 

net.doString_in("GUI", str) with str being  "mn = DCS.getMissionFilename( ); success = net.load_mission(mn); return success"

should do the trick (i.e. re-start the mission, whatever it's name). It did not. I always need a counterpart in /hooks for this to function; I explained that to me with some restrictions placed on net.doString_in(). 

Do you have a tip on how I can get a mission to re-start itself without relying on a /hooks file? Or some indication of what I'm doing wrong with doString_in? It seems that there's some property of net.doString_in() that I'm not fully comprehending.

Edited by cfrag
Posted (edited)

I understand, so you need to restart the mission when a certain flag is triggered in mission. In my case i only start it periodically., dont care about flags.

 

For that, you need that dostring return a value form mission . Theoretically it returns a string. You are trying to cast that string to a number. From my experiments I couldnt figure it out what dostring returns. Better log the return value and see what it is. Then decide if that's a number or a text.

Your code should work if dostring behaves.

Oh, by the way, shouldnt you write 

 net.dostring_in('mission', ...

 

instead of net.dostring_in('server', ... ? 

flags are defined in mission, maybe 'server' is ok, but lot of things are happening on clients part and reported back to server. Dunno, just asking.

 

P.S. I checked docs, and there is no option for 'server', how did that land in your code ?

 

 

Why did put all functions in smr table ? Get them out of there. They are not callbacks.

Leave only on/simulationFrame(), and make all others local or global fucntions and variables

 

The following I dont understand at all:

net.doString_in("GUI", str) with str being  "mn = DCS.getMissionFilename( ); success = net.load_mission(mn); return success"

net.dostring_in is a function that runs only in GUI already. in net. space more precisely. This can be write only in a hook file, not miision file.

If you write it in a hook file than it makes no sense. load_mission can be run directly without dostring.

so there is no place for dostring_in in any mission file.

 

you want to restart the mission from mission logic, not GUI. Ok, then you need to send info from misison to HOOKS. dostring can't do that, excepting the case it returns a value when run from HOOKS, but you need to write "mission" there not "server".

Other than that, I know a method to  make run code in HOOKS, directly from misiion. Any time, anything.

You can check the flag in misison and send information to HOOKS to a function that will restart the mission. 

 

 

 

 

 

 

Edited by mirq
Posted
17 minutes ago, mirq said:

For that, you need that dostring return a value form mission . Theoretically it returns a string. You are trying to cast that string to a number. From my experiments I couldnt figure it out what dostring returns. Better log the return value and see what it is. Then decide if that's a number or a text.

I'll try again asap, but since I'm not interested in doString's return value (it should restart the mission), I don't really have a problem with either type (I'm talking about invoking net.doString_in() from the mission into "GUI").

19 minutes ago, mirq said:

instead of net.dostring_in('server', ... ? 

well, it works. It's probably a leftover from older scripts I've written based on coding god @Ciribob's source (SSB, others). Unfortunately, a lot of the stuff we do in DCS comes from undocumented places...

22 minutes ago, mirq said:

Why did put all functions in smr table ? Get them out of there. They are not callbacks.

smr is the code that I run from GUI (i.e. /Hooks dir) so that the mission restarts when a certain flag changes to a positive value from inside the mission context. That way a mission can control (as opposed the code in /Hooks) when the mission should restart. Using a /Hooks-based script was the only way I was able to do that - because (to my great confusion) invoking net.doString_in() did not do what I thought it would do when calling out of 'mission' context into 'GUI' context.

27 minutes ago, mirq said:

Leave only on/simulationFrame(), and make all others local or global fucntions and variables

Wouldn't that require a script file in /Hooks? I'm trying to get a mission to restart itself without requiring a script in /Hooks.

Let me try to put it differently: when I'm invoking the following from within the mission context (as a trigger DOSCRIPT)

local res = net.dostring_in("GUI", "mn = DCS.getMissionFilename( ); success = net.load_mission(mn); return success")

it will not restart the mission. Yet, the same command sequence (getMissionFileName(); load_mission() ) does (as the smr script proves) from "GUI" space. And I'm too dense to figure out why. My only explanation was that net.dostring_in() is firewalled when invoked from within mission context. Changing "GUI" to "server" does not make a difference. And net.load_mission() isn't implemented in "mission" space, so I can't go that route. 

Any hint on where I screwed up could really make my week-end 🙂 

 

Posted (edited)

You are saying that net.dostring_in() is working from mission env ? Doesn't it raise an error ? like net. is nil ? I am curious, never tried.

I am trying now

Edited by mirq
Posted
Just now, mirq said:

You are saying that net.dostring_in() is working from mission env ?

Excellent question. My docs say that

Quote

The net singleton are a number of functions from the network API that work in the mission scripting environment. Notably for mission scripting purposes there is now a way to send chat, check if players are in Combined Arms slots, kick people from the server, and move players to certain slots.

and dostring_in() is among the methods defined. My results show different, hence my original (unproven) assertion that net.dostring_in() is nerfed in 'mission' context.

Posted (edited)

I am testing now. 1 minute

 

So, it doesnt raise any erro, but does nothing. Absolutely nothing. Like it even does not exist in code. I checked this one: 

 

 net.dostring_in("GUI", "net.send_chat('asdad', true)")

Maybe "GUI" is not good there, let me check with "server" and others

Edited by mirq
Posted (edited)

As it was not strange enough, i tried the following command:

  bullsmth.dostring_in("mission", "net.send_chat('asdad', true)")
 
 
It reports that bullsmth is nil value
 
So net. is not nil in mission. that's cool
Edited by mirq
Posted (edited)

Ok,

All good, net.dostringin works in mission, but "GUI" is reported to be "invalid state name". We should find the correct state name to access GUI.

Conclusion :

there are only 3 state names that are allowed in dogsting_in:  misison, export  and config. But server works too.

if we run with "mission" or "server" then net. is reported as nil value. The net. from inside the string . there is no net. in mission's net. space. Clear ? there is Group or Unit in mission but no net. Pretty strange.

if we using "GUI" or anything else, the state is reported as "invalid state name"

anyway, it is cool that dostring works in mission, you may run code from mission functions like get units, groups etc

 

 

Edited by mirq
Posted (edited)

OMG, Cfrag

 

The solution is fckin trivial. All this <profanity> for nothing

It is the fourth way I discover to send commands from mission to hooks. It is stupid simple. You can restart anytime from mission. 

 

Wanna know ? Are you here ?

Edited by mirq
Posted (edited)

'gui' should be lower-case, but it's incorrect that this is the lua_State for Hooks, it's not, but I bet you can't guess 😛

Edited by PravusJSB

Creator & Developer of XSAF ::An AI model that wants to kill you, and needs no help from humans.

Discord: PravusJSB#9484   twitch.tv/pravusjsb  https://www.patreon.com/XSAF  https://discord.gg/pC9EBe8vWU https://bmc.link/johnsbeaslu

Work with me on Fiverr: https://www.fiverr.com/pravusjsb

  • Recently Browsing   0 members

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