Jump to content




Peripheral works even if it is not wrapped


  • You cannot reply to this topic
2 replies to this topic

#1 tattyseal

  • Members
  • 15 posts

Posted 12 July 2014 - 12:47 PM

Hey guys, I am making a peripheral for ComputerCraft, that queues an event when it is right clicked, but it works even if it is not been wrapped with peripheral.wrap

Block
Spoiler

TileEntity
Spoiler

Peripheral Provider
Spoiler

Thanks :)

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 July 2014 - 01:01 PM

Yes, that is how events work. Peripheral wrapping is simply a process of invoking methods on peripherals easier than having to do a peripheral.call each time.

If you do wish to only have 'registered' computers provided the event, I would probably suggest forcing the developer/user to invoke a method, lets say something like eventSubscribe and then keeping a copy of the IComputerAccess from the callMethod method, as this is the computer that the method has been invoked from. Obviously only retain this computer if it isn't already in the list though, or else you'll start getting duplicate events if they invoke your register method multiple times. I'd probably suggest providing a unregister method too. Then whenever you wish to queue an event, you'd loop through this list of 'registered' computers and queue the event to them.

Btw it should be noted that multiple computers can be attached to your peripheral, so you should retain a list of the computers, not just retain the last attached computer like you're currently doing.
// A list of all computers attached to this peripheral
protected ArrayList<IComputerAccess> computers = new ArrayList<IComputerAccess>();

@Override
public void attach(IComputerAccess computer) {
  computers.add(computer);
}

@Override
public void detach(IComputerAccess computer) {
  computers.remove(computer);
}

// a method for queueing events to all attached computers

protected void computerQueueEvent(String event, Object... args) {
  for (IComputerAccess computer : computers) {
	computer.queueEvent(event, args);
  }
}

Edited by theoriginalbit, 12 July 2014 - 01:02 PM.


#3 tattyseal

  • Members
  • 15 posts

Posted 12 July 2014 - 02:55 PM

View Posttheoriginalbit, on 12 July 2014 - 01:01 PM, said:

Yes, that is how events work. Peripheral wrapping is simply a process of invoking methods on peripherals easier than having to do a peripheral.call each time.

If you do wish to only have 'registered' computers provided the event, I would probably suggest forcing the developer/user to invoke a method, lets say something like eventSubscribe and then keeping a copy of the IComputerAccess from the callMethod method, as this is the computer that the method has been invoked from. Obviously only retain this computer if it isn't already in the list though, or else you'll start getting duplicate events if they invoke your register method multiple times. I'd probably suggest providing a unregister method too. Then whenever you wish to queue an event, you'd loop through this list of 'registered' computers and queue the event to them.

Btw it should be noted that multiple computers can be attached to your peripheral, so you should retain a list of the computers, not just retain the last attached computer like you're currently doing.
// A list of all computers attached to this peripheral
protected ArrayList<IComputerAccess> computers = new ArrayList<IComputerAccess>();

@Override
public void attach(IComputerAccess computer) {
  computers.add(computer);
}

@Override
public void detach(IComputerAccess computer) {
  computers.remove(computer);
}

// a method for queueing events to all attached computers

protected void computerQueueEvent(String event, Object... args) {
  for (IComputerAccess computer : computers) {
	computer.queueEvent(event, args);
  }
}

Thanks, I think that is the best way too now you mention it! Thanks a bunch! :)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users