Log In  

Cart #49294 | 2018-02-15 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

Upload version to transfer to Chip Pocket. no updates

Cart #48906 | 2018-02-03 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

Hi guys

Note: very novice coder. sorry of the mess.

I'm trying to recreate a childhood Fav game called BeamRider and I could really use some help understanding how
"FOR X IN ALL(Array) DO" works.

I need two types of bullets 1. Laser (short ranged) and 2. Nuke (for hitting the mothership)

I've made the array (bullets) to hold both "B" for Laser and "N" for nuke. which I need to behave differently for each other but my (bullets,N) keep over overriding my (bullets,B).

PLUS my Nuke are been draw but to my understanding of coding (which is novice at best) they should not been draw yet as I only have a (FOR B IN ALL BULLETS) in my _draw function. RAGE!!!

Is what I'm trying to do posible or do I need to make a second array for my nukes?

P#48907 2018-02-02 20:50 ( Edited 2018-02-15 15:04)

:: Davbo

Hi there!
I'm a little confused by your description of the problem but I will try and answer anyway.
Firstly your termonology is a little confusing. You say you have 'the array bullets to hold B for Laser and N for nuke'. So you're using the word 'bullet' to refer to both a laser and a nuke...? So then referring to a laser as 'B' is confusing, is a nuke is still in a way a nuke. It would make more sense to have an array called bullets that holds L for Laser and N for nuke. Then it's clear what you are referring to when you say Bullet.

Moving from there, it seems in your latest version you have split both the lasers and the bullets into two different tables. This is fine! However to answer your original question it would be possible to have all your nukes and all your lasers in a bullet table but to have them do different things you would need to have a reference to them in a unique table as well.

Do you know about accessing variables by reference or by value? If not go google around for this info.

I just threw together this snippit as an example - copy it into your pico 8 and add some update info to both the laser and the nukes to get them to do different things. Then try going to the init function and adding more than one nuke and one laser to each table. Hope this helps!

--all bullets
bullets={}

--just the lasers
lasers={}

--just the nukes
nukes={}

function new_bullet(x,y,msg)
 b={}
 b.x=x
 b.y=y
 b.msg=msg
 --adds entry to the bullets table
 add(bullets,b)

 --returns ref to same table
 return b
end

function _init()
 --add a laser to both
 --bullets and lasers
 add(lasers,new_bullet(0,0,"l"))

 --add a nuke to both
 --bullets and nukes
 add(nukes,new_bullet(64,64,"b"))
end

function _update()
 for l in all(lasers) do
  --add code here!
  --specific update for lasers
 end

 for n in all(nukes) do
  --add code here!
  --specific update for nukes
 end
end

function _draw()
 cls()
 for b in all(bullets) do
  --commands that affect
  --both lasers and nukes
  print(b.msg,b.x,b.y,7)
 end
end
P#49295 2018-02-15 10:04 ( Edited 2018-02-15 15:04)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-06-06 11:38 | 0.018s | 2097k | Q:31