entity scripted name question

mtrain

New member
I want to make an entity, that will have a name consist of three words, that must be assigned by a script, in next way:

anim spawn
#----NAME
@script
void self = getlocalvar("self");
void n1;
void n2;
void n3;

n1 = "Dirt";
n2 = "Bag";
n3 = "Pete";

changeentityproperty(getlocalvar("self"), "name", n1+n2+n3);
@end_script
#----NAME
loop 0
offset 10 10
delay  10
        frame data/chars/misc/empty.gif

So in result entity must have name Dirt Bag Pete
 
I wrote a tutorial specifically on how to do this leveraging OpenBOR's text reading capabilities.

DC

Edit: Seems I misread - I thought you wanted to randomly assign the words to make different names. After a reread, I can't understand why you need script for this at all. Just make the name "Dirt_Bag_Pete" and be done with it.

DC
 
The problem is i dont know how to properly write only this part: n1+n2+n3
Because if i write changeentityproperty(getlocalvar("self"), "name", n1+n2+n3); openbor is crashing, if i write changeentityproperty(getlocalvar("self"), "name", "n1+n2+n3"); the name will be exactly n1+n2+n3. So how must i write that 3 variables together, to openbor can recognize them as three separated text words and show them together on screen. Not 1 random name, but 3 random words shown together.

DC, your method can allow me to make that?

Just make the name "Dirt_Bag_Pete" and be done with it.

I plan to make each of the word to be randomly chosen from list, in other words n1, n2 and n3 variables will be different every time, and they are three words, that will form a one long name, so i need to know is there possible to assign 3 words to be shown as name of the entity, how to make engine recognize them and do not crashing.
 
See my edit above.

Now if you must use script to change a name, there are three things to know.

[list type=decimal]
[*]To add spaces to a name, use an underscore. "Dirt_Bag_Pete"
[*]In a script anything enclosed in " " is exactly that. It's a literal string value, and that's how it's meant to be. Otherwise how could you have strings in the first place? Once you have assigned a string to a variable, you don't need to (and as you've discovered can't) enclose it in quotes for use.
[*]Lastly, you CAN add strings together, just again only enclosing the actual literal string values in quotes, not the variables.
[/list]

Put all that together, and here's the end result:

Code:
changeentityproperty(getlocalvar("self"), "name", n1 + "_" + n2 + "_" + n3);

I'm not a fan of doing work in function calls though - it's a bad habit to get into because it leads to messy code that is very difficult to reuse, modify or debug. Do something like this instead:

Code:
self = getlocalvar("self");

name = "Dirt";
name += "_Bag";
name += "_Pete";

changeentityproperty(self, "name", name);
 
mtrain said:

Glad to help, just remember my last tidbit above; you should really do all calculations first and then send the results to your function call (or whatever else). The engine doesn't care either way, but YOU will later on. Trust me.
 
Damon Caskey said:
mtrain said:

Glad to help, just remember my last tidbit above; you should really do all calculations first and then send the results to your function call (or whatever else). The engine doesn't care either way, but YOU will later on. Trust me.

I was going to ask you why but I think I got it. The original method uses 3 variables to archive this (n1,n2,n3), while you use just one (name).
 
That's not really it. Variables go away when the script finishes, and it's better to use too many than too few.

The problem is putting too many actions into one line.

Read this:
Code:
changeentityproperty(getlocalvar("self"), "name", n1 + "_" + n2 + "_" + n3);

Now read this:
Code:
self = getlocalvar("self");

name = "Dirt";
name += "_Bag";
name += "_Pete";

changeentityproperty(self, "name", name);

Sure the second version uses "more" code, but tell me which one do you think is easier to understand? Also just for the record, yes it will execute faster and use less memory than the first.

One serious mistake starting coders make is thinking less code = more efficient, but this is almost never the case. In fact it's usually the other way around.

Why? Computers don't "read" code - they compile it into a set of basic instructions. Those instructions are going to be identical for a given set of actions no matter how you write them into code. Ergo, it just makes sense to write in a way that keeps things more organized and readable.

It's not just readability and efficiency either. Think about modifying and reusing code. By keeping items blocked and organized, the second version can be reworked in a twinkling, where the first would take a bit more time. It all adds up quickly.

DC
 
ah yeah, there is that reason. Its like Mugen works too. People like to combine several triggers on the same line like:

trigger1 = !time && Power >1000 && StateType = A && Ctrl

The line in question checks it the time is zero (in fact, it triggers 1 tick before 0) AND the power is greater than 1000 AND the player is airbone AND it has control.

But is way better to do this:
Triggerall = Ctrl
trigger1 = Statype=A
trigger1 = Power > 1000
trigger1 = !time

Because the engine will check, first, if you have control (you can set the control on & off in mugen) because TRIGGERALL is a mandatory trigger which needs to be true, independent of what is bellow. If this isn't TRUE, Mugen won't execute anything more on that code block (but will parse it to check for errors)
 
Back
Top Bottom