Python References, want to get what SOL points to

For questions about using Classic.

Post » Thu Nov 08, 2012 2:27 pm

I was wondering if anyone could help, I have condensed my problem down into like just the smallest reproduction of the issue, if this works it will be so epic i will finally never loose those blasted sprites.

System.Create(Sprite, 2, xLocation, yLocation) #so we create a first sprite

RefToFirstSprite = SOL.Sprite #we want to save some ref to it, this however saves a link to Sol.Sprite not the target of it which i want

System.Create(Sprite, 2, xLocation, yLocation) #in creating this next sprite whatever sol points to somehow is updated

RefToFirstSprite.Destroy() #and this ends up therefore killing the new target of sol, not the first sprite i wanted to get rid of

I have racked my brains on this for seems like hours now, I feel at least satisfied I think I have identified my problem quite well, would be really greatfull if anyone could shed any light on what's happening here... i sort of have this impression I'm missing something fundamental
Posts: 31
Reputation: 412

Post » Thu Nov 08, 2012 5:17 pm

#This is how you save a object refrence
System.Create("Sprite", 1, 200,200)
RefToFirstSprite = SOL.Sprite[0]

#Then you can destroy it later like so.
Posts: 4,667
Reputation: 37,199

Post » Thu Nov 08, 2012 7:13 pm

wow, seriously thanks, i was really loosing it on that one. This is epic, no sprite will now be lost I was really being held back by that contemplating crazy workarounds

that was clearly a rather fundamental python concept I wasn't getting i can't thank you enoughDarkShroom2012-11-08 19:14:52
Posts: 31
Reputation: 412

Post » Tue Nov 13, 2012 8:12 pm

i wanted to share the code that has evolved from your advice:

def Spawn(sSpawntype, x, y, iSpawnLayer):
     System.Create(sSpawntype, iSpawnLayer, x , y )
     cCodeToExec = "ref = SOL." + sSpawntype + "[0]"
     exec cCodeToExec
     return ref

Then if you want to spawn something and never loose it:

EvilBadGuy1 = Spawn(sSpawntype, x, y, iSpawnLayer)


Thanks so much the possibilities are endless with this i can stream the tiles in and out as you walk about etc, awesome.
When I found Python could build and run strings well I just knew I would be I was in love with this xD

DarkShroom2012-11-13 20:12:42
Posts: 31
Reputation: 412

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests