• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Snippet] LUA_OBJECT_ID

Level 31
Joined
Jul 10, 2007
Messages
6,306
A very simple snippet for generating unique ids for objects with object merger.

The generateid function in object merger starts at its first index, so it does not return a completely unique id and may overwrite custom objects.

This script will continue to loop until it finds a unique id. Very simple, but very useful.

This script only returns a unique object id. It does not create the actual object for you.

Must be run through ObjectMerger

LUA_FILE_HEADER
JASS:
//GetObjectId 1.0.0.5
//! externalblock extension=lua FileExporter $FILENAME$
    //! runtextmacro LUA_FILE_HEADER()
    //! i writelua("GetObjectId", [[
    //////////////////////////////////////////////////////////////////
    //code

    //! i function getobjectid(obj, objecttype)
        //obj refers to the base object
            //"hpea", "Amov", "Bphx", etc
        //objectType refers to the type of object to create
        
        //! i if (currentobjecttype() ~= objecttype) then
            //! i setobjecttype(objecttype)
        //! i end
        //! i local object = generateid(obj)
        //! i while (
            //! i objectexists(object) or 
            //! i string.find(object, "'", 1, true) ~= nil or 
            //! i string.find(object, '\\', 1, true) ~= nil or 
            //! i string.find(object, ',', 1, true) ~= nil or 
            //! i string.find(object, '/', 1, true) ~= nil) do
            
            //! i object = generateid(obj)
            
        //! i end
        //! i return object
    //! i end

    //end code
    //////////////////////////////////////////////////////////////////
    //! i ]])
//! endexternalblock

Demo
JASS:
//! externalblock extension=lua ObjectMerger $FILENAME$
    //! runtextmacro LUA_FILE_HEADER()
    //! i dofile("GetObjectId")

    //generates an object id for hpea (peasant) of object type units
    //! i local object obj = getobjectid("hpea", "units")

    //create the object using the retrieved object id
    //! i setobjecttype("units")
    //! i createobject("hpea", obj)
        //modifications
//! endexternalblock
 
Last edited:

BBQ

BBQ

Level 4
Joined
Jun 7, 2011
Messages
97
This snippet serves absolutely no purpose. The ObjectMerger already provides the user with a generateid() function. All you need to do is write a snippet (or update the ObjectMerger) that would write the generated ID inside the map script.

Code:
int lua_generateid(lua_State *l)
{
	const char *oldid = lua_tosafestring(l, 1);
	dprintf("generateid %s\n", oldid);
	if (!oldid || strlen(oldid) != 4) {
		sprintf(errbuf, "Invalid object id %s\n", oldid);
		throw errbuf;
	}
	if (!curobj)
		throw "No object type has been loaded, use 'setobjectype' to do so\n";
	War3Id newid = curobj->getunusedid(War3Id(oldid));
	char oid[5] =  { 0, 0, 0, 0, 0 };
	memcpy(oid, newid.id, 4);
	lua_pushstring(l, oid);
	return 1;
}
 

BBQ

BBQ

Level 4
Joined
Jun 7, 2011
Messages
97
Yes, many parts of the JNGP are open-source... hosted somewhere on Assembla. Also, PitzerMike posted a direct link to the ObjectMerger's source code at Wc3C.net just recently - you may wanna check that out.

Those tools have "unimaginable" potential... it's a shame they're left to rot just like that (personally, I feel very stupid for not being able/willing to do anything - but I guess I'm too young for that).
 
Top