ID:1609948
 
(See the best response by Stephen001.)
Whenever you refer to a variable who does it belong to by default? The src, usr or neither? Is it possible to create a verb that edits the default value of a variable for everyone permanently? A way to turn a tmp variable into a regular variable through a verb(Not that I need to do this, was just curious)?

Something a little off topic, can you create a verb that creates verbs(completely create one from scratch, not create one from a precoded placeholder)?
Note: My code examples will be language-general, to promote learning versus copy-pasting of code.

Variables are owned dependent on the scope they are defined in. For example, if you have a class with their own variables, then they are owned by whatever that class instance is:

class Owner {
var my_variable;
}


As well, a function can have a local scoped variable, that is untouchable from a global source:

class Owner {
var my_variable;

function myFunction() {
var my_function_local_variable;
}
}


There are also application-specific global variables that DM introduces, which you can just declare:

var global_variable;

class Owner {
var my_variable;

function myFunction() {
var my_function_local_variable;
}
}


If you want more understanding of how variables and their scopes function, see: https://en.wikipedia.org/wiki/Scope_(computer_science).

I can't think of any examples or languages that allow an example of changing default values at run-time, but there are certainly hacks around this. One is really ugly, but you may be able to get away with using static variables to be able to modify at runtime for all new classes. Something like this might work:

var global_variable;

class Owner {
var my_variable;
var static default_value = 1;

function myFunction() {
var my_function_local_variable;
}

static function setDefaultValue(var default_value) {
this.default_value = default_value;
}
}

function changeOwnerDefault() {
Owner sample_owner = new Owner();
sample_owner.setDefaultValue(2); // changes default_value to 2.
}


^ This isn't entirely permanent, as it can always be manually changed later, but you certainly have control over how it gets changed. If you want it to only be changed once and never again, this will certainly work.

A way to turn a tmp variable into a regular variable through a verb(Not that I need to do this, was just curious)?

This isn't really something that can be done (unless I'm completely missing the point of what you are looking for), but I'm sure you can hack it in some way. A potential example may be something like:

class Owner {
var list/former_temp_variables;

Owner() {
former_temp_variables = new list();
}

sample() {
TempVariable new_temp_var = new TempVariable("name", "value");
former_temp_variables.Add(new_temp_var);
}
}

class TempVariable {
var name;
var value;

TempVariable(var name, var value) {
this.name = name;
this.value = value;
}
}


However, while I suppose the above is possible, I don't see why you'd ever need it.

Something a little off topic, can you create a verb that creates verbs(completely create one from scratch, not create one from a precoded placeholder)?

Really, no. Because you can't compile at run-time, you'd have an immensely difficult time creating the flexibility of an actual verb and then compiling. I mean, if you wanted to spend several hours creating a mini-compiler within your own game, I suppose it would be possible, but you'd be better off configuring a system within the game for custom actions and tailoring it to what your needs would be.

Edit: Though not very related, if you were creative enough, you might be able to get something useful from Kaiochao's response to my question on this thread. This may at least help in a bit of developing more dynamic functions, where argument sizes and functions aren't necessarily known.
Best response
I'm gonna break this down, because this is like ... 6 posts in one right here.

Gluscap wrote:
Whenever you refer to a variable who does it belong to by default? The src, usr or neither?

As Insomniaddikt has explained quite well, it depends on scope and context.

var/varA = 0

Test_Datum
var/varB = 1

proc/test_proc()
var/varC = 2
varA += varB * varC * varD // Calling context is a procedure on Test_Datum, so anything that isn't in local
// context (like varC) is assumed to be on src (like varB). If it's not there, it must
// be global (varA), failing that it's undefined (varD)


Is it possible to create a verb that edits the default value of a variable for everyone permanently?

The short answer is yes. The long answer is yes, but you need to consider what you mean by 'everyone' and what you mean by 'permanently' in order to actually create such a verb. If 'everyone' is mobs associated with clients in the world, loop on clients. If permanently means it's persisted to savefile, remember to save each client mob to savefile accordingly. If everyone includes offline people, you need to go trawling and loading mobs from savefiles, then writing them back. All doable quite comfortably in DM.

A way to turn a tmp variable into a regular variable through a verb(Not that I need to do this, was just curious)?

Well, the distinction is whether it's saved or not. You can put said tmp variable's value into a savefile yourself via a verb, and change your savefile loading code to test for and sets the value if it's present, yep.

It probably just shouldn't be a tmp variable in the first place though.


Something a little off topic, can you create a verb that creates verbs(completely create one from scratch, not create one from a precoded placeholder)?

Provided you are happy to have a world reboot, trusted mode, access to the full source code, and suitable access to dm.exe or DreamMaker, yup, sure.

Is it smart to do? Nooooooooooooooooooo.


In response to Stephen001
Your code example for scope is what I had in my head, and just for some reason could not write out. :P Thanks for helping clarify on my explanation.
Thank you very much for the info you two, sorry for the barrage of questions. I was worrying about forgetting to preffix some vars with "src." and it started a chain of thought that raised some questions.