CP Capture message problem

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Post Reply
ARCTroopaNate
Jedi
Jedi
Posts: 1160
Joined: Mon Mar 21, 2011 8:12 pm
Projects :: Star Wars Battlefront - Tides of War
Games I'm Playing :: SWBF2 RC EAW
xbox live or psn: I have ps4
Location: STALKER!
Contact:

CP Capture message problem

Post by ARCTroopaNate »

http://www.gametoast.com/viewtopic.php?p=140204#p140204

I've used this to add CP capture/lost messages for all the stock maps, but I run into a problem if more than once CP is getting neutralized at a time.

Code: Select all

OnBeginNeutralize(
   function(cPost)
      pTeam = GetCommandPostTeam(cPost)
end
)

OnFinishNeutralize(
   function(cPost)
      local pName = GetEntityName(cPost)      -- Gets the name of the post that was lost     
      
      if (pTeam == 1) then 
         ShowMessageText("level.dea1." .. pName .. "_all_lost")      -- Show this string to all teams
      else
         ShowMessageText("level.dea1." .. pName .. "_imp_lost")      -- Show this string to all teams 
        end
    end
) 
Let's say rebels begin neutralizing CP 4, then the variable pTeam == 1. Let's say the Empire begins neutralizing CP 2 before the rebels finish neutralizing CP 4, then pTeam value changes to 2. Now the rebels finish neutralizing CP 4, but the message displays as "Rebels lost blah blah post" instead of reading Empire lost blah blah post.

Is there any way to fix this??

Thank you.
User avatar
Sporadia
Private
Posts: 40
Joined: Thu Jan 24, 2019 11:02 pm
Projects :: No Mod project currently
Games I'm Playing :: None
xbox live or psn: No gamertag set

Re: CP Capture message problem

Post by Sporadia »

Have you tried making pTeam an array so each cp's team is stored as a separate value? e.g.
Hidden/Spoiler:
[code]
pTeam = {} -- Define array

OnBeginNeutralize(
function(cPost)
pTeam[cPost] = GetCommandPostTeam(cPost) -- Store team for specific command post in the array
end
)

OnFinishNeutralize(
function(cPost)
local pName = GetEntityName(cPost) -- Gets the name of the post that was lost

if (pTeam[cPost] == 1) then -- Check team for specific command post in the array
ShowMessageText("level.dea1." .. pName .. "_all_lost") -- Show this string to all teams
else
ShowMessageText("level.dea1." .. pName .. "_imp_lost") -- Show this string to all teams
end
end
)
[/code]
If that doesn't work, you can set up the array more neatly e.g.
Hidden/Spoiler:
[code]
pTeam = {} -- Define array
-- Fill array with 0s for addresses 1-6
for i = 1, 6 do
pTeam = 0
end

OnBeginNeutralize(
function(cPost)
pTeam[cPost] = GetCommandPostTeam(cPost) -- Store team for specific command post in the array
end
)

OnFinishNeutralize(
function(cPost)
local pName = GetEntityName(cPost) -- Gets the name of the post that was lost

if (pTeam[cPost] == 1) then -- Check team for specific command post in the array
ShowMessageText("level.dea1." .. pName .. "_all_lost") -- Show this string to all teams
else
ShowMessageText("level.dea1." .. pName .. "_imp_lost") -- Show this string to all teams
end
end
)
[/code]


In both these examples I've assumed cPost is a number, if that's wrong it may need some tweaking.
Edit: Corrected a syntax error (accidental comma in 2nd example)
ARCTroopaNate
Jedi
Jedi
Posts: 1160
Joined: Mon Mar 21, 2011 8:12 pm
Projects :: Star Wars Battlefront - Tides of War
Games I'm Playing :: SWBF2 RC EAW
xbox live or psn: I have ps4
Location: STALKER!
Contact:

Re: CP Capture message problem

Post by ARCTroopaNate »

It didn't work. I'm not sure if cPost is a number (i.e. 6) or (CP6)
User avatar
Sporadia
Private
Posts: 40
Joined: Thu Jan 24, 2019 11:02 pm
Projects :: No Mod project currently
Games I'm Playing :: None
xbox live or psn: No gamertag set

