Loading...
  OR  Zero-K Name:    Password:   

Need some LUA Widget scripting help

35 posts, 1908 views
Post comment
Filter:    Player:  
Page of 2 (35 records)
sort


5 years ago
So, I would like to try and make a LUA script, that would toggle the fire mode of Newton as following, depending on target:
1.If the target is Scuttle, Puppy, Imp, Snitch or Limpet it would always try to push the target.
2. If the unit's mass is equal or less than 200, the Newton will switch between pull and push mode every second.
3.If the unit's mass is over 200, the Newton will always try to push the target.


However, I have no idea how to do that! I was able to find LUA coding Tutorial, pretty basic stuff, but I have no idea where I can find LUA commands that are specific to Zero-K.

Basically my problems are following:
1.How do I specify that the Widget will affect specific unit, Newton in this case.
2.How do code different behaviour based on the target enemy type (Scuttle, Puppy, Imp, Snitch or Limpet).
3.How I can access target unit weight stat.
4.How I can switch the toggle between push and pull every X second/frame.


If someone could link me a source for this info or have direct answer to my problems, I would appreciate it.
+2 / -0
You could join XCOM and get that widget to learn from as a bonus as it's already written :)

Then we'd recruit you to participate with us to write other cool widgets. I've got a long list of cool ideas that wait to see daylight.

Here's all you need:
https:/springrts.com/wiki/Lua:Main

Well I lied a bit. You also should download and review the code. Writing your first widget is something I'd be happy to help you with like on a weekend as it's something I've recently gone through as well and want to do more of it.

Learn from examples: https://github.com/ZeroK-RTS/Zero-K/tree/master/LuaUI/Widgets
+3 / -0
Being more specific about your points:

1. To get anything to do whatever you want you need to get to run your code at some point. For that you need to use some callin. In your case it probably would be game state to run every frame - probably does not need to run that often so check if the number_of_frame modulo 5 = 0 to do it every 5th frame.

To find any newton at all you'd want to use unit created and unit destroyed callins and check if the unit that was created was a newton - then save it on some list of units that your main loop updates.

2. When game gets to run your code you can for example look for enemy units in some radius. Synced commands are reserved for Gadgets (widgets with more privileges) - but many of them work on widget side with filtered/altered results. Like you will get only the units you can detect. Unsynced reads can be found here.
3. With that call of get units you get list of unit ids that can be used. With that ID you can ask anything about the unit as long as it is in line of sight (Radar dots have different unit IDs I guess, not sure.) - including it's name and weight.
4. You probably also want to control the unit also known as sending commands to a unit. I believe the code for toggling pull and push would be "ONOFF 85" command.
+3 / -0


5 years ago
Thank you, I think I am able to figure out now how to do it.
+0 / -0


5 years ago
...I might have said that too early, as matter of fact. I still need some help.

I have problem utilising the GiveOrderToUnit(unitID, CMD.ONOFF, CMDTYPE.ICON_MODE)
The problem is the CMDTYPE.ICON_MODE, which is supposed to be "expect 1 parameter in return (number selected mode)" and I have no idea what I need to place there in order to switch between pull and push.

Once again, all help is appreciated.
+0 / -0

5 years ago
I was confused about giving commands part as well. I don't have it figured out completely so if someone more experienced could tune in and shine some light would be cool.
+0 / -0
Tried it out of curiosity. This is my first experience with Lua (the makers specifically ask not to write "LUA", BTW!). This post doesn't answer the question, but maybe it will serve to help someone avoid my mistakes when someone who knows how to do this provides a solution.

I managed to find CMD_PUSH_PULL in LuaRules/Configs/customcmds.h.lua, but it's not there in the working game. Using the integer directly doesn't seem to do anything, but the reason is probably that I can't figure out what to give as parameters (I wanted to make the turret switch to "Pull" the moment it was built). The command at the time I gave up on it looked like this:
Spring.GiveOrderToUnit(unitID, 35666, {3}, 0)


35666 is CMD_PUSH_PULL, 3 is just a guess: I thought that 1 is probably "default", 2 is "Push", 3 is "Pull". This is most likely nowhere close to what it really is though. I don't know where to look up the parameter(s) for this command, if the command is even at all exposed to widgets.
+2 / -0

5 years ago
You need
Spring.GiveOrderToUnit(unitID, CMD_PUSH_PULL, {1}, 0) to push
Spring.GiveOrderToUnit(unitID, CMD_PUSH_PULL, {0}, 0) to pull
+2 / -0


5 years ago
RUrankivand I tried to use that GiveOrderToUnit -command, but it doesn't seem to work.

So the "Weapon Impulse" code can be found from https://github.com/ZeroK-RTS/Zero-K/blob/master/LuaRules/Gadgets/weapon_impulse.lua

Will probably hold some clues to this problem, but I haven't had any luck so far. I tried replacing the
Spring.GiveOrderToUnit(unitID, CMD_PUSH_PULL, {1}, 0) with Spring.GiveOrderToUnit(unitID, CMD_PUSH_PULL, {'Push'}, 0), but that didn't work either.
+0 / -0
Here's working code snippet:

