Struggling with Lua script.

Ddogclaw

Chieftain
Joined
Apr 10, 2015
Messages
4
I've been trying to create a lua script to manage a civilization's UA and I can't figure out what I've done wrong. I figure it has to be something wrong with how I'm hooking into the game because nothing is printing to firetuner at all.

Code:
function checkHappiness(iPlayer)
  print("Running happiness check");
  local pPlayer = Players[iPlayer];
  local pCiv = pPlayer.GetCivilizationType();

  civilianUnits = {GameInfoTypes["UNIT_WORKER"], GameInfoTypes["UNIT_SETTLER"]}
  greatUnits = {GameInfoTypes["UNIT_ARTIST"], GameInfoTypes["UNIT_SCIENTIST"], GameInfoTypes["UNIT_MERCHANT"], GameInfoTypes["UNIT_ENGINEER"], GameInfoTypes["UNIT_GREAT_GENERAL"], GameInfoTypes["UNIT_GREAT_ADMIRAL"], GameInfoTypes["UNIT_MUSICIAN"], GameInfoTypes["UNIT_WRITER"]};

  legendarySpawn = false;
  greatSpawn = false;
  goodSpawn = false;
  mehSpawn = false;

  local netHappiness = pPlayer:GetHappiness() - pPlayer:GetUnhappiness();

  if (pCiv == GameInfoTypes.CIVILIZATION_ESPON) then
    print("Espon happiness is: " .. netHappiness);
    if (netHappiness >= 40) then
      legendarySpawn = true;
    elseif (netHappiness >= 20 and netHappiness < 40) then
      greatSpawn = true;
    elseif (netHappiness >= 10 and netHappiness < 20) then
      goodSpawn = true;
    else if (netHappiness > 0 and netHappiness < 10) then
      mehSpawn = true;
    end
  end
  if (legendarySpawn or greatSpawn or goodSpawn or mehSpawn) then
    spawnUnit(pPlayer)
  end
end

GameEvents.PlayerDoTurn.Add(checkHappiness);

I omitted the rest of the script because I can't think of how it could have broken anything. It's also possible that the problem is with how I added the script to the content tab of modbuddy, because I had problems with that in making a different script.
 
pPlayer.GetCivilizationType();

needs to be: pPlayer:GetCivilizationType();

See here

Even though when you create variable pPlayer you are really creating a table with all of the player's info within it, you cannot use the dot ( . ) method, you need to use the colon ( : ) method. This is true of Player:Something(), Unit:Something(), City:Something(), etc., and even though it flies in the face of the way some of the documentation on API methods is presented here or elsewhere on the net in the lists of things you can do with Player, Unit, City, etc. IE, many times these lists are shown in the format you were trying to use.

Do not, however, confuse the foregoing as having anything to with lua hook events. These do use a dot (.) seperator format, so:
Code:
GameEvents.PlayerDoTurn.Add(checkHappiness);
is correct.

The reason you are probably getting nothing printed in the logs is that the lua file is not getting loaded all the way into the game. If you look carefully enough you should be seeing an error message in the lua.log or in the Live Tuner log telling you have a format error in your code and which line number it is occuring at.
 
It's also possible that the problem is with how I added the script to the content tab of modbuddy, because I had problems with that in making a different script.

Which is one of the reasons it's a good idea to attach the mod (second link in my sig) as we can quickly look in the .modinfo file and check things like that (see third link in my sig for what ModBuddy settings a Lua file needs)
 
Back
Top Bottom