Help with Simple LUA Mod

jelloWorld

Chieftain
Joined
Apr 17, 2016
Messages
3
Hi everyone,

I'm trying to get started with LUA mods but I can't seem to get a simple mod I made to work. Here is what I've done so far:

1. Used ModBuddy to create a new Project named My New Mod
2. Created a folder in the solution of My New Mod named lua
3. Created a file named "My New Mod.lua" in the lua folder of the solution
4. Changed solution>properties>content with the following addition:
Type: InGameUIAddin, Name: My New Mod.lua, Description: , File Name: lua/My New Mod.lua

5. Edited My New Mod.lua to contain the following simple code:
Code:
GameEvents.PlayerDoTurn.Add(
function(iPlayer)
   local pPlayer = Players[iPlayer];
   if (pPlayer:IsAlive() and pPlayer:IsHuman()) then
      print("Function call success for My new Mod");
      pPlayer:SetGold(3007);
      pPlayer:SetFaith(1337);
   end
end
)

6. Saved the solution and built with ModBuddy
7. Ran a new game with My New Mod enabled

However, nothing happens when the game starts and the turns progress. My intention was to make a simple mod that set my gold to 3007 and faith to 1337 every turn; in my tests I would be the only human player involved. Eventually I planned to make a mod to do a lot more than that (which involved an actual use for cycling through every player), but I decided to start small.

I've used Firetuner to test its functionality, and running the code in a button on Firetuner successfully executed the code for one turn, but not for every turn as I intended.

Thanks for reading all of this, I appreciate any help I can get.
 
If I copy your code into the "My - Changes" mod (see link in my sig) it works for me (in future it's best to attach the built mod, not give parts of it, see second link in my sig) - so at a guess you're enabling the mod on the Mod Browser screen and at some point clicking Back, so not actually starting a modded game. (See the sticky in the main C&C forum on how to enable mods and start the game - specifically steps 8 thru 12 - as it's not obvious that something that looks like a header is actually a button to click)
 
Thanks for the help. It turns out that the issue was that my mod does not work when loading up a saved game that didn't have the mod enabled to begin with. After starting a new game as your tutorial pointed out, it worked perfectly fine!

Naturally, this leads me to another question - must ALL LUA mods (or mods in general) be loaded into a fresh game, or are there mods that retain functionality when loaded into a saved game?

My guess is that the hooks associated with lua mods (i.e. Hook.Add(someLuaModFunction)) must form that association when a game is started, otherwise the lua code cannot be added to that hook. Is that correct?
 
It turns out that the issue was that my mod does not work when loading up a saved game that didn't have the mod enabled to begin with.

By default, ModBuddy builds mods with the AffectsSavedGame flag checked (set to 1). This is by far the safest approach.

If you attempt to load a saved game with a such a mod enabled that was not enabled when the game was started, the game core will silently disable the mod. Converserly, if you load a game that had such a mod enabled when it was started, but it is no longer enabled, the game core will silently enable it during the load process.

Any mod that changes the primary tables in the database MUST have AffectsSavedGame set. If the mod only changes secondary tables, it should have it set. If the mod changes the UI and or uses Lua, the modder needs to know precisely what they are changing and the impact it will have on the game.

Nothing will go wrong if the AffectsSaveGame is set and it didn't have to be. Lots of things can potentially go wrong if it's not set and it should have been. Hence the safe option is to always leave it set.

Unless you have the experience and knowledge to know that you can unset it.

W
 
I don't think it will auto-enable a needed mod anymore. It simply won't allow you to load the saved game if not all the required mods are enabled. Expansions that have been turned off will be turned back on if a mod is enabled that requires them, though, I think.
 
I don't think it will auto-enable a needed mod anymore.

Just ran a test game, and it silently re-enabled a missing "AffectsSavedGame" mod

YMMV

(But's it's definitely not behaviour you should rely on, and it' a very bad idea to do it, as depending on the load order of the mods it silently re-enables you can get some very weird effects)

W
 
Thanks for all of your help guys! Definitely won't be relying on undefined/sketchy behavior; the second series of questions was more just for understanding.
 
Back
Top Bottom