Page 1 of 1

fighters health based on armor tech (former LUA help thread)

Posted: Wed May 24, 2017 7:00 am
by siyoa
thanks to sven's help, I think I got it working, here is the mod if anyone is interested
function f_health_add adds 10, 15, 20 health to the base health of fighters depending on armor tech known

if any issues then please let me know

https://www.dropbox.com/sh/n1pa89bqr4qd ... BqIoa?dl=0
____________________________

I am trying to mod the health of fighters / bombers per armor technology
I spent last two evenings flipping through PiL book, but only thing I figured so far is that I don't like Lua that much :lol:

here is my code in WEAPONS.lua

Code: Select all

function foo(f)
  local health_add = 0
  local a = (w?.object?.empire or gui_player()):known 'Duranium Construction'
  local b = (w?.object?.empire or gui_player()):known 'Adamantium Construction'
  local c = (w?.object?.empire or gui_player()):known 'Neutronium Construction'
  if a then health_add = 10 end
  if b then health_add = 15 end
  if c then health_add = 20 end
  return health_add
end


also I changed fighter / bomber health to health = 20+foo(w)

now, what is happening

every time I run the game I get "trying to index nil value" for each local a, local b, local c inside foo()
if I comment those 3 lines out before game starts, and I revert back (remove comments) once game loaded my save game, everything works fine and as intended

my head hurts :x , any help appreciated

Re: whoever understand LUA please respond

Posted: Wed May 24, 2017 7:51 am
by mharmless
If I follow you correctly the error happens before you actually get into a game, like at the main menu? It seems like there wouldn't be an empire object or possibly even the player gui at that point, so three times it tries to look at an object that doesn't exist.

I'm not familiar with lua syntax or functions, but I assume there is some way to check that an object exists before asking it for specific values; perhaps block the tech checking off in a conditional based on checking for those two objects' existence and return your zero if they don't exist?

Re: whoever understand LUA please respond

Posted: Wed May 24, 2017 3:41 pm
by siyoa
You are pretty much spot on, one exception. Game doesn't even get to main menu, it complains right after it starts and terminates with error message. Maybe I found a solution, will check when I have a chance.

Re: whoever understand LUA please respond

Posted: Thu May 25, 2017 4:12 am
by sven
siyoa wrote:You are pretty much spot on, one exception. Game doesn't even get to main menu, it complains right after it starts and terminates with error message. Maybe I found a solution, will check when I have a chance.


Yes, the problem, basically, is that the weapon definition blocks in WEAPONS.lua are executed before the game starts. So if you define one of the variables in those blocks in terms of the current empire / current player, you'll get an error.

The deeper problem is that right now, fighters aren't designed to have their health vary with technology; whatever value is defined in WEAPONS.lua is a fixed number that gets referenced in launch_fighter.lua when the fighters are created.

So if you want to make fighter health vary depending on the tech level of the ship that's launching the fighter, what you need to do is hook into launch_fighter.lua, not WEAPONS.lua. Look for the code that sets the health of the newly created fighter, and hook your foo()+ call into that*. An approach of that sort should work, I believe. It does look to me like you've figured out most everything else correctly -- you've just hit a wall due to an execution context misunderstanding ;)

* specifically, you probably want to mod line 69, and change value of health_per passed into TrackingObject.create.

Re: whoever understand LUA please respond

Posted: Thu May 25, 2017 6:18 am
by siyoa
ah, that did the trick :D thank you

I was poking around that file yesterday, but it somehow didn't click in my brain :oops:

now my only problem is that the code I added to report health of fighter / bomber in design screen shows basic health instead of one adjusted per armor technology, I guess it is not a big trade off to get this mod finally working ;)

fighter_health.jpg
fighter_health.jpg (44.39 KiB) Viewed 14992 times


I wanted to attach modded launch_fighter.lua if anyone wants to use it, but it says "Invalid file extension: launch_fighter.lua" :?:

Re: whoever understand LUA please respond

Posted: Thu May 25, 2017 6:42 am
by sven
siyoa wrote:now my only problem is that the code I added to report health of fighter / bomber in design screen shows basic health instead of one adjusted per armor technology, I guess it is not a big trade off to get this mod finally working ;)


Well, if you make your health-modding function a global

Code: Select all

function _G.get_fighter_health_bonus(weapon)
...


then you should be able to call it from anywhere in the code base: including the place where you added the line to report health per fighter in the designer info screens.

(Defining a lot of global functions is considered bad practice, and there are cleaner ways to make this happen, but they're also a bit more complex. A global with a suitably descriptive name is the route I'd recommend for a relative Lua novice.)

Re: whoever understand LUA please respond

Posted: Fri May 26, 2017 5:03 am
by siyoa
thanks, yeah going global would be the way to go, I don't need that info on design screen that much, it was there mainly to make sure my function altering fighter health is doing something :D

I'll resort to global functions for some more serious stuff ;)