quote:

local function TurnOn(newtonid)
local states = Spring.GetUnitStates(newtonid)
if states["active"] == false then
Spring.GiveOrderToUnit(newtonid,35666,{1},0)
end
end

local function TurnOff(newtonid)
local states = Spring.GetUnitStates(newtonid)
if states["active"] == true then
Spring.Echo("Set state to active")
--Spring.GiveOrderToUnit(newtonid,CMD.ONOFF,{0},0) -- does not work
Spring.GiveOrderToUnit(newtonid,35666,{0},0)
end
end


Indents were optimised out by forum.
+1 / -0
quote:
Indents were optimised out by forum.


local function TurnOn(newtonid)
    local states = Spring.GetUnitStates(newtonid)
    if states["active"] == false then
        Spring.GiveOrderToUnit(newtonid,35666,{1},0)
    end
end

local function TurnOff(newtonid)
    local states = Spring.GetUnitStates(newtonid)
    if states["active"] == true then
        Spring.Echo("Set state to active")
        --Spring.GiveOrderToUnit(newtonid,CMD.ONOFF,{0},0) -- does not work
        Spring.GiveOrderToUnit(newtonid,35666,{0},0)
    end
end


FTFY


quote:
        --Spring.GiveOrderToUnit(newtonid,CMD.ONOFF,{0},0) -- does not work

This probably requires an include for the CMD table to be present.
+0 / -0

5 years ago
quote:
RUrankivand I tried to use that GiveOrderToUnit -command, but it doesn't seem to work.


This is hardly possible. The excerpt of code I provided is a part of the production widget, that does automatic control of the impulse guns.

P.S. Actually if you google thoroughly enough, you should be able to find a working widget, that does what you probably want.
+0 / -0

5 years ago
Might need to find what CMD_PUSH_PULL is localized as in the widget. It's not a Spring global.
+1 / -0

5 years ago
--Spring.GiveOrderToUnit(newtonid,CMD.ONOFF,{0},0) -- does not work
that's a comment that ONOFF didn't work for me
+0 / -0
5 years ago
Thanks to all who chimed in. I can confirm that
Spring.GiveOrderToUnit(unitID, CMD_PUSH_PULL, {0}, 0)
works to switch from push to pull when you define it:
local CMD_PUSH_PULL = 35666


I'm not happy about just using the integer like that though. If it ever changes in customcmds.h.lua, the widget would stop working, or worse, would start doing something it's not supposed to be doing.

Strange, I thought I tried 0 and 1 before going to 2 and 3... There's still this small matter of figuring out where to look that up for other commands. And I mean other than by looking at widgets that use them.
+1 / -0

5 years ago
VFS.Include("LuaRules/Configs/customcmds.h.lua")
+2 / -0
Virtual file system... so that's where it's been hiding!

OK, here's a little working example widget that I made. Should be helpful if someone still can't figure out how to put all of the above together. Criticism welcome.

[Spoiler]
+0 / -0
Hi there. Welcome to the lua wizardry club.




local Pull = 0
...
GiveOrderToUnit (unitID, CMD_PUSH_PULL, {Pull}, 0)

quote:

local Pull = {0}

GiveOrderToUnit(unitID,CMD_PUSH_PULL, Pull,0)


Recreating a table every time you call this is insanity. It's very expensive to create a table. This is an optimization though. Basically good points:

* Using speedups is both good for your personal timesaving AND good for performance! Speedups are ~47-58% faster iirc.
* Try to keep tables around you're going to be reusing.




quote:

if Spring.GetSpectatingState() then
widgetHandler:RemoveWidget()
end


Move this to Initialize() and PlayerChanged(playerID) [?]. It doesn't belong in UnitFinished. Just don't need this check happening every time a unit is finished.
+2 / -0
5 years ago
Thank you, @_Shaman, for the welcome and the review. I have made the changes in post with the spoiler.

My apologies to FIrankterve886, as I never meant to hijack this thread, as I apparently have done.
+0 / -0
You ain't hijacking anything, it's about LUA widget scripting help.

All people with interest of programming and widgets are especially welcome in XCOM which can stand for eXploit COMbat with our glorious leaders @_Shaman and FIranksprang

We fight with exploits not using them though :P At least that's my idea for it. I have lots of ideas to try out but I lack company and motivation a bit for that. I hope that with some cooperation it would be more engaging to test things out. For now I haven't been able to do that with Shaman for few reasons, however we're here to help each other when one calls for it (be it testing or expertise tips).

Lately I've been motivating myself to read through documentation on the wiki. I've wanted to create few automation scripts or custom smart AI for units like puppy or flea.

The easy and useful script I wanted to code now is pseudo D-gun for kodachi which would be shortcut to set target on the edge of range in the direction of closest enemy. With kodachi changes players seem to want to do that often but it requires a lot of micro - that could be automated and later released as game improvement if others find it useful. Taking away micro intensive tasks is I believe in the spirit of this game.
+2 / -0
Page of 2 (35 records)