[SpaceMod] Unit Sandbox

Vadus

pretend the impossible
Joined
Nov 23, 2003
Messages
374
Location
Northern Germany
current Version : 0.1

Hi all,

let me present you the Unit Sandbox, with which you can rename and prepromote Units. These units can be built beside the original units.
The good thing is, that this system isn't touching the xml structure. Only GlobalContext is used, so that this ModComponent can be added to another mod easily ;)
Because this UnitSandbox will be a part of my SpaceMod, you will find some scifi designations in the files, e.g. the most important class , which is called : SpaceMod.py . But anyone is free to use this UnitBox for his mod.
Changes in bigger files are surrounded with a comment containing the word 'Vadus' or 'SpaceMod' .
here are some screenshots explaining, how the Sandbox is working:

http://www.vadus.de/images/civ4Unitsandbox01.jpg
http://www.vadus.de/images/civ4Unitsandbox02.jpg
http://www.vadus.de/images/civ4Unitsandbox03.jpg
http://www.vadus.de/images/civ4Unitsandbox04.jpg

Many Thanks to talchas, Belizan, Kael and 12monkeys for answering my questions here :)

Installation
Unzip the file to your Civ4 Mod folder, and start the Mod 'Vadus' .

Current Problems :
1. Don't swich from civ to windows, while you have the SandBox Screen open,
because internal variables will be reinitialized (which means that the civ variables will be refreshed, and the SpaceMod internals deleted), and the SandBox System won't work anymore
2. I can't find the code lines for setting the name of the unit, which is currently built by a city, at the cities textbars on the map (not in city screen) ! So, where you can see the city name and the current production...
3. Same thing with the unit name of the city production bar in the cityscreens header.

Future Features
1. UnitList of predefined units will be saved, too, so that the player will get the old unitlists when he loads a game.
2. Buttons of the UnitSandbox Screen : Remove and Replace a predefined unit
3. Change the unit costs, depending on the amount of promotions
4. Change the List of Available Promotions by regarding the promotions dependencies
5. Set the Unit MaxHitpoints with the Sandbox, if possible


It would be great, if anyone wants to help me out a bit, because I have only time in the late evening to do something. And with my hole spacemod project, changes to this will take much much time ..
 

Attachments

Vadus said:
Current Problems :
1. Don't swich from civ to windows, while you have the SandBox Screen open,
because internal variables will be reinitialized (which means that the civ variables will be refreshed, and the SpaceMod internals deleted), and the SandBox System won't work anymore
...
Future Features
1. UnitList of predefined units will be saved to, so that the player will get the old unitlists when he loads a game.
You should probably take a look at the SD-Toolkit and save your info using that - it should fix both of these by letting Civ4 deal with these issues. Looks to me like you could do this:
Code:
    def __init__(self): 
        """ Create singleton instance """ 
        # Check whether we already have an instance 
        if getInstance() is None: 
            # Create and remember instance 
            setInstance(SpaceMod.__impl())
              
    def __getattr__(self, attr): 
        """ Delegate access to implementation """ 
        return getattr(getInstance(), attr) 
 
    def __setattr__(self, attr, value): 
        """ Delegate access to implementation """ 
        return setattr(getInstance(), attr, value) 

def getInstance():
    try:
        return sdGetGlobal( 'MyModName_string', 'SpaceMod.py.instance' )
    except: # should only take the particular type that this throws, but I don't know what that is.
        return None
def setInstance(inst):
    return sdSetGlobal( 'MyModName_string', 'SpaceMod.py.instance',inst)
This might have two problems - it could slow the game down if you use SpaceMod.py a lot, as you are then unpickling each time - a fix would be to cache an instance like before, but call getInstance "if SpaceMod.__instance is None" and only make a new __impl if that also fails. The second problem.... I forget what I was going to say :blush:.
 
