Skip to content

CNC Generals Zero hour Lua scripting examples

MetaIdea edited this page Mar 14, 2024 · 5 revisions

Loop through all available map objects and print their object types to the console:

for objectindex,object in globals() do
	if strfind(objectindex, "ObjID") then
		print(GetObjectName(object))
	end
end

Spawn a dozer at Player 1 start waypoint and print it's ObjectType and teleport it 10 feed up.

MyUnit = "test1"
ExecuteAction("CREATE_NAMED_ON_TEAM_AT_WAYPOINT", MyUnit, "AmericaVehicleDozer", "teamplayer0", "Player_1_Start")
print(GetObjectType(MyUnit))
x,y,z = ObjectGetPosition(MyUnit)
ObjectSetPosition(MyUnit,x,y,z+10)

Spawn another extra supply depot next to each existing one on the map

local x,y,z
local ObjectList = {}
local ObjectType
local DuplicateOffset = 85
for objectIndex,object in globals() do
	if strfind(objectIndex, "ObjID") then
		ObjectType = GetObjectType(object)
		if ObjectType == "SupplyDock" or ObjectType == "SupplyPile" or ObjectType == "SupplyWarehouse" then
			tinsert(ObjectList, {object,ObjectType})
		end
	end
end
for i=1,getn(ObjectList),1 do
	x,y,z = ObjectGetPosition(ObjectList[i][1])
	ExecuteAction("UNIT_SPAWN_NAMED_LOCATION_ORIENTATION", GetUnitName(), ObjectList[i][2], NeutralTeam, {x+DuplicateOffset,y+DuplicateOffset,GetGroundHeight(x+DuplicateOffset,y+DuplicateOffset)}, ObjectGetRotation(ObjectList[i][1]))
end

Memory read examples

function GetObjectBuildCost(object)
	return MemRead(format("%X",tonumber(ObjectGetPointer(object),16)+4) .. "+3CC+0","int")
end

function MemReadGetObjectType(object)
	return MemRead(format("%X",tonumber(ObjectGetPointer(object),16)+4) .. "+10+4+0","text")
end

function MemReadGetObjectHealth(object)
	return MemRead(format("%X",tonumber(ObjectGetPointer(object),16)+372) .. "+8+0","float")
end

function GetObjectBuildCost(object)
	return MemRead(format("%X",tonumber(ObjectGetPointer(object),16)+256) .. "+1C+0","int")
end

One of the many utility functions from EssentialScripts.lua

function GetPlayer(object)
	local UnitName = GetObjectStringRef(object)
	for i=1,getn(Players),1 do
		if EvaluateCondition("NAMED_OWNED_BY_PLAYER", UnitName, Players[i]) then
			return Players[i]
		end
	end
	return "no player"
end