PDA

View Full Version : pausing game logic when OnItemMenu is being called



Pleng
05-13-2009, 06:00 PM
One problem gone, and another one springs straight up!

I call OnItemMenu() from an entity's OnAction() script. Essentially, if the player is over an item and presses action, he is given that item, and provided with his inventory.

Now, within the OnItemMenu() a global variable is set, g_dropedItem. It is set to set to nil when the function is called, then changed if an item is selected from the menu.

I need the entity's OnAction() script to then make a choice depending on whether an item has been selected or not, so I add the following code



if (g_dropedItem==nil) then

//he's kept hold of the cross
//let's get after him!

GetEntityByName("Pirate"):RunFunction("AddPatrolGoals");

end


But it appears that the condition is being evaluated before the player has made his selection from the menu (I have confirmed this by placing a logmsg between the OnItemMenu("_first_"); option and the if (g_dropedItem==nil) condition, and other logmsgs in the menu script, the logmsg before the conditional is shown before the logmsgs within the OnItemMenu function.

Is there any way of 'pausing' the function until an item has been selected from the menu?

Honestly, after this little issue it should all be plain sailing!

:D

Seth
05-14-2009, 01:23 AM
Well, two things - one is, if you are using global variables, you might want to use _G.g_droppedItem, the _G makes sure if you set it (and it hasn't been created yet) it will get made in the global namespace and not the local scripts.


About checking stuff after a menu happens.. maybe in the OnAction(), right before you start the dialog thingie, do:

Schedule(100, this:GetID(), "this:PostOnAction();");
(dialog starts next)

then in that script, add:


function PostOnAction()
//check for what items are dropped and continue the game logic here
end

The schedule should hit it after the choice because the game timer is paused while dialog is active.

A script function can never pause/stop in the middle, it can only exit and use scedule()'s to run functions later. ChoiceDialog objects allow you to set a function to call back when they finish so that's another method. (Like how talking to the barmaid works in the NPC example)

That item stuff I did for the npc example is 100% script so it's possible to modify it too, to say, have it call another user definable function when it's done or something.

Pleng
05-14-2009, 06:35 AM
Schedule(100, this:GetID(), "this:PostOnAction();");
(dialog starts next)



Hmm. There's a little problem with this, because the delete flag is set in the function, prior to the scheduling, so I get the error message:



Ignoring Schedule() command for EntID 790 (), it's marked for deletion. ("this:OnActionChoice();" would never have been delivered)


I have tried moving the relates functions to the entity which is going to 'pick up' the actions by altering the code as follows:



Schedule(10, GetEntityByName("Pirate"):GetID(), "GetEntityByName(\"Pirate\"):GetID():RunFunction(\"OnCrossActionChoice\")");


But am getting the follow LogError message:



[string "GetEntityByName("Pirate"):GetID():RunFuncti..."]:1: attempt to index a number value


Any ideas what I am doing wrong?

Pleng
05-14-2009, 06:40 AM
Nevermind I had an extra GetID()

in the staement:




Schedule(10, GetEntityByName("Pirate"):GetID(), "GetEntityByName(\"Pirate\"):GetID():RunFunction(\"OnCrossActionChoice\")");


it should have been



Schedule(10, GetEntityByName("Pirate"):GetID(), "GetEntityByName(\"Pirate\"):RunFunction(\"OnCrossActionChoice\")");