Where should I declare variable with good performance
#1

I always create a variable and work like solution 1, but when I read some documents, it said I should use solution 2. Question 1: Which solution I should use ?
Solution 1:
Код:
CMD:increaseCountNumber(playerid, params[]) {
	new int: count = 1;
	count += 1;
}
Solution 2:
Код:
new int: count = 1;

CMD:increaseCountNumber(playerid, params[]) {
	count += 1;
}
Question 2: I use open bracket like this, is it seem be ok ?
Код:
CMD:increaseCountNumber(playerid, params[]) {
}

forward goForward() {
}

pubic goForward() {
}

stock outOfStock() {
}

if isAMoto(vehicleid) {
	println("this is a bike");
} else if isABike(vehicleid) {
	println("this is a bike");
} else {
	println("this is a plane");
}
Reply
#2

The first one is a local variable. So it will not be existing outside of the function you declared it (or before it).
So, if you need the value to be kept globally, there's no point of declaring it locally anyway..

If you only need it inside one function, declare it locally. Again, there's no point in creating a global variable for a local-only purpose.

There's also a minimal speed difference between local and global. But for single variables, this shouldn't be of any concern. You shouldn't worry about that if you don't know the purpose of local and global variables.


When forwarding a function, you don't need brackets for that statement. This is what it should look like:

Код:
forward myfunction(parameter);
public myfunction(parameter)
{
// code
}
Reply
#3

1. The first solution is called local variable, Which means it can only be accessed within the function it is declared into only and then it is removed from memory.
The second solution is called global variable, Which as the name suggests it can be accessed anywhere inside your script and it always has a place allocated in memory.

When to use? It depends on your use case, For example a variable that keeps track of player kills then that must be global because we want to access it anywhere and it needs to keep counting the kills,.

But there is cases where you can use both without a problem, Then local variables is more optimized because it is removed after you finish using it so it frees space in the memory.
---------
2. Sure why not.
Reply
#4

#NaS #FailerZ
I have understand all of you said. Thank you so much !!
Reply
#5

By the way you're using java's stuff like println.. Declaring an integer... You can just use new count = 1;
Reply
#6

Quote:
Originally Posted by TheToretto
Посмотреть сообщение
By the way you're using java's stuff like println.. Declaring an integer... You can just use new count = 1;
I just write a demo code without syntax testing, don't worry about that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)