Create function inside another function
BlitzMax Forums/BlitzMax Beginners Area/Create function inside another function
| ||
Note: Health is gained from Entity A - This works Local Enemy1:TEnemy = TEnemy.Create() Print Enemy1.Health B - This does not Function EnemySpawn() Local Enemy1:TEnemy = TEnemy.Create() End Function EnemySpawn() Print Enemy1.Health Why can't I do B? |
| ||
Because enemy1 is local - it only exists inside the EnemySpawn funcition. You should do: Function EnemySpan:TEnemy() Local enemy1:TEnemy = TEnemy.Create() return enemy1 End Function Local newEnemy:TEnemy = EnemySpawn() Print newEnemy.Health Or even Print (EnemySpawn()).Health |
| ||
Creating the spawn outside of the Function would defeat the purpose of the Function in the first place though. I'm trying to write a function that will spawn 1-16 enemies and assign them properties that can be accessed outside of the function. I mean.. I can do it the ugly way, but trying to be nice and clean here. Your post gave me a couple ideas though, so thanks for that. Last edited 2012 Last edited 2012 |
| ||
this is easy... you can add them to a list, where all members of the type enemy are collected:Global List:TList=New TList Type TEnemy Field Health% End Type EnemySpawn() For Local locEnemy:TEnemy = EachIn List Print locEnemy.Health Next End Function EnemySpawn() Local Enemy1:TEnemy = New TEnemy ' Local Enemy1:TEnemy = TEnemy.Create() Enemy1.Health=123 List.AddLast Enemy1 End Function |
| ||
Thank you again Midi I was thinking a bit too far outside the box there. OOP > me so far Thank you guys. |
| ||
Well. That helps me create the function, but how would you create a dynamic number from a random number of enemies without doing Enemy1, Enemy2, etc etc. Psuedo code: x = Rand(1,16) 'This would be my number of enemies I want if x = 1 Function EnemySpawn() Local Enemy1:TEnemy = New TEnemy ' Local Enemy1:TEnemy = TEnemy.Create() Enemy1.Health=123 List.AddLast Enemy1 End Function else if x = 2 ??? Would I have to create 16 versions of EnemySpawn() I'm not opposed to that, just making sure I'm doing it the best way. |
| ||
EnemySpawn() should create a local instance for a New TEnemy, then once initialized it properly, should add it to the global list. The local object reference is dropped once the scope goes out from the Function, but the global List will keep a reference to it until the object is explicitely destroyed. EDIT: It's what it actually does, so you don't need to change it. :-) After that, to access each Enemy you'll have to browse the list using the For..Each iterator. Try to do it yourself and You'll learn a lot of things. Enjoy your coding. :-) EDIT#2: Just like Midimaster suggested. This is officially my useless post ever in this forum! :-D Last edited 2012 |
| ||
Nice, thanks for the hints. I really do like figuring stuff out myself, but some concepts are a bit alien without direction. I think once I get list management down I will be over the first big hurdle of this project. |
| ||
Once mastered Scope and Objects' lifecycle concepts, You'll be Black Belt of Blitzmax. :-) This modified code could make the TList Object management clearer. Untested, as I haven't Blitzmax on the Office PC. :P Global List:TList=New TList Type TEnemy Field Health% Field ID:int End Type For e = 1 to 5 EnemySpawn(e) Next For Local locEnemy:TEnemy = EachIn List Print locEnemy.Health Print locEnemy.ID Next End Function EnemySpawn(e:int) Local Enemy1:TEnemy = New TEnemy Enemy1.Health=123 Enemy1.ID = e List.AddLast Enemy1 End Function |
| ||
I actually found some more help digging through the posts and making better use of Methods and understanding how they related to the object's timeline/lifespan/existence/everything and how I can inherit it from the base as a "function".... it's all becoming muuuuuch clearer. Corum, the enemy ID idea just might work better . I've done what I needed to, it's just very dirty , or at least I always feel like I'm taking the long way of doing it ;) |
| ||
I'd probably do it like this:Function createEnemies:TList(amount:int) local enemyList:TList = new TList for local i:int = 0 until amount list.addLast(new TEnemy) next return enemyList endFunction like that you could call createEnemies(10) to create 10 enemies.. you'd get a list you can iterate over. success, at one point things suddenly click and it'll be very easy I promise ;) have fun Last edited 2012 |