• 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!

Cannot update object position when deactivated

S

steveyg90

Guest
Hi,

I deactivate game objects when not in view, but still need their position to be updated as they appear on a mini map( I draw circles for them) and also I want them to move through the game world. Deactivating the object stops me from then updating the x,y position of the object (although in debug mode shows the position is updating correctly when deactivated???). Is there a way I can create a list of x,y values for each object I have in my game and then just updates these instead and get the object(s) to use these? Can I create an object like I do in C++:

Code:
class ObjectPos { float x, y };

ObjectPos obj = new ObjectPos( );

obj.x++;
obj.y++;
and store a list of the above? Is it something like using a ds_list?

Code:
globalVar shipBuffer;
ds = ds_map_create();
ds[? "x"] = xpos;
ds[? "y"] = ypos;
shipbuffer[index] = ds;
Thanks in advance,
Steve
 
A

Andy

Guest
A deactivated instance essentially does not exist. You can’t change values within deactivated instances. You have to reactivate them before changing their values.

Edit: Sorry I misread your question. Yes, you can store x,y positions within a "master" object. Then get the values from that object once you reactive an instance.
 

The-any-Key

Member
To change values in deactivated instances you need to save the instance id. Note that "with" wont work. So you need to address them using instance_id.value_to_change.
So if you create an instance and save the id to a var:
Code:
my_instance=instance_create(0,0,obj_inst);
Say you deactivate my_instance. When the instance is deactivated and you want to change or grab values from it you need to use
Code:
my_instance.x=60;
if my_instance.y>60 my_instance.under_water=true;
See docs: https://docs.yoyogames.com/source/d...s/instances/deactivating instances/index.html
"A deactivated instance effectively ceases to exist in the game, but individual instances can still have their variables accessed. You cannot use with(object) or with(instance) however, but rather you must access the instance directly using it's unique id"
 
S

steveyg90

Guest
Thanks all,

I am using their instance id's, but still, no joy. Here is my creation code to create all the instances:

Code:
for(var i=0; i<amountOfRandomAlienShips;i++) {
    xx = 400 + random(worldWidth);
    yy = 400 + random(worldHeight);
    randomAlienShipList[i] = instance_create_depth(xx,yy,0,o_AlienShipOne);
Then I have a script which deactivates objects which is called every frame:

Code:
var cam = view_camera[0];
var camX = camera_get_view_x(cam);
var camY = camera_get_view_y(cam);
var camW = camera_get_view_width(cam);
var camH = camera_get_view_height(cam);

instance_deactivate_object(o_asteroid);
instance_deactivate_object(o_planet);
instance_deactivate_object(o_AlienShipOne);                                // This is the object that appears on the mini map
instance_activate_region(camX, camY, camW, camH,true);
So as you can see any instance of o_AlienShipOne is activated if in the active region.

Then I do the following in my mini map (radar) draw_gui event:

Code:
map_radius_squared = power(view_wview[0] * 4.5, 2);
for(k=0; k<amountOfRandomAlienShips;k++) {
    dx = randomAlienShipList[k].x - o_player.x;
    dy = randomAlienShipList[k].y - o_player.y;
    dis_squared = (dx*dx + dy*dy);// sqrt(dx * dx + dy * dy);  // use own distance function
    if(dis_squared < map_radius_squared)//view_wview[0] * 4.5)
    {
            minimapX = worldWidth / randomAlienShipList[k].x;
            minimapY = worldHeight / randomAlienShipList[k].y;
            awayx = max(o_player.x-randomAlienShipList[k].x,randomAlienShipList[k].x-o_player.x);
            awayy = max(o_player.y-randomAlienShipList[k].y,randomAlienShipList[k].y-o_player.y);

            if(o_player.x < randomAlienShipList[k].x)
                minimapX += 150 + (awayx/30);    // the / 30 is just a coefficient
            else
                minimapX += 150 - (awayx/30);
            if(o_player.y < randomAlienShipList[k].y)
                minimapY += 150 + (awayy/30);
            else
                minimapY += 150 - (awayy/30);
            // display on mini map as a circle
            draw_circle(minimapX,minimapY,2,true);
    }  
}
So as you can see in the draw_gui, I'm using the instance of the objects just like suggested, but they don't move on the mini-map (they do if I don't deactivate the objects)...
I've been trying to sort this out most of the day and just cannot see anything wrong, debugger shows correct positioning and objects x,y being updated when they
are deactivated?...

Any other thoughts?

Thanks
 

The-any-Key

Member
but they don't move on the mini-map
What do you mean they don't move on the mini map? You get their position so they are drawn on the spot where they where deactivated?

Note that all events in a deactivated instance wont run. So if you want the AI to handle the instances and move them around, you need to loop all deactivated ship instances and move them with the same procedure (instance_id.x+=1...) in a activated instance. So you need to create a control instance that handle this.
 
S

steveyg90

Guest
The control the minimap is in is in a different object that's not deactivated. The code for the updates to the position of the ships are also in another object which isn't deactivated. Here is the code for which shows how the positions are updated in a control that is always active:

Code:
for(var i=0; i<amountOfRandomAlienShips;i++) 
{
            randomAlienShipList[i].direction=choose(0,30,60,90,120,150,180,210,240,270,300,330,360);
            randomAlienShipList[i].image_angle=    randomAlienShipList[i].direction;   
            sspeed = choose(1,2);
            randomAlienShipList[i].speed=sspeed;
            randomAlienShipList[i].x+=sspeed;
            randomAlienShipList[i].y+=sspeed;         
        }      
}
alarm[0]=room_speed;
When I display the contents of randomAlienShip array inside the minimap code, it shows the values are being updated when instances are deactivated...
 

sylvain_l

Member
what's the speed of your ship and what's the ratio of your minimap ? if your minimap is at 1:100 and your ship only move a few pixel per step say 3, at 30fps you only see ships moving every 10 seconds at best (depends if they move straight to the player or if they move randomly).
 
Last edited:
S

steveyg90

Guest
what's the speed of your ship and what's the ratio of your minimap ? if your minimap is at 1:100 and your ship only move a few pixel per step say 3, at 30fps you only see ships moving every 10 seconds at best (depends if they move straight to the player or if they move randomly).
Hi, you see them moving on the minimap when the objects are deactivated...
 
S

steveyg90

Guest
Strange, adding 50 to the controller which loops and moves the instances we now see it moving on the minimap when the ship(s) are deactivated, of course, when ship is active I need to stop adding the 50.

Also, all the ships on the minimap will go to the right now...
 
Last edited by a moderator:
S

steveyg90

Guest
This is my new code to update the ships in the controller:

Code:
for(var i=0; i<amountOfRandomAlienShips;i++) 
{
        randomAlienShipList[i].direction=choose(0,30,60,90,120,150,180,210,240,270,300,330,360);
        randomAlienShipList[i].image_angle=    randomAlienShipList[i].direction;   
        sspeed = choose(-2,-1,1,2);
        randomAlienShipList[i].speed=sspeed;
}
alarm[0]=room_speed;
I just need the ship to face the direction it is travelling now.
 
Top