• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [Help] Create building

Sawyer

Member
Hello everyone,

I try to make a building system.
I can put many differents building in my room. And i want than my units goes to build them.
For exemple if i put a wall on the map, and if an unit is near, it goes to build this wall. After less or more time it end to build it.

Actually if i put my wall my units goes to this wall, change his alpha instantanly and stay.


In my object player step event :

Code:
var can_build = 0;
with obj_in_contruction_parent
    {
    if hp_build = hp_max_build
        {can_build = 0}
        
    if hp_build < hp_max_build
        {can_build = 1}}
    
nearest = instance_nearest(x,y,obj_in_contruction_parent)

if (selected = false && distance_to_object (obj_in_contruction_parent) <= 600  && can_build = 1 )
{
if (mp_grid_path(global.grid,path,x,y,nearest.x-1,nearest.y,1))
{path_start(path,speed_unit,path_action_stop,false);
        
}}

In my obj_road :

Create event

Code:
hp_build = 0
hp_max_build = 100
Step event :
Code:
if (distance_to_object (obj_player) <= 10 )
{
hp_build +=1 //This is to have a counter to build
}
if hp_build = 100
{mp_grid_add_instances(global.grid, self, false);} //add my object to the grid
draw event :
Code:
image_alpha=0.5 + 1*hp_build
draw_self()
 

Sawyer

Member
What i want if i put my wall on the room :
- Low alpha (0.5) --->Ok
- My units, if they are near to this wall goes to build it ---> ok
- the building creation takes time ----> not ok, they build it instantanly
- when it end alpha 1, and my units have collision with my wall--->ok

The second problem is that my units stay near to my building. It seems they don't understand that my building in completed.
 

Sawyer

Member
Indeed it's better with your code.
But my units still doesn't detect when my building is completed.

Maybe it is possible to change my wall parent.
If hp_build = 100 --> change parent
 
You are so close to having it work, don't need to change parent I think for now.

You need to make sure that once the building is complete, can_build == 0.

So you need to add a check so that if HP_build == 100, then the player stops adding HP to the building. Currently your code just keeps adding hp endlessly.

Once you do that, your code for setting can_build to zero will work, and then the player will end up looking for a different building.
 

Sawyer

Member
With this code it should work, but no.

Code:
var can_build = 0;
with obj_in_contruction_parent
    {
    if (hp_build >= 100)
        {can_build = 0}
        
    if (hp_build < hp_max_build)
        {can_build = 1}}
    
nearest = instance_nearest(x,y,obj_in_contruction_parent)

if (selected = false && distance_to_object (obj_in_contruction_parent) <= 600  && can_build = 1 )
{

if (mp_grid_path(global.grid,path,x,y,nearest.x-1,nearest.y,1))//-10 to avoid to be stuck
{path_start(path,2,path_action_stop,false);
        
    }

}
 

Sawyer

Member
I tried a different approach [ i use a draw text with string(can_build) to know my player state]

In my step event i wrote this :

Code:
can_build = 0;
nearest = instance_nearest(x,y,obj_in_contruction_parent)
if distance_to_object (nearest) <= 600{
with (nearest)
    {
    if (hp_build >= 100)
    with obj_player{
        {can_build = 0}}
 
    if (hp_build < hp_max_build)
        with obj_player{
        {can_build = 1}}}}
 

if (selected = false && distance_to_object (nearest) <= 600  && can_build = 1 )
{

if (mp_grid_path(global.grid,path,x,y,nearest.x,nearest.y,1))
{path_start(path,2,path_action_stop,false);
}}
With this code :

If i build my wall near to my player :
- He goes to build it (can_build = 1)
- When he end it can_build = 0

But now if i put an other wall in his building range can_build stay at 0.

However if i select my unit and if i put it near to this new wall, can_build = 1, and he do the job.
But i have to put it closer to the wall to build than the finished wall.
If i put it closer the finished wall than the wall to build, can_build stay at 0.

My problem is that it detect the hp_build of the nearest obj_in_contruction_parent only.
 
Last edited:
The problem here is you are using "var" for the can_build variable.

This makes it a local variable to the script, even if it is inside the with() statement.

Just remove the "var can_build =0" line.

Make sure you have added can_build into the create event of the constuction object.
 
You'll have to write some code that goes through all the instances and get the nearest one, but also checks it can be built.

Using instance_nearest() by itself is always going to return only the nearest building, regardless of it being built or not.
 

Sawyer

Member
Indeed i just need to find the nearest instance with less than 100 hp_build.
I am searching how to do it.
 
L

Linkdeous

Guest
Maybe make a raoming state in your worker object, so they get around then after they are attrcted to a building, they are going to construct it (just make a variable like progress in your wall obj that is like 10000) so when the worker start constructing, they add 1 to a empty variable, and when it's done (after adding a lot of 1 so it get to 10000) the construct is finished, and the Romaing state kick in and they wander around after that
 

Sawyer

Member
With this code my units go near the obj_in_contruction_parent with the less hp_build.
It also and i don't know why change my mouse sensibility (my fps don't move).

Code:
var val, lowest, lowest_val;
lowest = noone;// No instance at all.
lowest_val = 0;
with (obj_in_contruction_parent) {
  val = hp_build;
  if (lowest == noone || val < lowest_val) {//or
    lowest = id;
    lowest_val = val;
  }
}
if (lowest != noone) {


if (mp_grid_path(global.grid,path,x,y,lowest.x,lowest.y,1))
{path_start(path,2,path_action_stop,false);
}
}
 
Last edited:
L

Linkdeous

Guest
Why is there my quote in the middle xd ?
anyway the romaing state would be another vairable, that make the worker do roam around

if roaming=1{ //code so he walk around so he will move around after finishing building a building}
 
Top