Maybe an arg or extra question...?

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Maybe an arg or extra question...?

Post by sheamkennedy »

My knowledge of args is pretty basic and I've never worked with extra data before. I'm not sure what's best to accomplish this (maybe theres a better way entirely) so I'd like some direction.

A game I'm working on is a rouge-like turnbased sort of system that's evolved into a dice-roll-to-move type of gameplay.

The gameplay proceeds as follows:
1) You roll a die and it's a value from 1-6.
2) You are then able to walk your hero that many spaces and then end your turn.
3) Now every single NPC on the map get's their turn to move. They move one at a time. The code for this is called in the autorun and looks like this:

Code: Select all

script, moveNPCs, begin
variable(npc) 
for(npc, -1, -300, -1) do(
  if(get NPC id(npc) >= 0) then(
  # this NPC exists
    variable(i)
    for(i, 0, 1) do(
      set NPC speed(npc, 4)
      wait
      suspend NPCs
      wait for NPC(npc)
      set NPC speed(npc, 0)
      resume NPCs
    )
  )
)
end
As you can see, there is a for-loop which currently allows every single NPC on the map to move exactly 2 tiles total.

What I'd prefer is if I could somehow set some sort of data inside the editor which represents each NPC's movement range. I want every NPC to have it's own movement range. For example, guards always move 3 tiles, robots always move 4 tiles, etc... Is there anyway I can simply set each NPCs movement range in the editor and then pull that number into my code to modify how many times the for-loop iterates?
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7660
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

You could certainly use the NPC's "Script argument" value for this.

Normally, the script argument is passed to the the NPC's activation "Run Script" but as long as you are not using that, you can make use of the data in your own way.

Code: Select all

script, moveNPCs, begin
variable(npc)
for(npc, -1, -300, -1) do(
  if(get NPC id(npc) >= 0) then(
  # this NPC exists
    variable(walk range)
    walk range := read npc(npc, NPCstat:script argument)
    variable(i)
    for(i, 1, walk range) do(
      set NPC speed(npc, 4)
      wait
      suspend NPCs
      wait for NPC(npc)
      set NPC speed(npc, 0)
      resume NPCs
    )
  )
)
end 
Let me know how that works
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7660
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

Oh, also!

Your method for looping through all the NPCs on the map is okay, and will work fine, but here is an even better method that will loop through all NPCs on the map even if we add support for more than 300 NPC instances at some point in the future

Code: Select all

script, moveNPCs, begin
variable(npc)
npc := next NPC reference()  # The first NPC
while(npc) do(
  if(get NPC id(npc) >= 0) then(
  # this NPC exists
    variable(walk range)
    walk range := read npc(npc, NPCstat:script argument)
    variable(i)
    for(i, 1, walk range) do(
      set NPC speed(npc, 4)
      wait
      suspend NPCs
      wait for NPC(npc)
      set NPC speed(npc, 0)
      resume NPCs
    )
  )
  npc := next NPC reference(npc)
)
end 
Rather than using a "for" loop, we use:

Code: Select all

npc := next NPC reference()
to get a reference to the first npc on the map.

Since a valid NPC reference number is always non-zero we can say:

Code: Select all

while(npc) do(
And at the end of the while loop, we fetch the next npc on the map:

Code: Select all

npc := next NPC reference(npc)
And when the last npc on the map is finished, the npc variable will be set to 0, ending the "while" loop.
Last edited by Bob the Hamster on Tue Nov 14, 2017 6:04 pm, edited 1 time in total.
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

EDIT: Just tested the script out and it's working perfectly, thanks again! I modified the script so the camera moves from NPC to NPC and follows their movement and I also added a check so the camera follow only focuses on NPC's that have a positive arg value that way I can exclude certain NPCs from the script and have them behave like normal NPCs.

Thanks a lot! I'll get something going soon. I'm sure the script you sent will work. I just didn't really know how to access the arg data with a script but now I understand.

I'll also look into changing my script to support over 300 NPCs. Thanks!
Last edited by sheamkennedy on Thu Nov 16, 2017 1:13 pm, edited 3 times in total.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Post Reply