Day of Defeat Plugins Community
This site is an archive — everything is readable and downloadable, nothing can be posted. Admin
Archive › Old Forum Threads › Help › Scripting Help › Array by reference
6 posts, 4 people · 1,419 views Thread Rating: ★★★★★
closedArray by reference
Started by h0_noMan on 04-01-2010, 11:50 PM
H h0_noMan
started this thread
Posts: 80
Threads: 11
Joined: Dec 2007
04-01-2010, 11:50 PM #1

Hi guys,rnrnI am working on a tricky plugin and i need to send to a function an array by reference. I tried many thins but never succeed, i always end on an error saying that a parameter cant be both an array and a reference.rnrnThanks for help.rnrnExemple :rn

// Just a Functionrnpublic doFunctionA()rn{rn	// Array instancern	new myArray[32];rn	rn	// Calling doFunctionB() with myArrayrn	doFunctionB(myArray);rn}rnrn// I want doFunctionB() to modifyrn// the previously created arrayrn// declared in doFunctionA()rn//rn// I dont want doFunctionB to creatern// a clone of myArrayrnpublic doFunctionB(&refArray[])rn{rn	format(refArray, 31 "It works %s", "xD");rn}
Vet
posted here
Posts: 229
Threads: 24
Joined: Oct 2006
04-02-2010, 03:26 AM #2

Once you leave doFunctionA, myArray[] no longer exists. Thus doFunctionB can't access it. Define myArray[32] globally to access it outside of doFunctionA.

H h0_noMan
started this thread
Posts: 80
Threads: 11
Joined: Dec 2007
04-02-2010, 10:25 AM #3

myArray[] still exist until doFunctionA() ends, but when i call doFunctionB(myArray), doFunctionA() wait for doFunctionB() to finish to continue its own instructions, so myArray[] is not destroyed when doFunctionA() call doFunctionB().rnrnHere is the proof that my code works with an integer :rnrn

// Just a Functionrnpublic doFunctionA()rn{rn    // Integer instancern    new myInteger = 0;   rnrn    // Calling doFunctionB() with myArrayrn    doFunctionB(myArray);rnrn    // Will display 100 and not 0rn    log_amx("Value of myInteger : %d", myInteger)rn}rnrn// doFunctionB() actually modifyrn// the previously created integer myIntegerrn// and change his value to 100rnpublic doFunctionB(&value)rn{rn    value = 100;rn}
rnrnI just want to know how to do it with an array.rnrnThanks.

Dr.G
posted here
Posts: 776
Threads: 75
Joined: Nov 2007
04-02-2010, 06:04 PM #4
#include <amxmodx>rn#include <amxmisc>rnrn#define PLUGIN "New Plug-In"rn#define VERSION "1.0 by Dr.G"rn#define AUTHOR "AMXX DoD Team"rnrnrnpublic plugin_init() rn{rn	register_plugin(PLUGIN, VERSION, AUTHOR)rn	rn	register_clcmd("say !ding", "GetName")rn}rnpublic GetName(id)rn{rn	new iName[32]rn	get_user_name(id, iName, sizeof iName - 1)rn	rn	PassNameToPrint(id, iName)rn	rn	returnrn}rnpublic PassNameToPrint(const id/*int*/, const ClientName[]/*string*/)rn{rn	if(id)rn	{rn		client_print(id, 3, "My Name is : %s", ClientName)rn		returnrn	}rn	rn	elsern		client_print(0, 3, "My Name is : %s", ClientName)rn	rn	return	rn}
H h0_noMan
started this thread
Posts: 80
Threads: 11
Joined: Dec 2007
04-02-2010, 06:59 PM #5

That is not what i want.rnrnYou just send an array as parameter.rnrnYour function PassNameToPrint() create a new array (ClientName[]) and put the content of iName[32] into it.rnI dont want to create a new array but to use the previously created array.rnrnI want to send a pointer to an array (or a reference) as a parameter, NOT a simple array.rnMy doFunctionB() will modify the array created in doFunctionA() without creating a new array because it use the pointer and can modify it.rnrnTake a look at my exemple code, the & means reference.rnrnEDIT : I found how to do it.rnrn

public doFunctionA()rn{rn	new message[64]rn	doFunctionB(message, 63)rnrn	log_amx("Message : %s", message)rn}rnrn// This function will change the value of message[]rn// which was created in doFunctionA()rnpublic doFunctionB(data[0], size)rn{rn	format(data, size, "Pi : %d.%d", 3, 14159)rn}rn
F Fysiks
posted here
Posts: 108
Threads: 3
Joined: Aug 2007
05-11-2010, 12:38 AM #6

All arrays are passed by reference by default. In fact, I don't think you can do it any other way.nnIn your function you only need to donn

function(arrayname[], size)
Archive › Old Forum Threads › Help › Scripting Help › Array by reference
DoD Plugins Archive · Repository · Old Forum Threads · Maps · Admin · built 14 Sep 2026 00:34 UTC