Code : how would i use a parent instance to regroup instances

L

Linkdeous

Guest
Hello guys, i'm making a game wherethe player would choose a certian zone with the mouse , then the villager would go cut the wood, mine the stone ,...

or making them check for every single instance out there or stuff would be pretty ressource intensive/having a huge mess of a code, so i thinked about adding the instance the "order" object touches to a parent instance, that would tell the npc to go to the nearest one, and cut it, mine it, etc etc

So for exemple i would need to use this like this :
after adding a tree / a rock to the order parent named obj_selected, does using this kind of code work ? :

npc step event :
work = instance_nearest(x,y,obj_selected)
move_toward_points(work.x,work.y,speed)

does this would make the npc go to the "controller" location, or would go to a tree, a rock, ... ?

And else, does there is some kind of way to do it with a dslist or some magical complexw stuff :) ? thanks for reading
 

NightFrost

Member
I guess this is for some type of colony management / survival game in the style of Dwarf Fortress, Oxygen Not Included, Rimworld etc? One way in which these handle player-given orders is a job management system. For example if you select the Cut Trees command and then select a bunch of trees, the game creates a job for it and associated selected trees with it. The job list might be as simple as an array into which you insert arrays containing relevant job information. When a dwarf / duplicant / colonist / whatever becomes idle, they check the job list and claim the topmost job, then start going through the paces of completing it. They might first check they have an appropriate tool, then pathfind to the closest entity indicated in the job and perform the relevant task on it. There might also be interfaces to change job priorities so you can control what need to be done sooner, and some jobs are autogenerated by the game - for example in Dwarf Fortress, creating jobs to haul logs of felled trees to storage zone is automated.
 
L

Linkdeous

Guest
I see , so i shouldnt go for parent instance but some array list then ? So i should make a 2D array for exemple, then set each time a tree, ... is selected for harvesting the ressource location like [1;1]=x[1;2]=y
then make each time the villager do the task tat the line n°1, then delete it so a villager will do the task at the (previous n°2 line) 1st line again then ? Seem clever, thanks :)
 

NightFrost

Member
A 2d array may not be the best choice because you can't easily rearrange it in case you want to have job priorities and let player change them around. Well you could have one column be the priority but that creates unnecessary extra coding when priority queue data structure exists. You'd want to first create the new job as a separate array and then add it to the job listing. Enumerate (or use macros) all possible jobs first and create a job list as priority queue:
Code:
enum Job_Types {
    Mine,
    Cut_Tree,
    Harvest
    ...
}
Job_List = ds_priority_create();
Then decide that the first entry in the job's array is the job type, and the rest are targets for the job. So your code that reads player input, when seeing they want to define a job, creates an array for new job (make it a script for easy reuse). Let's assume the routine already knows what job enum type player selected and saved in in Selected_Job, and knows what targets have been mouse-drag selected and saved their instance id values in Job_Targets array.
Code:
New_Job = array_create(0);
New_Job[0] = Selected_Job;
for(var i = 0; i < array_length_1d(Job_Targets); i++){
    New_Job[i + 1] = Job_Targets[i];
}
Job_Targets = 0; // Destroy the array as it has no use now.
// Or if you don't mind working with arrays inside arrays, instead of the loop just insert the array:
New_Job[1] = Job_Targets;
Now you have a job defined and you add it to job list, for example at some predefined default priority:
Code:
ds_priority_add(Job_List, New_Job, Default_Priority):
When a character wants to take a job they, read and remove the highest priority job in the Job_List, then look at the array to figure out the job type and target instances. From here onwards it is the character's task to refer to their current job and act accordingly.
 
L

Linkdeous

Guest
Thanks a lot, it's basically exactly what i need actually, i will try my best to make something as good as this code ,i'm not very efficient at making compact and efficient code x)
 
Top