View Full Version : Default New Item in Windowlist
PneumaPilot
June 11th, 2009, 23:34
Is there a way for me to make a windowlist begin its life with a default 'new' item? I want it to exist just as if I had right clicked and 'create new' on it.
Foen
June 12th, 2009, 00:01
If it is a shared list, then this can be hard (shared window lists are painful) but if it only has one viewer at once then you might want to specify <skipempty/> and add some code to be invoked in onListRearranged:
function onListRearranged(changed)
if changed then
if #getWindows()==0 then
--[[ create a bespoke entry here ]]
end
end
end
Just to be certain, you should also call onListRearranged(true) from onInit() to force a check when the list is first shown.
Hope that helps
Stuart
PneumaPilot
June 12th, 2009, 04:50
Hmm. Interesting. I'll give it a shot and see what turns up. I'm not sure what you mean by shared windowlists, but this is an equipment list on a character sheet.
Moon Wizard
June 12th, 2009, 05:34
The charsheet is a shared window, so yes the windowlist would be considered shared. The problem with shared windowlists is that you can sometimes get a race condition in shared windowlists that cause an infinite loop situation. It's pretty rare to see, more so if you have fairly complex windowclass nesting.
In my character sheets, I just added an onClick handler for the windowlist that automatically created a window if one doesn't exist, without having to use the right-click menu.
Cheers,
JPG
Foen
June 12th, 2009, 06:16
As it is a shared list, if both the GM and the player have the character sheet open, both computers will receive the onListRearranged event and they will both try to create the new entry. At best you will end up with duplicate entries, at worse each computer will have its own version (and hence the two lists will become out of sync) or the race condition will occur, as JPG says, and you can end up having to disconnect the client and reconnect.
You can avoid this problem by forcing the new window only to be created on the client (the host will see it as soon as it is created anyway), the code becomes:
function onListRearranged(changed)
if (not User.isHost()) and changed then
if #getWindows()==0 then
--[[ create a bespoke entry here ]]
end
end
end
It could be made so that the host always creates the new entry, but then it wouldn't appear unless the host has the character sheet open - I figured the player is more likely to have the sheet open than the host.
Hope that helps
Stuart
PneumaPilot
June 12th, 2009, 14:51
What if you just put the code in the onInit function for the window? Why would it have to be in the onListRearranged? I guess you put it there in case the user deletes the last entry then it will automatically come back, but I would assume that if he's deleting things, he will know how to add it right back. I just want there to be one instance when the character sheet is first opened, so couldn't I just onInit it? Is a race condition likely to occur in this situation?
PneumaPilot
June 12th, 2009, 14:54
Oh, by the way, when I started asking this question, what I was really asking about was not where to put the code, but what to put where Foen has this line: "--[[ create a bespoke entry here ]]"
In other words, what command to I use to create a windowlist entry in the script?
Lithl
June 12th, 2009, 21:25
I've got a couple windowlists on my Exalted sheet that uses code along these lines, just to make the sheet look nice when you open it:
function onInit()
local nwindows = #self.getWindows();
local needed = 5 - nwindows;
for i = 1,needed do
self.createWindow(); --[[ This is the line you're looking for ]]
end
end
PneumaPilot
June 12th, 2009, 21:29
Aha! Perfect. Thanks.
Foen
June 13th, 2009, 08:58
What if you just put the code in the onInit function for the window? Why would it have to be in the onListRearranged? Is a race condition likely to occur in this situation?
Using onListRearranged means that the default entry is created whenever the list is empty, as you point out this can happen when the last item is deleted. If you are happy to leave an empty list, then onInit will work just fine.
As for the race condition, it could still arise, but would be far less likely. The host and client would have to open the characer sheet to that page at the same time, which would be quite rare.
If you put code in onInit for the windows *in* a list, then the race condition arises whenever a new item is added to the list by one party while being viewed by the other, as onInit is invoked both on the host and the client for the newly-created list entry.
Stuart
Powered by vBulletin® Version 4.2.1 Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.