1. We have added the ability to collapse/expand forum categories and widgets on forum home.
    Dismiss Notice
  2. All Civ avatars are brought back and available for selection in the Avatar Gallery! There are 945 avatars total.
    Dismiss Notice
  3. To make the site more secure, we have installed SSL certificates and enabled HTTPS for both the main site and forums.
    Dismiss Notice
  4. Civ6 is released! Order now! (Amazon US | Amazon UK | Amazon CA | Amazon DE | Amazon FR)
    Dismiss Notice
  5. Dismiss Notice
  6. Forum account upgrades are available for ad-free browsing.
    Dismiss Notice

Script to get number of Miasma tiles in a city

Discussion in 'CivBE - Modding Help & Mod Creation' started by Starrynite120, Apr 18, 2016.

  1. Starrynite120

    Starrynite120 Chieftain

    Joined:
    Jul 15, 2015
    Messages:
    472
    I'm trying to write a script for a wonder that gives you culture for every miasma tile you have in a city. This is what I've got.

    Code:
    
    function XenoDrome(playerID, cityID)
    	local Player = Players[playerID];
    	local Perk = GameInfo.PlayerPerks["PLAYERPERK_WONDER_STARRYNITE_XENODROME"].ID;
    	local City = player:GetCityByID(cityID)
    	local Wonder = GameInfo.Buildings["BUILDING_XENODROME"].ID;
    	local bonus = 0
    	
    	if Player:HasPerk(Perk) then
    		for City in Player:Cities() do
    			if City:IsHasBuilding(Wonder) then
    				for Plot = 0,City:GetNumCityPlots(),1 do
    					SpecificPlot = City:GetCityIndexPlot(Plot);
    					if SpecificPlot:HasMiasma() then 
    						print('Miasma Found')
    						bonus = bonus + 1;
    					end
    				end
    			end
    		end
    	end
    
    	if bonus > 0 then
    		Player:ChangeCulture(bonus);
    
    		-- Send a notification to the player
    		local text = Locale.ConvertTextKey("TXT_KEY_STARRYNITE_XENODROME_NOTIFICATION", tostring(bonus));
    		Player:AddNotification(NotificationTypes.NOTIFICATION_GENERIC, text, text);
    	end
    end
    GameEvents.PlayerDoTurn.Add(XenoDrome)
    
    
    
    I am getting an error on the lines that define the local variables Player and Perk, saying that they are a nil value, but I'm not sure why as I've defined them similarly elsewhere. Why am I getting this error?
     
  2. Machiavelli24

    Machiavelli24 Mod creator

    Joined:
    May 9, 2012
    Messages:
    818
    Code:
    function XenoDrome(playerID, cityID)
    ...
    	local Player = Players[playerID];
    	local City = player:GetCityByID(cityID)
    ...
    GameEvents.PlayerDoTurn.Add(XenoDrome)
    
    Your player object is in capital-p Player but then you use lowercase p player. In general you want to use lowercase letters for your local variables. Function names start with uppercase.

    In addition PlayerDoTurn provides one argument: playerID. It does not provide the second cityID argument. Functions that you add to events should only have arguments that match what the event provides them.
     
  3. Starrynite120

    Starrynite120 Chieftain

    Joined:
    Jul 15, 2015
    Messages:
    472
    Yeah I caught the error with the lowercase p after I posted this. So would playerdoturn not providing the cityID Ake my function not work? If so, how would I get the city that has the wonder in it?
     
  4. Machiavelli24

    Machiavelli24 Mod creator

    Joined:
    May 9, 2012
    Messages:
    818
    Well the way you are currently doing it is looping over all the player's cities and testing each one to see if it has the wonder. Which is a fine approach. A slightly faster but more complex method would be to save the plot of the city with the wonder in a global variable that you populate on game start or buildingProcessed. That way you're just grabbing the city from cache each turn instead of looping for it.
     
  5. Starrynite120

    Starrynite120 Chieftain

    Joined:
    Jul 15, 2015
    Messages:
    472
    Ok great. So I didn't get any errors just now, but its giving me incorrect yields. One test it yielded 7 culture every turn with only 2 miasma in its borders, and the second time it yielded 2 with no miasma in borders. Any idea what's wrong with my calculation? I've never written a script that references map tiles before, so I'm not really sure what I did wrong.

    Edit: Actually, after some more testing, it is write sometimes. Other times though its just off and seems to be counting the number of non-miasma tiles. For example, my starting city with 7 tiles produced 7 culture when it had no miasma, but when I added miasma it increased to 8. Another times my starting city had 1 miasma tile and granted 6 culture, which increased to 7 when I added miasma. I am using IGE to test this though, so I may be producing a weird situation that would not exist in normal gameplay.

    Edit Again: I think I've nailed down the problem. For some reason when I put the Wonder in my capital before it grew any tiles it screwed up the calculation, but when I went in and grew the borders THEN placed the wonder everything worked as planned. A bit odd, but since the weird situation will never be encountered in a real game it can be ignored I think.
     

Share This Page

Ebates: Get Paid to Shop