Adding something to a world inside of a global function


(Noah) #1

How would I go about adding an entity to a world from inside a global function? Here’s the function I’m trying to add the entity with in case that helps.

public static function updateEnemies()
	{
		if(!BattleMenu.enemy1 == 'void')
		{
			var enemyCheck = BattleMenu.enemy1;
			add (new VisualTile(0,0,enemyCheck));
		}
	}

(Jacob Albano) #2

Why do you think this needs to be in a static function?


(Noah) #3

Oh man. I was worried this may happen. I really hope this explanation doesn’t become really long winded.

The game I am working on is an RPG and I have two separate worlds right now, one to contain movement on the field and another for where the battles occur. When a variable called battleSteps, which decreases whenever the Player is moving, reaches zero, it loads the battle world. In the battle world I have 2 primary classes. I of course have the world itself, but I also have a class called BattleMenu that not only stores all information about the enemies and then relays them to the BattleScreen world but also contains all the graphics for it, including the text and the menu itself.

When battlesSteps reaches 0 it calls the static function beginBattle which is stored inside the class BattleMenu. When this is called a random enemy is chosen based on which area the player is in and then is stored in 1-3 variables located in there as well, those being enemy1, enemy2, and enemy3. After that it calls the function updateEnemies which tells the BattleScreen world to add graphics for all the enemies that were called for by the beginBattle function.

I hope that answers your question.


(Martí Angelats i Ribera) #4

It doesn’t make sanse to me. I wouldn’t use this method. I think it’s a best option going for parent-child one:

BattleMenu have a BattleScreen saved as a variable. When making a new BattleScreen pass the BattleMenu as a parameter and save that in the BattleScreen object. Then you can make them call each other (this is independent of FP.world wich you can select the one wich updates and renders).

It’s hard to explain. Basically they have each other in variables wich allows them to call non-static functions and variables.

PD: If you want i can have a look at the code.

PD2: Avoid using static. Only use them if they are 100% necessary.


(Jacob Albano) #5

I really feel like you’re shooting yourself in the foot by doing this in a static function, but I’ll let it pass. Can you pass the world as a parameter to the function? You can access the current world through FP.world, but that can be problematic in a number of edge cases so I’d recommend against it if at all possible.


(Martí Angelats i Ribera) #6

Yes you can. In fact you can use it as a parameter of the constructor, thing that allows you to create a world and then return to the other (for instance go to a battle and go back to the map).


(rostok) #7

@Zazorah maybe I didn’t get the whole idea. But can’t you just add and remove entities between worlds? In particular you could have single BattleScreen entity that would be added to the world every time it is active - just overload begin() method.


(Noah) #8

I managed to solve the problem after reading some of your suggestions. Thank you so much for all the help you guys give. :smile: This has to be one of the most friendly communities on the internet.