• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Lua] Console Window at Runtime

Status
Not open for further replies.
Level 7
Joined
Dec 28, 2014
Messages
83
Hello!

I had this idea since Blizzard implemented Lua and the custom user-interface in Warcraft III. When I was testing my other projects (Perlin Noise, File IO, Network), I'm using player chat event and Lua's load function as an input, DisplayPlayerTextMessage/print for output, to debug my scripts during runtime. This method feels rigorous as you have to keep pressing the Enter key for the input and to open the Log to show the long outputs that is not visible in the in-game's display text.

As I studied Tasyen's Frame UI tutorials and used Lua's load function (along with pcall/xpcall for error handling), I manage to create a Lua console while Warcraft III is running:


war3lua-1.jpg


war3lua-2.jpg




The code for this is currently messy. The console window is still cannot be closed. I created a sandbox environment without the common.j and blizzard.j functions for this setup. You can set the environment to access stuff like CreateUnit, BlzFrameAbsPoint, etc. on runtime. The purpose of the sandbox test is to show you can create two or more different environments in the map. Like you can open Environment A by clicking a Footman and Environment B by clicking a Rifleman. The function "print" is written differently for this particular environment, it uses the "BlzFrameAddText" function to print the output to the text area. The native Lua "print" function in the global environment is not overwritten.

I'm think this kind of feature can be used for Sci-fi themed maps or make something really ridicilous like running an Operating System in Warcraft III similar to the Minecraft mods: Computercraft and Opencomputers.

What are your thoughts about this?
 
Last edited:
This is interesting. This can help mapmakers overcome the burden of having to print their debug messages in the game display itself, leaving behind more meaningful messages in the log.

I'm not sure about the possibility of running an OS in wc3, though. That might need some really "low-level" functions to be exposed, and it could be risky on Blizzard's end to expose it without considerable thought.

By the way, what exactly do you mean by "environment"? Is this pertaining to the "_ENV" upvalue, or is it something more?
 
Level 7
Joined
Dec 28, 2014
Messages
83
I'm not sure about the possibility of running an OS in wc3, though. That might need some really "low-level" functions to be exposed, and it could be risky on Blizzard's end to expose it without considerable thought.

Oh, when I'm talking about running an OS in Warcraft III, it is something like simulating or imitating those "low-level" functions in Lua virtual machine.

By the way, what exactly do you mean by "environment"? Is this pertaining to the "_ENV" upvalue, or is it something more?

Yes, the ones in the "_ENV". My example uses the fourth parameter (env) of Lua's load function to create non-global/sandbox environments.

For those who are wondering how I use Lua's load function, this is my implementation:

Lua:
console.stdin = function (code)
        local chunkName, mode = "wc3stdin", "t"
        local chunk = load(code, chunkName, mode, env)
        local args = table.pack(pcall(chunk))
        if not args[1] then
            chunk = load ("return " .. code, chunkName, mode, env)
            args = table.pack(xpcall(chunk, console.stdout))
        end
        if args[1] then
            table.remove(args, 1)
            console.stdout(table.unpack(args))
        end
end

My implementation is not the best method as it has issues of not showing nil values, it might be due to table.remove and table.unpack.
 
Level 7
Joined
Dec 28, 2014
Messages
83
You would need to encode the type nil as a string "nil" or an unique table as a key.

Thank you so much for the idea.

Also, I didn't realized table.pack actually gives out the total amount (n variable) including the nils. That made things easier.

I added this code and it is fixed.

Lua:
for i = 1, args.n do
    if args[i] == nil then
        args[i] = "nil"
    end
end


upload_2020-9-5_19-41-14.png




Since the code is a bit polished enough, here is a copy of the map I'm working on. I'm currently working of multiplayer feature of the console window.
 

Attachments

  • Console Window.w3x
    17.1 KB · Views: 64
Status
Not open for further replies.
Top