Any idea how I might get an object reference?

jorotroid

Member
Basically I want to create an object, store a reference to it, then do something to the object at a later point in time. I'm not seeing an obvious way of doing it at the moment. Anyone who is more familiar with the code base have any ideas?

Example uses:
-A remote bomb. that gets place with one button press, then explodes on the next button press.
-An monster that moves towards an non-player object, or an object that follows a monster.
 

jorotroid

Member
Thought I would share my progress on my problem. I got the logic around my remote bomb to work, but still not sure how to handle keeping track of the reference to the object after placing it. Right now it explodes what ever object is in the second object slot which is fine when the player and the bomb are the only objects on screen, but if there is an enemy already there, the enemy will explode instead. Here are a series of screenshots showing all that in action.

PhqoPgM.png

MGbrsiI.png

PZmNcVz.png

Kl72DLF.png

4IdOc3F.png

d2oxdEu.png


I'm think that it would be easier to have a bomb on a fuse because it would just require setting up two action steps: a timer and one for the detonation logic. As I type that I'm thinking that maybe I could still put the detonation logic in a AI script assuming I can put input reading inside the an AI script. Maybe that would work.
 

MistSonata

Moderator
Using input reads in the AI script might be unreliable. I'd recommend something else...

Go to UserVariables.asm and create a bomb variable somewhere (doesn't matter where really, just make sure it's on its own line). Like so:

Code:
     bomb_object        .dsb 1

Then, after the code where you create your bomb object, store the X register into your bomb_object variable.

Code:
CreateObject blah, blah, blah, blah
STX bomb_object

And then instead of detonating the object in the second slot, you can tell it to explode the bomb_object and it should explode the right object.

Something to keep in mind, though... this will only work for one bomb at a time, so you'll want to make a check in your "create bomb" code to make sure you don't create more than one.
 

jorotroid

Member
Thanks, Mist. That the object reference ends up in X is exactly what I was looking for. Strange, when I was trying to figure it out, I though that it ended up in the A register and couldn't figure out why storing in a variable wasn't working. But now that you pointed it out I don't know how I didn't notice that. Thanks! The bomb pretty much works exactly how I want it to now. The only problem now is if I place the bomb in another screen and then walk to another screen without detonating it, then something else will explode in the new screen. But I think this should be an easy fix I just need to find what code get called when a screen transition happens and insert some code to flip the flag I am using to remember if a bomb has been placed.

Also a side note, I have notice that UserVariables.asm isn't a good place to put custom variables because NESMaker rewrites that file everytime you export. I made my self a MyVariables.asm file and included it after UserVariables.asm gets included.
 

jorotroid

Member
I found the spot to deactivate the bomb when leaving a screen. I put it in HandleScreenLoads.asm right after the screenIsCued label. I think I am done messing with the adventure stuff for now. Just eagerly awaiting the platformer stuff to see how I might adapt it for Witch City.
 

RadJunk

Administrator
Staff member
After seeing...

Code:
CreateObject blah, blah, blah, blah
STX bomb_object

I'm just so damn proud. I never even use the STY or STX opcodes! So seeing you give someone tips using them demonstrates your growing ASM prowess and not just cargo cult programming. Freaking awesome.

There's also a great new interface in the project settings where you can set up user variables. I'm not 100% sure it's fully implemented with the init values right now, but it should still create the variable...99% sure that's up and running, but I haven't given it its proper test yet.
 

chronosv2

New member
I can confirm the User Variables tab does work perfectly well. I use it in my Long Bars code. :D
Edit: Misread slightly. Init values. Well, I can go look at that now. (And I did, and cannot confirm either.)

This is a really cool concept. I'll have to keep an eye on it to see how it evolves!
 

dale_coop

Moderator
Staff member
The new variables system is so GREAT! thanks you guys. Used it to migrate some of my codes to the 4.0.11, and now no need to modify myself systemvariables.asm or initLoads.asm. And most important, it is linked/specific to the projects, now.
 
Top Bottom