Returning struct from a function [SOLVED]

Started by duckwizard, Wed 09/03/2011 21:18:13

Previous topic - Next topic

duckwizard

Is there any way to return a struct from a function?

MyStruct foo() { ... } gives me "Cannot return entire struct from function."
MyStruct* foo() { ... } works if I make MyStruct a managed type, but then I can't create a MyStruct.

What should I do?

duckwizard

Edit - found this thread http://www.adventuregamestudio.co.uk/yabb/index.php?topic=13458.msg162111#msg162111 after a lot of googling.

Answer: Nope, can't do it.  Have to use a global variable (ugh)

monkey0506

Well, what are you trying to do exactly? There are various ways of getting around this depending on your actual implementation and needs.

duckwizard

monkey_05_06:

See my new thread at http://www.adventuregamestudio.co.uk/yabb/index.php?topic=43057.0 - basically I was trying work out a system similar to this before I realized a plugin could do an even cleaner job.  So I've given up on struct passing.  But FWIW, here's what I was trying to do.

I had an unhandled_event function that would figure out which type of thing was being clicked and which type of action.  Then it would grab the ID of that thing.  Then it would bitwise-pack those three pieces of data into an int so it could be passed to on_call using CallRoomScript. 

Then I wanted a function that could be called from on_call in the room script which would unpack the data into a struct, setting up character/object/invitem references and return the struct for use in on_call.  So your on_call would look like this:

Code: ags

//room1.asc
function on_call(param)
{
	UnhandledEvent e = UnpackUE(param);	//this would unpack the param into an UnhandledEvent struct if it had gone to plan
	
	if(e.targetCharacter == cChrisJones && e.action == eaTalk)
	{
		cChrisJones.Say("You're in room 1!");
	}
	else if(e.targetObject == oAwesomeThing && e.action == eaInteract)
	{
		cChrisJones.Say("Hey! Don't take my Awesome Thing!");
	}
	//etc
}


Anyway, I realized the plugin API is simple enough and if I can solve the problem in my other thread then I'll be golden.  Thanks for the quick reply, though!

duckwizard

BTW, I finished implementing my plan with a global struct variable.  Ugly, but it worked great.  Still, the plugin method would be far cleaner.  In fact, maybe someone has already made a plugin like this - I tried to find evidence of that but couldn't.

monkey0506

If you've already written the method to "pack" the data into a single integer and a method to "unpack" said variable, why do you need to return the struct from the function at all? You are aware that you can have member functions in AGS structs, right? You could do:

Code: ags
struct UnhandledEvent
{
  // ..your definition of the structure..
  import int Pack();
  import bool Unpack(int);
};

int UnhandledEvent::Pack()
{
  // ..pack the existing data in this struct instance into an int..
  // you can use the 'this' keyword here to do this
}

bool UnhandledEvent::Unpack(int data)
{
  // ..unpack the data into this struct instance..
  // again, use the 'this' keyword here
}


Then you would simply use:

Code: ags
function on_call(int param)
{
  UnhandledEvent e;
  e.Unpack(param);
  // ...
}

duckwizard

I did know about member functions, but that didn't occur to me.  It's very clever.  Thanks!

SMF spam blocked by CleanTalk