Weird problem with structs or just me?

Started by Creator, Tue 29/10/2013 13:26:16

Previous topic - Next topic

Creator

I've just found that if I make a structure and only have a function in it:
Code: AGS

//script header

struct thing
{
  import void Thisfuction();
};

import thing mystruct;


and the code in the .asc file being:

Code: AGS

thing mystruct;

void thing::Thisfuction()
{
  QuitGame(0);
}

export mystruct;


and try and use it:

Code: AGS

mystruct.Thisfunction();


I get the "Script link failed: Runtime error: unresolved import mystruct

However, if I add a variable to my struct, in this case, an integer:

Code: AGS

struct thing
{
  int whatever;

  import void Thisfuction();
};


and change nothing else, the error doesn't happen.

Bug or is there something I don't know about?

Khris

I can't confirm this, but I'm getting a null pointer error when I try this (during run time). When I add a variable, the code runs fine.

Crimson Wizard

Might be a bug, can't test right now.

Here's a tip, though: if you are not adding any variables to struct, then declare all functions as "static" and use the struct without creating actual object, i.e.:

Code: ags

struct thing
{
  import static void Thisfuction();
};

thing.Thisfuction();

Creator

#3
Quote from: Crimson Wizard on Tue 29/10/2013 15:28:52
Here's a tip, though: if you are not adding any variables to struct, then declare all functions as "static" and use the struct without creating actual object

Thanks for that. Works fine. However, I don't really know why someone would use that instead of just using normal functions unless they want to put a group of certain functions together.

For clarification (in case I get the "well, the reason you were using it is why" :-D), I was doing it just as an experiment. I generally have variables as well as functions when I create structs, so I've never run into this before.

monkey0506

Well coming at that question from the opposite angle, why would someone define member functions if the instances have no members on which to function?

Creator

#5
Quote from: monkey_05_06 on Wed 30/10/2013 06:10:43
Well coming at that question from the opposite angle, why would someone define member functions if the instances have no members on which to function?

Assuming I understood that right, I thought the exact same thing. I mean, maybe you could make a wallet for example only using functions within a structure.

For example:

Code: AGS

//Some kind of wallet

struct wallet
{
  import static void addMoney(int money);
  import static void loseMoney(int money);
  import static void getStolen();
};

//Then you could use:
wallet.addMoney(20); //and so on.


But it would be much simpler (or more readable, at least) to have:

Code: AGS

struct cashCarrier
{
  int money;

  import void getStolen();
};

cashCarrier wallet;

wallet.money += 20;


So, without creating an object out of the structure, there's no real point in having one.

Crimson Wizard

#6
Quote from: Creator on Wed 30/10/2013 08:06:18
So, without creating an object out of the structure, there's no real point in having one.
No, there is of course a point of having a structure without making objects, which you named few posts above: to group related functions. This is a quite common practice in programming.
Also, when you want to allow strictly one object of that type.
AGS has a number of built-in structs that has only static members, like Game, or System.

The question is, do you need to have any data in structure, applicable per individual object, or not. If not, then there's no sense in making objects of that structure, neither have non-static functions.

monkey0506

Quote from: Creator on Wed 30/10/2013 08:06:18
Code: AGS

//Some kind of wallet

struct wallet
{
  import static void addMoney(int money);
  import static void loseMoney(int money);
  import static void getStolen();
};

//Then you could use:
wallet.addMoney(20); //and so on.

If you called wallet.addMoney, where would you be storing the value? Member functions would typically be used in a situation where you want to be able to handle how the data (stored in the members) is mutated. A typical layout would have a protected member to hold the data and exposed functions for accessing said member.

Quote from: Crimson Wizard on Wed 30/10/2013 08:18:08The question is, do you need to have any data in structure, applicable per individual object, or not. If not, then there's no sense in making objects of that structure, neither have non-static functions.

Exactly. Static structures are great at keeping related functions grouped together. If you're not storing anything on a per-instance basis, then making instances doesn't have any useful purpose though.

SMF spam blocked by CleanTalk