Adding soldiers to another obj

Solvius

Member
I have two the same instances, one has in a var 100 soldiers and the other has 150 soldiers. How can I merge the soldiers when the instances collide and one of them is destroyed?
 
Last edited:

samspade

Member
First you need some way of getting the instance id from one (or both) of them. There are so many ways to do this that it really depends on what you're doing with the rest of your code. But once you have that, the code is simple:

Code:
///this code is in the object

var inst = instance_place(x, y, obj_soldier); //or some other code to get instance id
if (inst != noone) {
    if (soldier_count >= inst.soldier_count) && (merge_possible) {
        inst.merge_possible = false;
        soldier_count += inst.soldier_count;
        instance_destroy(inst);
    }
}
You have to determine which to merge into the other, but that's really a design question. The above is simply a sample. If there is a collision between to obj_soldier, the instance with the higher soldier count adds the lower soldier count to it's soldier count and destroys the other instance. The merge possible flag is to account for both instances having the same amount of soldiers. I'm not exactly sure what would happen if you didn't have that. I think the one that ran the code first would merge (in which case you wouldn't need the check), but it's possible they would both run the code and destroy each other.
 

Solvius

Member
Thnx samspade

At the moment I have a banner instance with within a variable called banner.soldiers.
This banner instance is standing next to a city with another variable: collision_with_enemytown = 1. When the banner instance gets created this var is set to 0. It then moves to the city and find the other banner standing there. If its 0 then it will destroy itself. So its supposed that the banner instance that has the variable: collision_with_enemytown = 1 will get the soldiers. this, because that banner is in a fight with that city.

I have this now:

Code:
if collision_with_enemytown = 0
{
var inst = instance_place(x, y, obj_banner); //or some other code to get instance id
if (inst != noone) {
    if (banner_soldiers >= inst.banner_soldiers) && (merge_possible) {
        inst.merge_possible = false;
        banner_soldiers += inst.banner_soldiers;
        instance_destroy();
    }
}
}
But I get this error:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Eventobj_banner
for object obj_banner:

Variable obj_banner.merge_possible(100169, -2147483648) not set before reading it.
at gml_Object_obj_banner_CollisionEvent_36_1 (line 6) - if (banner_soldiers >= inst.banner_soldiers) && (merge_possible) {
############################################################################################
 

samspade

Member
Thnx samspade

At the moment I have a banner instance with within a variable called banner.soldiers.
This banner instance is standing next to a city with another variable: collision_with_enemytown = 1. When the banner instance gets created this var is set to 0. It then moves to the city and find the other banner standing there. If its 0 then it will destroy itself. So its supposed that the banner instance that has the variable: collision_with_enemytown = 1 will get the soldiers. this, because that banner is in a fight with that city.

I have this now:

Code:
if collision_with_enemytown = 0
{
var inst = instance_place(x, y, obj_banner); //or some other code to get instance id
if (inst != noone) {
    if (banner_soldiers >= inst.banner_soldiers) && (merge_possible) {
        inst.merge_possible = false;
        banner_soldiers += inst.banner_soldiers;
        instance_destroy();
    }
}
}
But I get this error:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Eventobj_banner
for object obj_banner:

Variable obj_banner.merge_possible(100169, -2147483648) not set before reading it.
at gml_Object_obj_banner_CollisionEvent_36_1 (line 6) - if (banner_soldiers >= inst.banner_soldiers) && (merge_possible) {
############################################################################################
my code was written assuming that both instances would be instances of the same object. If that is not true, you will need to put the important variables in all possible objects. This code says that obj_banner does not have a variable named merge_possible. However, if your code has some other way of figuring out who will merge, then you probably don't even need that variable. Again, my code will only work for very specific situations. The important parts are of it are:
  • figure out the instance id
  • determine who gets the soldiers and who gets destroyed
  • use the instance id to add soldiers and destroy the instance
How you implement this will depend again on how you set things up. Perhaps in your case you actually want the city object to collect instance ids from both, or all, colliding banner objects and then add the values to one and destroy the other.
 

Solvius

Member
They are both instances of the same object. Like I told in my second message.
I added the merge_possible as a var:
Code:
if collision_with_enemytown = 0
{
var inst = instance_place(x, y, obj_banner); //or some other code to get instance id
var merge_possible = true
if (inst != noone) {
    if (banner_soldiers >= inst.banner_soldiers) && (merge_possible) {
        inst.merge_possible = false;
        banner_soldiers += inst.banner_soldiers;
        instance_destroy();
    }
}
}
And then no error, but the second instance gets destroyed and nothing happens to the soldiers.
I did figure out the id of the instance with this:
banner = id
After that nothing seems to work.
The determering is that the instance with the variable collision_with_enemytown = 1 gets the soldiers.
use the instance id to add soldiers and destroy the instance. This seems impossible. I do not ask help for nothing. I tried several things.
Your code does not work, sorry.
Try it out and you'l see for yourself.
 

samspade

Member
They are both instances of the same object. Like I told in my second message.
I added the merge_possible as a var:
Code:
if collision_with_enemytown = 0
{
var inst = instance_place(x, y, obj_banner); //or some other code to get instance id
var merge_possible = true
if (inst != noone) {
    if (banner_soldiers >= inst.banner_soldiers) && (merge_possible) {
        inst.merge_possible = false;
        banner_soldiers += inst.banner_soldiers;
        instance_destroy();
    }
}
}
And then no error, but the second instance gets destroyed and nothing happens to the soldiers.
I did figure out the id of the instance with this:
banner = id
After that nothing seems to work.
The determering is that the instance with the variable collision_with_enemytown = 1 gets the soldiers.
use the instance id to add soldiers and destroy the instance. This seems impossible. I do not ask help for nothing. I tried several things.
Your code does not work, sorry.
Try it out and you'l see for yourself.
I did and it does. But likely it relies on assumptions which are different. However, one important change in the code you typed could be causing the problem. You have instance_destroy, rather than instance_destroy(inst). The difference being your code will destroy the instance that gets the additional soldiers rather than the instance that gives the additional soldiers. Additionally, by adding var merge_possible = true you have removed the error message but also made the check pointless as it will always evaluate to true if set two lines before being called (rather than in the create event). These are obviously problems, but might not be the main ones. Based on your description, I wonder if collision_with_enemytown == 1 shouldn't be the conditional to check for as you want that object to receive the soldiers. Also, like I said, if you already have a means of determining who gets the soldiers then you probably don't need the >= merge possible check.

Code:
if collision_with_enemytown == 1
{
    var inst = instance_place(x, y, obj_banner); 
    if (inst != noone) {
        banner_soldiers += inst.banner_soldiers;
        instance_destroy(inst);
    }
}
Again though without actually having your project on my computer this is a guess. Sometimes on the forum the exact code will be given, but not most of the time. Most of the time you're going to get code that will correctly present one implementation of an idea which may or may not work with the rest of your project. It is important, and generally necessary, to take apart the code and understand how it works and then correctly apply it to your own project rather to copy and paste it.
 

Solvius

Member
samspade, thnx for the effort. before I post on the forum I tried several things, and nothing worked. Also you code is not working for what I want to do.
Thats not a problem because I solved the prob in a different manner.
There are simply to much problems with doing it the way I tried.
Again thnx for your effort.
 

NicoFIDI

Member
if(soldiers == 0)
instance_destroy();
else {
soldiers += other.soldiers;
other.soldiers = 0;
}

On the collide event
 
Last edited:
Top