Quick simgroup loading question
by Dave · in Torque Game Engine Advanced · 03/09/2006 (7:47 am) · 9 replies
Hi,
I'm putting a number of simobjects in a simgroup and saving it using the simgroup.save functionality. When I reload the simgroup I just exec the saved file. That recreates the simgroup with the name I saved it as. All fine up to here.
I have another simgroup that belongs to my player and I want to get the simobjects from the reloaded group into my player's group. To do this I just run through a loop adding each object I find in the reloaded group to my player's group. Not sure if this is the way to do it, but I can't find any examples of how to load and allocate from a simgroup.
Anyway, the copy only gets half way through (that is, it copies 5 objects out of 10) and then stops copying with no errors. The rest of the script in the .cs file carries on with no problems. A debug line before the copy shows that the count in the reloaded group is 10 as it should be. A count of the player's simgroup after the copy shows as 5, and a dump of the contents does indeed only show 5 simobjects in the group.
Can anyone tell me if I am doing the copy incorrectly, or if there might be something obvious I'm doing wrong? The copy loop is below, and note that I never get my "invalid object" message.
for(%i = 0; %i < playerinv.getCount(); %i++) {
%obj = playerinv.getObject(%i);
if( isObject(%obj)) {
$player.dginv.add(%obj);
}
else {
error("Loading inventory, invalid object in SimGroup");
}
}
playerinv is the reloaded simgroup and $player.dginv is the player's simgroup
Thanks very much, sorry about the code formatting, I don't know how to get those little code boxes!
Dave.
I'm putting a number of simobjects in a simgroup and saving it using the simgroup.save functionality. When I reload the simgroup I just exec the saved file. That recreates the simgroup with the name I saved it as. All fine up to here.
I have another simgroup that belongs to my player and I want to get the simobjects from the reloaded group into my player's group. To do this I just run through a loop adding each object I find in the reloaded group to my player's group. Not sure if this is the way to do it, but I can't find any examples of how to load and allocate from a simgroup.
Anyway, the copy only gets half way through (that is, it copies 5 objects out of 10) and then stops copying with no errors. The rest of the script in the .cs file carries on with no problems. A debug line before the copy shows that the count in the reloaded group is 10 as it should be. A count of the player's simgroup after the copy shows as 5, and a dump of the contents does indeed only show 5 simobjects in the group.
Can anyone tell me if I am doing the copy incorrectly, or if there might be something obvious I'm doing wrong? The copy loop is below, and note that I never get my "invalid object" message.
for(%i = 0; %i < playerinv.getCount(); %i++) {
%obj = playerinv.getObject(%i);
if( isObject(%obj)) {
$player.dginv.add(%obj);
}
else {
error("Loading inventory, invalid object in SimGroup");
}
}
Thanks very much, sorry about the code formatting, I don't know how to get those little code boxes!
Dave.
About the author
#2
By the time you got halfway through, the size of the original SimGroup was half of what it originally was, and the index was pointing past the end of the original SimGroup.
As you noted, you should be using SimSets for this.
03/09/2006 (12:35 pm)
What was going on is that every time you copied the object from your first SimGroup to your second SimGroup, it was in fact removing it from the first group--which screwed up your ordering (everything got shifted down when it was removed).By the time you got halfway through, the size of the original SimGroup was half of what it originally was, and the index was pointing past the end of the original SimGroup.
As you noted, you should be using SimSets for this.
#3
While I'm here, is there a simpler way of allocating a reloaded set to a player variable? I've tried player.var = exec("some file") but that doesn't seem to work. Or is the way I'm doing it the "right" way to do it in TSE?
Thanks very much,
Dave.
03/10/2006 (2:55 am)
Doh! Thanks Stephen for clearing that up. Caught like a babe in arms by a feature I haven't used in the language before :-(While I'm here, is there a simpler way of allocating a reloaded set to a player variable? I've tried player.var = exec("some file") but that doesn't seem to work. Or is the way I'm doing it the "right" way to do it in TSE?
Thanks very much,
Dave.
#4
A) Give each of your sets a unique name that will stay consistent every run of your game, and then you can simply do
B) re-write the saving/loading functionality to collect all of the return values of the "new" operators in some sort of ordered list, and iterate over that list to assign objectID handles to your variables as appropriate. Obviously that's a bit more work ;)
03/10/2006 (10:07 am)
You have a couple of options:A) Give each of your sets a unique name that will stay consistent every run of your game, and then you can simply do
%player.var = nameToId("NameofSet");B) re-write the saving/loading functionality to collect all of the return values of the "new" operators in some sort of ordered list, and iterate over that list to assign objectID handles to your variables as appropriate. Obviously that's a bit more work ;)
#5
Must go and do a search for that nameToId thing, sounds useful now that I know it's there.
Thanks very much for the help.
Dave.
03/11/2006 (4:32 am)
Heh, right then, it looks like option A is the one to go for for now :-) Must go and do a search for that nameToId thing, sounds useful now that I know it's there.
Thanks very much for the help.
Dave.
#6
03/11/2006 (7:27 am)
NameToId() is simply an exposed ConsoleFunction that takes the string, does a Sim::findObject() on the string, and returns the ID of the search result. It's actually not nearly as needed as you may think (after 23 months of Torque development I just found out about it 2 weeks ago for a very specific scenario I had) since the engine implicitly does this conversion for you in > 99.4% of the cases you need it.
#7
It's handy enough for loading things into user vars, so I'm pleased you mentioned it. It gets rid of a for-loop from my code, much cleaner to look at.
Any more hidden script gems you care to pass on will be much appreciated :-)
Dave.
03/13/2006 (9:02 am)
Thanks Stephen, if *you* just discovered it that explains why I couldn't find out anything about it on the forums or TDN. Just a couple of bits of code that use it, but no explanation.It's handy enough for loading things into user vars, so I'm pleased you mentioned it. It gets rid of a for-loop from my code, much cleaner to look at.
Any more hidden script gems you care to pass on will be much appreciated :-)
Dave.
#8
03/15/2006 (12:07 am)
I didn't bother reading this whole post(sorry), but I'm pretty sure something like this would work for moving objects from one SimGroup to another.%firstGroup = nameToId("SavedGroup");
%secondGroup = %player.mGroup;
while(isObject(%simObj = %firstGroup.getObject(0))) {
%secondGroup.add(%simObj);
}
#9
Thanks again,
Dave.
03/15/2006 (3:38 am)
Thanks Josh. Yes, that looks like it should work as well as I see you're always using index 0. Nice to have a backup idea for situations where the direct nameToId() method doesn't fit.Thanks again,
Dave.
Torque Owner Dave
I read that things can only be in one simgroup at a time. Does anyone know if simgroups are thread safe? I just wonder, because I have a dual core PC. Maybe the add/delete functionality is being messed up by that?
Or is this a known bug? I saw something in the forum search about simgroup wierdness, but couldn't view it as it was a T2D article.
Dave.