Posted: 4th Feb 2024 19:40
Hello everyone, please tell me if there is a way to pass the function name to another function as a parameter. To have another function accept this function and run it?

I wrote a small library for myself that creates dialog boxes. I need to be able to easily process the buttons in the dialog box and launch the desired function. I'll give you an example:


+ Code Snippet
Function WinMenuYesNo(text$, FUNCTION_NAME)
	// Function text
	do
		if GetVirtualButtonReleased(98)=1
			FUNCTION_NAME( )
		endif
		if GetVirtualButtonReleased(99)=1
			exit
		endif
		Sync()
	loop	
EndFunction

// Here we are calling my function from the library, is it possible to do this or is there another way?
text$ = "Should I put an item in my backpack?"
WinMenuYesNo(text$, FUNCTION_NAME)

// I want to create a window so that when you click on the YES button, a function will start that will put the item in the backpack. I want to pass this function as a parameter
Posted: 4th Feb 2024 20:51
Unfortunately that, as you have described, is not possible
Posted: 5th Feb 2024 20:55
You'd have to do something like this

+ Code Snippet
Function WinMenuYesNo(text$, FUNCTION_NAME as string)
    // Function text
    do
        if GetVirtualButtonReleased(98)=1
			callFunction(FUNCTION_NAME)
        endif
        if GetVirtualButtonReleased(99)=1
            exit
        endif
        Sync()
    loop   
EndFunction


function callFunction(func$)
	select func$
		case 'myfunc':
			myfunc()
		endcase
		case 'doThing':
			doThing()
		endcase
	endselect
	
endfunction
Posted: 6th Feb 2024 14:48
Function Pointers / Objects aren't supposed to exist in AppGameKit Teir 1...
They do, but it's a bug and their behaviours inconsistent.

Now with this said, the scenarios for needing a Function Pointer are quite rare (esp. in C++ with virtualisation as a built-in aspect).
Your own example really is unnecessary.

Personally, and I say this as I have written my own UI Library...
I would use a Common Window., which uses Binary Flags to determine the "Common" Buttons to use.
These would be ABORT, CANCEL, RETRY, YES, NO, OK, CONTINE, IGNORE

And as there are only a few different variants you'll ever _actually_ use these could themselves be defined as Flags
ABORTRETRYIGNORE
CANCELTRYCONTINUE
HELP
OK
OKCANCEL
RETRYCANCEL
YESNO
YESNOCANCEL

As none of these buttons are used / defined twice., you pre-define, create and load the resources as part of the API then simply have them "Offscreen" or "Not Visible" unless they're in usage.
If you need help with Binary Flags., check the Code Snippets Forum as I have a fairly comprehensive Bit Operation sample on there.
Posted: 11th Mar 2024 17:40
As others have mentioned, this can't be done in Tier 1. I wish it could be done as I have a generic state machine with states that can call a function every frame.

It would probably be best to do something like this in your case:

+ Code Snippet
Function WinMenuYesNo(text$)
    // Function text
    do
        if GetVirtualButtonReleased(98)=1
            ExitFunction 1
        endif
        if GetVirtualButtonReleased(99)=1
            exit
        endif
        Sync()
    loop  
EndFunction 0

// Here we are calling my function from the library, is it possible to do this or is there another way?
text$ = "Should I put an item in my backpack?"
If WinMenuYesNo(text$)
  // call function
endif