Page 1 of 1
Popup Messages
Posted: Sun Aug 30, 2009 6:43 pm
by Fiodis
I've been trying to get a popup message to show the first time the human player spawns, and not any of the other times. To this end, I wrote a code:
Code: Select all
humanspawn = 1
OnCharacterSpawn(
function(character)
if IsCharacterHuman(character) then
if humanspawn =~ 1 then -- This is line 50.
ShowPopup("level.ARI.beginningpopup")
humanspawn = 2
end,
end,
end
)
And I stuck that inside ScriptPostLoad. I get a mungelog error:
Code: Select all
C:\BF2_ModTools\data_ARI\_BUILD\..\..\ToolsFL\Bin\luac.exe: ..\..\common\scripts\ARI\ARIc_con.lua:50: `then' expected near `='
ERROR[scriptmunge scripts\ARI\ARIc_con.lua]:Could not read input file.ERROR[scriptmunge scripts\ARI\ARIc_con.lua]:Could not read input file. [continuing]
2 Errors 0 Warnings
The problem is that "then" is already there. It looks fine to me - I think I've got the punctuations in the right places and all - but I don't code LUA very often, and there's something I've obviously missed.
Also, a forum search told me that ShowObjectiveTextPopup() is better than ShowPopup(), but for this method, do you need ScriptCB_SetGameRules("campaign")?
Re: Popup Messages
Posted: Sun Aug 30, 2009 6:47 pm
by Maveritchell
No commas.
Re: Popup Messages
Posted: Mon Aug 31, 2009 9:50 am
by Fiodis
A version without commas was one of the many others I tried. It comes up with the same error.
Re: Popup Messages
Posted: Mon Aug 31, 2009 9:51 am
by Maveritchell
~= is correct
Re: Popup Messages
Posted: Mon Aug 31, 2009 9:59 am
by Fiodis
Ah, thanks. That gets rid of the mungelog errors, but the message still doesn't show in-game, and there's nothing related to it in the error log. Should I use == instead?
Re: Popup Messages
Posted: Mon Aug 31, 2009 10:02 am
by Maveritchell
Yes you should. Right now there is no way of filling the condition "humanspawn ~= 1".
Re: Popup Messages
Posted: Mon Aug 31, 2009 10:04 am
by Frisbeetarian
What's wrong with doing it the way the campaign scripts do it?
Re: Popup Messages
Posted: Mon Aug 31, 2009 10:09 am
by Fiodis
Well, they have it set up as objectives. I'm not sure if that matters or not, but I have a conquest map right now.
I also had tried several variations of popupText(), but it didn't work.
EDIT - Right, with == it didn't work either, but I got this:
Code: Select all
Message Severity: 2
.\Source\LuaCallbacks_Mission.cpp(2264)
ShowPopup() has been depricated. Please remove all references to it in code
I knew I'd get a debug error, but I thought that it would still work.
Re: Popup Messages
Posted: Mon Aug 31, 2009 10:26 am
by Frisbeetarian
Code is code is code. It doesn't matter where it is. The most you have to worry about is including any ScriptCB_DoFile() calls that you need.
Code: Select all
onfirstspawn = OnCharacterSpawn(
function(character)
if character == 0 then
ShowPopup("level.geo1.hints.hints")
ReleaseCharacterSpawn(onfirstspawn)
onfirstspawn = nil
BeginObjectivesTimer()
ScriptCB_EnableCommandPostVO(0)
BroadcastVoiceOver("GEO_obj_18", ATT)
ScriptCB_PlayInGameMusic("rep_geo_amb_obj1_3_explore")
end
end)
From the Geonosis script. You just want the popup part? OK, take the rest out. Why would your map being conquest matter?
Re: Popup Messages
Posted: Mon Aug 31, 2009 10:44 am
by Fiodis
I edited the Geonosis code slightly, so it looks like:
Code: Select all
humanspawn = 0
onfirstspawn = OnCharacterSpawn(
function(character)
if humanspawn == 0 then
ShowPopup("level.ARI.beginningpopup")
humanspawn = 1
end
end)
Same results; error log message and no popup. This code isn't much different from my own, it just doesn't have the IsCharacterHuman bit.
Re: Popup Messages
Posted: Mon Aug 31, 2009 1:54 pm
by Frisbeetarian
Your old code was closer. Your new code doesn't even reference the function input (the whole point of using OnCharacterSpawn()).
You just don't understand what the code I posted does. You would just need the following. It's like I said, just remove the part of the code you don't need. You don't need to add anything to it.
Code: Select all
onfirstspawn = OnCharacterSpawn(
function(character)
if character == 0 then
ShowPopup("level.ARI.beginningpopup")
ReleaseCharacterSpawn(onfirstspawn)
onfirstspawn = nil
end
end)
Re: Popup Messages
Posted: Mon Aug 31, 2009 2:21 pm
by Fiodis
I tried that code, and still nothing shows up except for that error in the debug log. What does ReleaseCharacterSpawn do? And how does the line "if character = 0" work? Isn't "character" the variable of the character being spawned?
Re: Popup Messages
Posted: Mon Aug 31, 2009 3:14 pm
by Frisbeetarian
Do a manual clean, remunge, and if the same error shows up, post the Lua script, it's probably somewhere else in the code.
I think that releasing an event clears it from memory, though I'm not completely sure. In single player, the human's character index is 0. Instead of checking to see if the character is human now, you just compare ir straight away.
Re: Popup Messages
Posted: Mon Aug 31, 2009 3:17 pm
by kinetosimpetus
In one of the other scripting topics, the release event made it so it would only trigger once.
Re: Popup Messages
Posted: Mon Aug 31, 2009 3:20 pm
by Frisbeetarian
Nope, "onfirstspawn = nil" makes it so that the function does not exist and thus cannot be run again.
Re: Popup Messages
Posted: Mon Aug 31, 2009 3:23 pm
by [RDH]Zerted
Fiodis's new code is fine. The popup code is run once when the first time anyone spawn. It isn't the most efficient way to do it, but most people around here don't write efficient code.
SWBF2 passes in a value for character when it calls your callback function. That value is a character Id. Character ids are numbers, with 0 being assigned to the human player when in singleplayer. The if character == 0 then is saying only run this code when the player with the id of 0 spawns.
ReleaseCharacterSpawn 'turns off' the callback function. Your function will be called every time anyone spawns. To keep the popup from showing each time, you use the humanspawn variable. The other code doesn't have that check. Instead, it would show the popup each time the player with an id of 0 spawns. To prevent this from happening, the callback function is canceled/deleted/removed/released/turned off by using ReleaseCharacterSpawn. Use that function is slightly better as the game will no longer continue to call that callback function. (The game still will do other OnCharacterSpawn callback, but not the one that gets passed into ReleaseCharaterSpawn)
To get the popup to show, try setting the game to campaign mode. Its a function call, but I don't remember the exact name right now. Changing the mode may mess some other things up, but right now we're just trying to get the popup to work first.
Edit:
ReleaseCharacterSpawn(onfirstspawn) -- Tells the game to un-register that callback function
onfirstspawn = nil -- Just sets the variable to nil, just like any other =nil statement. This was done to clear up some Lua memory. We released the character spawn, so there is no reason to use that variable again. I don't believe onfirstspawn is a function. It holds the return value of OnCharacterSpawn(). I expect it to be some type of userdata IDing the registered callback. I've never looked into its type since there is really no point in exactly knowing it.
Re: Popup Messages
Posted: Mon Aug 31, 2009 3:34 pm
by Frisbeetarian
[RDH]Zerted wrote:To prevent this from happening, the callback function is canceled/deleted/removed/released/turned off by using ReleaseCharacterSpawn
I don't understand. If this is true, why do they set "onfirstspawn" to nil; it seems rather redundant.
[RDH]Zerted wrote:Fiodis's new code is fine.
I was saying that his code after his fourth post is better, because it checks to make sure the character is human. What Mav said should have solved the error, I just got confused because I thought he was saying the munge error was still showing up so I was checking with code I knew for sure worked.
Basically, I can't read.