This is just an incredible idea! I mean if this is possible maybe somebody will figure out a way to programmatically assemble units (as long as they share bones and animation it shouldn't be a problem), then you could not only assign promotions at will but will actually be able to assemble brand new units.

Great job thinking outside the box Vadus. :goodjob:
 
talchas said:
You should probably take a look at the SD-Toolkit and save your info using that - it should fix both of these by letting Civ4 deal with these issues.
Is the Toolkit a Mod ? Or do I have to import something from civ4 additionaly ?
I get the error : sdSetGlobal - not found

EDIT : allright, found it : http://forums.civfanatics.com/showthread.php?t=146130
 
@talchas :
I tried your sample, but get an error in the sdToolKit.py (see attachment).
After loading the mod, I get the message, that the Mod Data is initialized (dsToolKit.py -> sdModInit ) , but than after pressing the Unit Sandbox AdvisorButton the error occurs.

Also when I import the SpaceMod.py in the sdToolKit the error occurs in the following function :
Code:
def sdSetGlobal( ModID, var, val ):
	szGlobal = 'Global'
	mTable           = sdModLoad(ModID)
	if ( mTable.has_key(szGlobal) ):
		eTable   = pickle.loads(mTable[szGlobal])   # <-- occurs here
	else:
		eTable   = {}
	eTable[var]      = val
	mTable[szGlobal] = pickle.dumps(eTable)
	sdModSave(ModID, mTable)

do you, or anyone else, have any thoughts about this ?
I assume, that the pickle functionality only can load known instances from a dumped file like a list of strings and stuff , but no self made classes ? :hmm:
 

Attachments

  • error01.png
    error01.png
    168.7 KB · Views: 386
Vadus said:
@talchas :
I tried your sample, but get an error in the sdToolKit.py (see attachment).
After loading the mod, I get the message, that the Mod Data is initialized (dsToolKit.py -> sdModInit ) , but than after pressing the Unit Sandbox AdvisorButton the error occurs.

Also when I import the SpaceMod.py in the sdToolKit the error occurs in the following function :

do you, or anyone else, have any thoughts about this ?
I'll go and fiddle around with python for a bit and see what is wrong
I assume, that the pickle functionality only can load known instances from a dumped file like a list of strings and stuff , but no self made classes ? :hmm:
I don't think so - the whole point is that pickle can serialize most classes with no effort. (or at least that is my understanding, anyone who actually really knows python feel free to correct me)
 
Ah, found the problem:
From the pickle docs:
The following types can be pickled:
...
* classes that are defined at the top level of a module
So, __impl isn't pickleable. However, if you change over to using sd toolkit to make it singleton, then it doesn't matter - move all of the functions to class SpaceMod, remove __getattr__ and __setattr_ and instead of doing SpaceMod.SpaceMod().doStuff() do SpaceMod.getInstace().doStuff()
 
talchas said:
However, if you change over to using sd toolkit to make it singleton, then it doesn't matter - move all of the functions to class SpaceMod, remove __getattr__ and __setattr_ and instead of doing SpaceMod.SpaceMod().doStuff() do SpaceMod.getInstace().doStuff()
Yes, this should behave like the actual Singelton. I will try, and test it also under the performance aspect.
 
First, great idea for a MOD, and great job! I may not be understanding how the upgrading system is working, but here are a few ideas (if this is not how it is already)...

First, you can set the upgrades to the units in the future. They will use whatever experience points they have when they are built to upgrade, and they will continue to auto-update when they can based on what you chose for them on creation.

Second, you can use as many upgrades as you want on a unit. The unit will be built with the upgrades from the beginning, however, the unit cannot upgrade again, will cost a lot more (cumulative for each upgrade)
 
pap1723 said:
Second, you can use as many upgrades as you want on a unit. The unit will be built with the upgrades from the beginning, however, the unit cannot upgrade again, will cost a lot more (cumulative for each upgrade)
This is like it's working now. But the unit still can get promotions on the battlefield, but in my mind, this should be other promotions.
For example : In the unitbox you can upgrade the unit from a set of 10 Promotions (e.g. Drive Level 1 - 3) . In the Battlefield the Unit can be upgraded with other, battle - promotions, like March ..
 
Rabbit said:
This is just an incredible idea! I mean if this is possible maybe somebody will figure out a way to programmatically assemble units (as long as they share bones and animation it shouldn't be a problem), then you could not only assign promotions at will but will actually be able to assemble brand new units.

Great job thinking outside the box Vadus. :goodjob:
He didn't think outside the box, he thought inside the sandbox :lol:

Looks very, very cool! Nice work :)
 
finnally, we got a unit editor, this is great, good job. Just make sure you update this thing cause we want you to iron some bugs and improve this very important and useful component.
 
Back
Top Bottom