[Include] Confirmation Dialog
#1

Confirmation Dialog
Introduction
I haven't posted a work of mine in this section for quite a while. A couple months ago, when I was putting confirmation dialogs across the game mode, I didn't want to do them all manually but instead I made a dynamic system for easy implementation. Simply said, you're able to state your question and pass necessary information in a single line with easy call-back processing later on. This script is a little advanced (while its usage is fairly basic) so if you don't understand parts of it feel free to ask! Here's an example usage that should speak for itself:

Syntax: ConfirmDialog(playerid, caption[], info[], callback[], ...);

Example: ConfirmDialog(playerid, "Confirmation", "Are you sure you want to purchase a Deagle for $5000?", "OnPlayerPurchaseWeapon", WEAPON_DEAGLE, AMMO, PRICE);

Example Processing:
Код:
public OnPlayerPurchaseWeapon(playerid, response, weaponid, ammo, price){
	if(response){
		GivePlayerMoney(playerid, -price);
		GivePlayerWeapon(playerid, weaponid, ammo);
		SendClientMessage(playerid, -1, "You bought a gun!");
	}else{
		SendClientMessage(playerid, -1, "You cancelled the purchase!");
	}
}
You can read detailed documentation in the source file.

Dependencies
  • sscanf
  • DIALOG_CONFIRM_SYS dialog ID defined
  • OnDialogResponse ->
    Код:
    if(dialogid == DIALOG_CONFIRM_SYS){
    	ConfirmDialog_Response(playerid, response);
    	return 1;
    }
Source Code
Код:
/*
	Created by Mmartin, 2014. Free to use.

	Confirmation dialogs:
		"Are you sure?" made simple, without unnecessary additional dialog ID's
		
	How does this work:
		Syntax: 	ConfirmDialog(playerid, caption[], info[], callback[], ...);
		Example:
			ConfirmDialog(playerid, "Confirmation", "Are you sure you want to purchase a Deagle for $5000?", 
				"OnPlayerPurchaseWeapon", WEAPON_DEAGLE, AMMO, PRICE);
		Example explanation:
			playerid		- playerid that needs something confirmed
			"Confirmation"	- Title / caption of the dialog window
			"Are you su..."	- Content / info / text of the dialog window
			"OnPlayerPu..."	- Callback that is called after answering the dialog
			WEAPON_DEAGLE	- Additional argument to be passed
			AMMO			- Additional argument to be passed
			PRICE			- Additional argument to be passed
		
		^	The above will call the callback specified when answered. The callback has
			two default arguments, "playerid" and "response". Then, the rest of your
			additional arguments follow. Example callback for the ConfirmDialog used
			in example:
			
			public OnPlayerPurchaseWeapon(playerid, response, weaponid, ammo, price){
				if(response){
					GivePlayerMoney(playerid, -price);
					GivePlayerWeapon(playerid, weaponid, ammo);
					SendClientMessage(playerid, -1, "You bought a gun!");
				}else{
					SendClientMessage(playerid, -1, "You cancelled the purchase!");
				}
			}
	
	[ ! ] NOTE: Right now, the additional arguments only work for integer values
*/

stock ConfirmDialog(playerid, caption[], info[], callback[], ...){
	new n = numargs(), 		// number of arguments, static + optional
		szParamHash[64];	// variable where the passed arguments will be stored
	for(new arg = 4; arg < n; arg++){	// loop all additional arguments
		format(szParamHash, sizeof(szParamHash), "%s%d|", szParamHash, getarg(arg)); // store them in szParamHash
	}
	SetPVarInt(playerid, "confDialogArgs", n -4);			// store the amount of additional arguments
	SetPVarString(playerid, "confDialCallback", callback);	// store the callback that needs to be called after response
	SetPVarString(playerid, "confDialog_arg", szParamHash);	// store the additional arguments
	
	ShowPlayerDialog(playerid, DIALOG_CONFIRM_SYS, DIALOG_STYLE_MSGBOX, caption, info, "Yes", "No"); // display the dialog message itself
	
	return;
}

stock ConfirmDialog_Response(playerid, response){
	new szCallback[33],		// variable to fetch our callback to
		szParamHash[64], 	// variable to check raw compressed argument string
		n,					// variable to fetch the amount of additional arguments
		szForm[12];			// variable to generate the CallLocalFunction() "format" argument
		
	n = GetPVarInt(playerid, "confDialogArgs");	// Fetch the amount of additional arguments
	GetPVarString(playerid, "confDialCallback", szCallback, sizeof(szCallback));	// fetch the callback
	GetPVarString(playerid, "confDialog_arg", szParamHash, sizeof(szParamHash));	// fetch the raw compressed additional arguments
	
	new hashDecoded[12];	// variable to store extracted additional arguments from the ConfirmDialog() generated string
	
	sscanf(szParamHash, "p<|>A<d>(0)[12]", hashDecoded);	// extraction of the additional arguments
	
	new args, 	// amount of cells passed to CallLocalFunction
		addr, 	// pointer address variable for later use
		i;		// i
		
	format(szForm, sizeof(szForm), "dd");	// static parameters for the callback, "playerid" and "response"
	
	#emit ADDR.pri hashDecoded	// get pointer address of the extracted additional arguments
	#emit STOR.S.pri addr		// store the pointer address in variable 'addr'
	if(n){	// if there's any additional arguments
		for(i = addr + ((n-1) * 4); i >= addr; i-=4){ // loops all additional arguments by their addresses
			format(szForm, sizeof(szForm), "%sd", szForm); // adds an aditional specifier to the "format" parameter of CallLocalFunction
			#emit load.s.pri i	// load the argument at the current address
			#emit push.pri		// push it to the CallLocalFunction argument list
			args+=4;			// increase used cell number by 4
		}
	}
	
	
	args+=16;	// preserve 4 more arguments for CallLocalFunction (16 cause 4 args by 4 cells (4*4))
	
	#emit ADDR.pri response				// fetch "response" pointer address to the primary buffer
	#emit push.pri						// push it to the argument list
	
	#emit ADDR.pri playerid				// fetch "playerid" pointer address to the primary buffer
	#emit push.pri						// push it to the argument list
	
	#emit push.adr szForm				// push the szForm ("format") to the argument list by its referenced address
	#emit push.adr szCallback			// push the szCallback (custom callback) to the argument list by its referenced address
	#emit push.s args					// push the amount of arguments
	#emit sysreq.c CallLocalFunction	// call the function
	
	// Clear used data
	#emit LCTRL 4
	#emit LOAD.S.ALT args
	#emit ADD.C 4
	#emit ADD
	#emit SCTRL 4
	
	// Clear used PVars
	DeletePVar(playerid, "confDialCallback");
	DeletePVar(playerid, "confDialog_arg");
	DeletePVar(playerid, "confDialogArgs");
	
	return;
}
As of right now, the script only allows integer parameters to be passed. This may change in the future whenever I find an efficient way to handle various variable types.

If you've any questions or concerns feel free to ask!
Reply
#2

Cool
Reply
#3

Damn, that's some good shit
Reply
#4

wow Very nices includes
Reply
#5

Well nice, but one question, why you used Pvar?
Reply
#6

Interesting, good job mate, i will test it soon!
Reply
#7

Good job mate
Reply
#8

Quote:
Originally Posted by Sanady
Посмотреть сообщение
Well nice, but one question, why you used Pvar?
I agree. You should create a PVar-free version of this script.
Reply
#9

WoW That's Cool Man
Reply
#10

y_dialogs and y_inline solve my problem in that case.

Anyway nice work.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)