Re: CP Capture message problem

Post by Sporadia »

You might be right. The first thing you should do is add print("Checking the format of cPost: "..cPost) into OnBeginNeutralise. Then you can see if cPost is a number or a string in the log. Assuming cPost does give you a name (eg CP6) then I've updated the code below. I've put this together using techniques that I've got working in user scripts of my own before. But I haven't actually tested this exact code so I'm hoping there aren't any mistakes (and it all fits together fine). The overall idea of putting together a CP index/matrix and making functions to use it should work regardless, even if my suggested code gets something wrong. Hope this helps.
Hidden/Spoiler:
[code]
CPTotal = 10 -- this should be bigger than the number of command posts on the map

-- Create an index/matrix for storing command post names and their teams
pTeam = {} -- Define matrix
for i = 1, CPTotal do
pTeam = {}
for j = 1, 2 do -- CP names in row 1, teams in row 2
pTeam[j] = 0
end
end

-- Making a new function UpdateCPIndex
-- To add and update command posts in the index
-- Argument postname is the name of the command post eg CP6
-- Argument postteam is the team value for that command post
if UpdateCPIndex then -- Error check
print("Warning: UpdateCPIndex already exists as a function")
else
function UpdateCPIndex(postname, postteam)
-- Update the team of a command post already in the matrix
-- Look for the command post's name in the index first
local j = 0 -- don't confuse this j with the one above
for i = 1, CPTotal do
if (pTeam[1] == postname) then
pTeam[2] = postteam -- found CP in index so update team
j = 1
end
end
if (j == 0) then -- This j is 0 when CP hasn't been found in index already
-- Add command post name to the index
local k = 0
for i = 1, CPTotal do
if (k == 0) then
-- Find first available slot
if (pTeam[1] == 0) then -- look for a 0 in the name column
-- Add CP info to the index
pTeam[1] = postname
pTeam[2] = postteam
k = 1 -- stops the rest of the index from being filled accidentally
end
end
end
-- Error check
if (k == 0) then
print("Warning: Unable to add command post to index, increase CPTotal")
return nil
end
end
end
end

-- Making a new function ReadCPIndex
-- To find teams in the index using the command post name
-- This function only has one argument, postname
-- It returns the team number for that CP
-- If the function can't find a command post in the index, it returns nil
if ReadCPIndex then -- Error check
print("Warning: ReadCPIndex already exists as a function")
else
function ReadCPIndex(postname)
local postteam = nil
-- Look for the command post's name in the index
for i = 1, CPTotal do
if (pTeam[1] == postname) then
postteam = pTeam[2]
end
end
return postteam
end
end

OnBeginNeutralize(
function(cPost)
print("Checking the format of cPost: "..cPost) -- Check this in the log to find out if cPost is a number
UpdateCPIndex(cPost, GetCommandPostTeam(cPost)) -- Store team for specific command post in the array
end
)

OnFinishNeutralize(
function(cPost)
local pName = GetEntityName(cPost) -- Gets the name of the post that was lost

if (ReadCPIndex(cPost) == 1) then -- Check team for specific command post in the array
ShowMessageText("level.dea1." .. pName .. "_all_lost") -- Show this string to all teams
else
ShowMessageText("level.dea1." .. pName .. "_imp_lost") -- Show this string to all teams
end
end
)
[/code]
ARCTroopaNate
Jedi
Jedi
Posts: 1160
Joined: Mon Mar 21, 2011 8:12 pm
Projects :: Star Wars Battlefront - Tides of War
Games I'm Playing :: SWBF2 RC EAW
xbox live or psn: I have ps4
Location: STALKER!
Contact:

Re: CP Capture message problem

Post by ARCTroopaNate »

It does return a name (cp6). I tried the code and didn’t work, how would I go about testing to see whqt the issue is? I’m not very familiar with coding for battlefront
User avatar
Sporadia
Private
Posts: 40
Joined: Thu Jan 24, 2019 11:02 pm
Projects :: No Mod project currently
Games I'm Playing :: None
xbox live or psn: No gamertag set

