Question
Is it possible to run a script directly from a text in the assets like running a script called from the asset page which would give the possibility of modding to a game. I dont know if im clear for example: runScript("assetdestination")
Is it possible to run a script directly from a text in the assets like running a script called from the asset page which would give the possibility of modding to a game. I dont know if im clear for example: runScript("assetdestination")
Something you in fact CAN do, is run javascript from text. Because microstudio is likely recognizing the code file as a normal text file, you can use asset_manager.loadText( path, callback )
in your code. The documentation provides following examples:
asset_manager.loadText("somefolder/mytextfile", function(text)
mytext = text
end)
loader = asset_manager.loadText("somefolder/mytextfile")
if loader.ready then
mytext = loader.text
end
You can then use system.javascript( javascript_code )
to utilize the actual code.
However adding the glue between the game and the code of the "mod" is something I lack knowledge of. Maybe you can find it out yourself or somebody else is able to help properly.
is it possible to add javascript code like said confusedcatgirl
.
I also wanted to add dynamic code in MicroScript and this is what I managed to discover.
The code is dynamically (probably) parsed into bytecode at the time of startup (the part that is currently to be executed) The Virtual Machine in which the code is executed has several tables that have reference to
ops
, opcodes
, arg1
, ref
, table
, label_count
, labels
, transpile
, import_refs
, import_values
, import_self
, locals_size
.
There is also information about the original function code, location in the project (file and line number - probably for the debugger), original function code.
So once a program is running, it is difficult to modify it.
Maybe it would be possible to reach the parser through the "window" object and add the code - but I don't know how to do it.
But if you want to add something via a plugin, you can use the API. Then you add a plugin, it adds code to the project.
I see that a class cannot be added dynamically, but a function can be added.
Even though MicroStudio shows that there is no Test() function, it calls it correctly.
The second run of the code returns no errors.
init = function()
string ="
global.DynamicClass = class
printString = function( s )
print( s )
end
end
Test = function()
print('Test function')
end
registerDynamicClass = function()
global.DynamicClass = DynamicClass
end
"
status = system.project.writeFile( "source/generated/src1", string, object replace = "true" end,
function(result, error) print("New file " + result)
d = new DynamicClass()
registerDynamicClass()
end )
print( "status = " + status.ready )
while not status.ready end
print( "status = " + status.ready )
dc = new DynamicClass()
dc.printString( system.time() )
end
update = function()
dc.printString( system.time() )
Test()
end
draw = function()
end