Structs not working as I thought, now I cannot get things to function.

R

Razazke

Guest
I recently downloaded the 2.3 beta, and have been trying to convert object data into structs. Here are some examples:

Player object Create event:
GML:
PlayerStruct = {
__player_life : 0,
__player_attack : 0,
__player_defense : 0,
__player_critChance : 0,
__PlayerInventory : Inventory_obj(),

setPlayerHealth : function(value)
{
    __player_life = value
}
};
//function Player_obj() constructor
//{
//__player_attack = 1;
//__player_life = 10;
//}
show_debug_message("We are in the game")
function getPlayerHealth()
{
    return __player_life;
}
Bullet object collide event

GML:
if(instance_exists(player))
{
    PlayerStruct.setPlayerHealth( getPlayerHealth())
    instance_destroy()
    if(getPlayerHealth<=0)
{
    instance_destroy(player)
    instance_destroy()
}
}
else
{
    instance_destroy();
}
Inventory script(because I reference it in Player)

GML:
Inventory = {
     size : 0,
Inventory: function() constructor
{
size +=1;
},
getSize : function()
{
    return size();
},

}

Compilation is fine, for the most part, and I can "fire" a bullet object, but when colliding with the wall, I receive an error of


Code:
___________________________________________
############################################################################################
ERROR in
action number 1
of  Step EventWall
for object bullet:

Variable bullet.PlayerStruct(100030, -2147483648) not set before reading it.
at gml_Object_bullet_Collision_Wall (line 5) -        PlayerStruct.setPlayerHealth( getPlayerHealth())
############################################################################################
gml_Object_bullet_Collision_Wall (line 5)

Can you not reference a struct like a typical class? Also tried to make a constructor in the struct, and that failed too, when trying to instantiate a new instance of the struct. Thanks for the help.
 
Last edited by a moderator:

Bart

WiseBart
The error message is quite clear in this case:
Code:
Variable bullet.PlayerStruct(100030, -2147483648) not set before reading it.
The message correctly tells you that the bullet object doesn't have an instance variable PlayerStruct, since you define it in the player's Create event.
You'll need to reference the PlayerStruct variable in the player instance.

Not sure if I understand the reason why you want to make things this complex in this situation, though.
Putting the player data in a struct and prefixing the variables with __player_ adds a layer of complexity that you don't seem to need here.

Hope this can clear things up a bit :)
 
R

Razazke

Guest
The error message is quite clear in this case:
Code:
Variable bullet.PlayerStruct(100030, -2147483648) not set before reading it.
The message correctly tells you that the bullet object doesn't have an instance variable PlayerStruct, since you define it in the player's Create event.
You'll need to reference the PlayerStruct variable in the player instance.

Not sure if I understand the reason why you want to make things this complex in this situation, though.
Putting the player data in a struct and prefixing the variables with __player_ adds a layer of complexity that you don't seem to need here.

Hope this can clear things up a bit :)
simply put...seperation of interface and implentation. Something that is seemingly absent here, as I can reference anything from anywhere. I come from the c family(mainly c++) and decided to learn this engine since it was recommended to me over unity for 2d development. a little tangent.Hopefully this clarifies things.
 

Roldy

Member
simply put...seperation of interface and implentation. Something that is seemingly absent here, as I can reference anything from anywhere. I come from the c family(mainly c++) and decided to learn this engine since it was recommended to me over unity for 2d development. a little tangent.Hopefully this clarifies things.
I think you are confused and just need to take a step back.

Can you not reference a struct like a typical class?
The question doesn't really make any sense.

Seeing as that is the only question you asked I'll try to help you by pointing out the many problems in your code.

GML:
if(instance_exists(player))
{

    PlayerStruct.setPlayerHealth( getPlayerHealth())      // PlayerStruct::setPlayerHealth is not static
                                                          // Are you sure you didn't mean player.setPlayerHealth(player.getPlayerHealth())?
                                                        // Additionally you are trying to set the health to what it already is?
                                                      
    instance_destroy()        // You are destroying bullet here

    if(getPlayerHealth<=0)     // Are you trying to call an function and forgot the ()?
                               // Did you mean if (player.getPlayerHealth()<=0)
    {

        instance_destroy(player)
        instance_destroy()      // You destroy bullet a second time needlessly
        
    }

}
Slow down. Think it through. Check your code.
 
R

Razazke

Guest
I think you are confused and just need to take a step back.



The question doesn't really make any sense.

Seeing as that is the only question you asked I'll try to help you by pointing out the many problems in your code.

GML:
if(instance_exists(player))
{

    PlayerStruct.setPlayerHealth( getPlayerHealth())      // PlayerStruct::setPlayerHealth is not static
                                                          // Are you sure you didn't mean player.setPlayerHealth(player.getPlayerHealth())?
                                                        // Additionally you are trying to set the health to what it already is?
                                                     
    instance_destroy()        // You are destroying bullet here

    if(getPlayerHealth<=0)     // Are you trying to call an function and forgot the ()?
                               // Did you mean if (player.getPlayerHealth()<=0)
    {

        instance_destroy(player)
        instance_destroy()      // You destroy bullet a second time needlessly
       
    }

}
Slow down. Think it through. Check your code.
the original idea was to take the health, subtract x from it and then set it inside the parameter. But seeing as though I couldn’t get it to run fully anyway, i just kinda left it.

do you reference the object that you operate on? Even though the methods are inside the struct, And all data members are public? At this point I am struggling to see the point of a struct, in this instance, if it is not treated like a c Family class. Yoyo talks a lot in their update notes about moving to a more “OOP” style, which includes things like encapsulation(which is prominent in structs) and obfusication. But, everywhere I look, I hear people talking about moving to a JS-style language, which is very different in how it handles data access. Maybe they’re planning on adding access limiters in the future?
 
Top