Re: CP Capture message problem

Post by Sporadia »

Sorry my suggestion hasn't worked. The best way to debug code is to shove the print() command in smart places. With print, you put text inside of "", eg print("this is text") and it will show up in the log when you test a map. To read values of things then put them outside of the "" eg print("What is cPost" cPost) will show you something like What is cPost cp6. The .. is a way of removing some of the spacing but it only works for numbers or strings, if cPost contained userdata for example it would cause a crash with print("text "..cPost).

So what I'd suggest doing is putting things like print("UpdateCPIndex called successfully") under function UpdateCPIndex(postname, posteam) to check in the log every time the function UpdateCPIndex runs, and then throwing print commands everywhere where a number is changing or being used to check they're all doing what they're supposed to be. That kind of thing. And then you can remove them all later when you don't need them. As for specifically what code I posted, setting up a matrix in lua is easy to look up online. Making functions is something I've picked up from looking at Zerted's user script docs that come with the patch and playing around with them. There are examples of looking up values in matrices and using indexes in some of the mission scripts for campaign levels. Like look at how they spawn Greivous in the Utapau level. And so I would have thought that putting a matrix together for all the cPost names to be stored next to their team values should be step 1. Learning how to make a function is step 2. Figuring out how to get those functions to fill in and read from the matrix correctly is step three (this uses a lot of for commands which is probably where the mistake is). And that as a method ought to work. I suspect I've just made a slight mistake somewhere, but I can't help you there because I don't have any way of testing this stuff as I was writing it.

Edit: If you don't use .. but you're testing 2 numbers/strings/userdata, you need to use a comma. Eg
print("text" num) will work but have a big space between the text and the number
print("text "..num) will remove the spacing (num must be a number or a string or .. will crash)
print("text" num "text") will work
print("text "..num.." text") will work (num must be a number or a string or .. will crash)

print(num1 num2) will crash
print(num1, num2) will work but have a big space between the 2 numbers
print(num1.." "..num2) will put a single space between the 2 numbers (num must be a number or a string or .. will crash)
print(num1 "text" num2) will crash
print(num1, "text", num2) will work but have big spaces
Last edited by Sporadia on Thu Jun 27, 2019 4:32 pm, edited 2 times in total.
Marth8880
Resistance Leader
Posts: 5006
Joined: Tue Feb 09, 2010 8:43 pm
Projects :: EVERYWHERE + Project Infiltrator
Games I'm Playing :: Silent Hill 2
xbox live or psn: Marth8880
Location: Edinburgh, UK
Contact:

Re: CP Capture message problem

Post by Marth8880 »

Why do you need to get pTeam in OnBeginNeutralize? Why not just get the CP's team in OnFinishNeutralize?

If the execution order is off and the team is always 0 in OnFinishNeutralize, you can make pTeam into a string-indexed table. It would look something like this:

Code: Select all

local pTeams = {}

OnBeginNeutralize(
	function(cPost)
		pTeams[GetEntityName(cPost)] = GetCommandPostTeam(cPost)
	end
)

OnFinishNeutralize(
	function(cPost)
		local pName = GetEntityName(cPost)
		print("OnFinishNeutralize: "..pName.." (team "..pTeams[pName]..")")
		
		-- Which team lost the CP? (Need to account for other teams too!)
		if (pTeams[pName] == ALL) then 
			ShowMessageText("level.dea1." .. pName .. "_all_lost")      -- Show this string to all teams
		elseif (pTeams[pName] == IMP) then
			ShowMessageText("level.dea1." .. pName .. "_imp_lost")      -- Show this string to all teams 
		end
		
		-- Cleanup
		pTeams[pName] = 0
	end
)
User avatar
Sporadia
Private
Posts: 40
Joined: Thu Jan 24, 2019 11:02 pm
Projects :: No Mod project currently
Games I'm Playing :: None
xbox live or psn: No gamertag set

