AIGroup.CreateGroup bug

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
Xycor
Engineer
Engineer
Posts: 29
Joined: 17 Mar 2014 01:28

AIGroup.CreateGroup bug

Post by Xycor »

I cannot seem to create a group for each vehicle type using AIGroup. Instead the first group I create succeeds, but the others are ignored.

In the example below I only get a Rail group. If I put the Road group creation as the first call I get a road group but no rail group.

Ideas? This is on 1.4 RC1.

Code: Select all

function OrdersAI::CreateGroup(VehicleType, GroupName)
{
	local newgroup = AIGroup.CreateGroup(VehicleType)
	AIGroup.SetName(newgroup, GroupName)
	return newgroup
}

function OrdersAI::CreateManualGroups()
{
	/*Create groups to give users hints about how to place units under manual control*/
	
	//VT_RAIL 	 Rail type vehicle.
	//VT_ROAD 	 Road type vehicle (bus / truck).
	//VT_WATER 	 Water type vehicle.
	//VT_AIR 	 Air type vehicle.
	
	local ManualGroupName = "Human Controlled"
	
	GetOrCreateGroup(AIVehicle.VT_RAIL, ManualGroupName)
	GetOrCreateGroup(AIVehicle.VT_ROAD, ManualGroupName)
	GetOrCreateGroup(AIVehicle.VT_WATER, ManualGroupName)
	GetOrCreateGroup(AIVehicle.VT_AIR, ManualGroupName)
}

function OrdersAI::Init()
{	
	AILog.Info("Init Starting")
	OrdersAI.CreateManualGroups() 
	AILog.Info("Init Complete")
	return
}
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: AIGroup.CreateGroup bug

Post by R2dical »

Group names need to be unique:
static bool AIGroup::SetName wrote:Exceptions:
AIError::ERR_NAME_IS_NOT_UNIQUE
So the others are not being ignored but are actually throwing this error, and "AIGroup.SetName" is returning false. I find its good practice to validate every single API method for times like these:
function OrdersAI::CreateGroup(VehicleType, GroupName)
{
local newgroup = AIGroup.CreateGroup(VehicleType)
if (!AIGroup.SetName(newgroup, GroupName)) throw (AIError.GetLastErrorString());
return newgroup
}
Xycor
Engineer
Engineer
Posts: 29
Joined: 17 Mar 2014 01:28

Re: AIGroup.CreateGroup bug

Post by Xycor »

Thanks. That fixed it.
Xycor
Engineer
Engineer
Posts: 29
Joined: 17 Mar 2014 01:28

Re: AIGroup.CreateGroup bug

Post by Xycor »

Thanks. That fixed it.
frosch
OpenTTD Developer
OpenTTD Developer
Posts: 988
Joined: 20 Dec 2006 13:31
Location: Aschaffenburg

Re: AIGroup.CreateGroup bug

Post by frosch »

Huh?

Uniqueness is only enforced for groups of the same vehicle type (since 1.3). Above script works fine for me, if I replace GetOrCreateGroup with CreateGroup.
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁
Xycor
Engineer
Engineer
Posts: 29
Joined: 17 Mar 2014 01:28

Re: AIGroup.CreateGroup bug

Post by Xycor »

Doh! You are right. I found my bug. I forgot I was using my GetOrCreate function. It didn't sort by specify the vehicle type in the search.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 16 guests