Re: CP Capture message problem

Post by Sporadia »

Yeah Marth's suggestion is more elegant than what I was trying. When you have if pTeams[cPost] == 1 do causing a problem because cPost is a string not a number then there are 2 approaches. One is to just go "oh ok, replace the 1 with ALL." The other is to forget strings can index tables in lua and try to put a matrix together that you have to continuously search through. I actually tried modding kamino today to test this problem and got it working with a matrix too if the string indexed table method doesn't work. Btw pTeams[cPost] would work just as well as pTeams[GetEntityName(cPost)] and pTeams[pName] if you want to simplify it even more.


Edit: Oh wait no, I'm definitely wrong here. Because ALL is just the same as 1.

Edit edit: The problem isn't that pTeam is 0 on OnFinishNeutralize, the problem was that pTeam would be overwritten when two command posts were being neutralized at the same time, so the first CP was taking the team of the 2nd CP. Still solved with a string indexed table if string indexed tables work. But if pTeam[cPost] didn't work in my first suggestion, and print(cPost) was giving a string like CP6 then why does pTeam[GetEntityName(cPost)] work any better? cPost and GetEntityName(cPost) are both strings aren't they? Am ready with a solution to this based on matrices but I don't want to post it if I'm missing something and Marth's way works. Because my way is almost the same as before which is like 3 times as big as Marth's and I'll probably explain it step by step this time so you know how to debug it if you have to.
Last edited by Sporadia on Thu Jun 27, 2019 4:02 pm, edited 1 time in total.
User avatar
Sporadia
Private
Posts: 40
Joined: Thu Jan 24, 2019 11:02 pm
Projects :: No Mod project currently
Games I'm Playing :: None
xbox live or psn: No gamertag set

Re: CP Capture message problem

Post by Sporadia »

I've tested Marth's code with print() in OnBeginNeutralize and OnFinishNeutralize to check everything that was happening (because I haven't set up ShowMessageText). Completely worked. I then did print(cPost) and print(GetEntityName(cPost)) in OnBeginNeutralize. Both printed the same thing on Kamino (I think GetEntityName(cPost) might give you the in game names on maps which have them, if that line does anything at all. But for Kamino I saw cp1, cp2 etc). Then I went and tested the first response I posted where I was using pTeam[cPost] instead of pTeam[GetEntityName(cPost)] and pTeam[(pName)]. Totally worked for me too. No idea why it didn't when you tried it (maybe there's a typo, I typed it out again). I would take Marth's idea of using elseif pTeam[cPost] == 2 instead of else in the OnFinishNeutralize though; sometimes maps have natives and extra teams. The 2nd thing I posted (matrix method) is completely unnecessary, string indexed arrays do work.


Edit: This is the exact code I put at the end of ScriptPostLoad() on Kamino. I have checked the log to make sure it directs to the right ShowMessage every time since they just appear as [NULL] to me. So I can confirm this is working code for me.

Hidden/Spoiler:
[code]
pTeam = {} -- Define array

OnBeginNeutralize(
function(cPost)
pTeam[cPost] = GetCommandPostTeam(cPost) -- Store team for specific command post in the array
print("Team "..GetCommandPostTeam(cPost).." are losing a command post: "..GetEntityName(cPost))
end
)

OnFinishNeutralize(
function(cPost)
local pName = GetEntityName(cPost) -- Gets the name of the post that was lost
if (pTeam[cPost] == 1) then -- Check team for specific command post in the array
print("Team 1 have lost a command post: "..pName)
ShowMessageText("level.dea1." .. pName .. "_all_lost") -- Show this string to all teams
elseif pTeam[cPost] == 2 then
print("Team 2 have lost a command post: "..pName)
ShowMessageText("level.dea1." .. pName .. "_imp_lost") -- Show this string to all teams
end
end
)
[/code]

Edit edit: Have you been using pTeam anywhere else in your code? Because I've been assuming that you haven't but that might break the line pTeam = {}, in which case you should try local pTeam = {} (also in Marth's version) or rename pTeam to something else for this bit.
Post Reply